blob: ef77590761714d22761fc462608efba8d9998978 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
29
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070032#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include <utils/Errors.h>
35#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080036#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080038#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060040#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070041#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080043
Dan Stoza6b9454d2014-11-07 16:00:59 -080044#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080045#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080046#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
Yiwei Zhang60d1a192018-03-07 14:52:28 -080048#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020049#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070050#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070052#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070053#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian1b031492012-06-20 17:51:20 -070056#include "DisplayHardware/HWComposer.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080057#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070058
Peiyong Lincbc184f2018-08-22 13:24:10 -070059#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070060
Dan Stozac5da2712016-07-20 15:38:12 -070061#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070062#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070063
David Sodman41fdfc92017-11-06 16:09:56 -080064#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066namespace android {
67
Yiwei Zhang5434a782018-12-05 18:06:32 -080068using base::StringAppendF;
69
Lloyd Piquef1c675b2018-09-12 20:45:39 -070070std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072Layer::Layer(const LayerCreationArgs& args)
73 : mFlinger(args.flinger),
74 mName(args.name),
75 mClientRef(args.client),
76 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
79 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070080 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
81 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
82 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070083
Dan Stozaf7ba41a2017-05-10 15:11:11 -070084 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
97 mCurrentState.appId = 0;
98 mCurrentState.type = 0;
99 mCurrentState.active.w = UINT32_MAX;
100 mCurrentState.active.h = UINT32_MAX;
101 mCurrentState.active.transform.set(0, 0);
102 mCurrentState.transform = 0;
103 mCurrentState.transformToDisplayInverse = false;
104 mCurrentState.crop.makeInvalid();
105 mCurrentState.acquireFence = new Fence(-1);
106 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
107 mCurrentState.hdrMetadata.validTypes = 0;
108 mCurrentState.surfaceDamageRegion.clear();
109 mCurrentState.cornerRadius = 0.0f;
110 mCurrentState.api = -1;
111 mCurrentState.hasColorTransform = false;
Valerie Haued54efa2019-01-11 20:03:14 -0800112 mCurrentState.colorDataspace = ui::Dataspace::UNKNOWN;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113
114 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800115 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700116
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800117 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700118 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800119 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200120 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700121
122 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700123}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700124
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800126 sp<Client> c(mClientRef.promote());
127 if (c != 0) {
128 c->detachLayer(this);
129 }
130
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000131 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700132
chaviw61626f22018-11-15 16:26:27 -0800133 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700134
135 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700136}
137
Mathias Agopian13127d82013-03-05 17:47:11 -0800138// ---------------------------------------------------------------------------
139// callbacks
140// ---------------------------------------------------------------------------
141
David Sodmaneb085e02017-10-05 18:49:04 -0700142/*
143 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
144 * Layer. So, the implementation is done in BufferLayer. When called on a
145 * ColorLayer object, it's essentially a NOP.
146 */
David Sodmaneb085e02017-10-05 18:49:04 -0700147void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800148
Chia-I Wuc6657022017-08-15 11:18:17 -0700149void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700150 mRemovedFromCurrentState = true;
151
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800152 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
153 if (mCurrentState.zOrderRelativeOf != nullptr) {
154 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
155 if (strongRelative != nullptr) {
156 strongRelative->removeZOrderRelative(this);
157 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700158 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800159 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carr5edb1ad2017-04-25 10:54:24 -0700160 }
Rob Carr4bba3702018-10-08 21:53:30 +0000161
Robert Carr2e102c92018-10-23 12:11:15 -0700162 // Since we are no longer reachable from CurrentState SurfaceFlinger
163 // will no longer invoke doTransaction for us, and so we will
164 // never finish applying transactions. We signal the sync point
165 // now so that another layer will not become indefinitely
166 // blocked.
167 for (auto& point: mRemoteSyncPoints) {
168 point->setTransactionApplied();
169 }
170 mRemoteSyncPoints.clear();
171
172 {
173 Mutex::Autolock syncLock(mLocalSyncPointMutex);
174 for (auto& point : mLocalSyncPoints) {
175 point->setFrameAvailable();
176 }
177 mLocalSyncPoints.clear();
178 }
179
Chia-I Wuc6657022017-08-15 11:18:17 -0700180 for (const auto& child : mCurrentChildren) {
181 child->onRemovedFromCurrentState();
182 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800183
184 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700185}
Chia-I Wu38512252017-05-17 14:36:16 -0700186
chaviw61626f22018-11-15 16:26:27 -0800187void Layer::addToCurrentState() {
188 mRemovedFromCurrentState = false;
189
190 for (const auto& child : mCurrentChildren) {
191 child->addToCurrentState();
192 }
193}
194
Mathias Agopian13127d82013-03-05 17:47:11 -0800195// ---------------------------------------------------------------------------
196// set-up
197// ---------------------------------------------------------------------------
198
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700199const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800200 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201}
202
chaviw13fdc492017-06-27 12:40:18 -0700203bool Layer::getPremultipledAlpha() const {
204 return mPremultipliedAlpha;
205}
206
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700207sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800208 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700209 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800210}
211
212// ---------------------------------------------------------------------------
213// h/w composer set-up
214// ---------------------------------------------------------------------------
215
Dominik Laskowski075d3172018-05-24 15:50:06 -0700216bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700217 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
218 to_string(displayId).c_str());
David Sodmanb8aaea12017-12-14 15:54:51 -0800219 auto layer = std::shared_ptr<HWC2::Layer>(
220 hwc->createLayer(displayId),
221 [hwc, displayId](HWC2::Layer* layer) {
222 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 if (!layer) {
224 return false;
225 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700226 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700227 hwcInfo.hwc = hwc;
228 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700229 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700230 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 return true;
232}
233
Dominik Laskowski075d3172018-05-24 15:50:06 -0700234bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700235 if (!hasHwcLayer(displayId)) {
Chia-I Wu83806892017-11-16 10:50:20 -0800236 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700237 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700238 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800239 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700240 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800241 hwcInfo.layer = nullptr;
242
Chia-I Wu83806892017-11-16 10:50:20 -0800243 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700244}
245
chaviw61626f22018-11-15 16:26:27 -0800246void Layer::destroyHwcLayersForAllDisplays() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700248 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700249 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
250 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700251 }
chaviw61626f22018-11-15 16:26:27 -0800252}
253
254void Layer::destroyAllHwcLayersPlusChildren() {
255 destroyHwcLayersForAllDisplays();
David Sodman6f65f3e2017-11-03 14:28:09 -0700256 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800257 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700258
259 for (const sp<Layer>& child : mDrawingChildren) {
chaviw61626f22018-11-15 16:26:27 -0800260 child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700261 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700262}
Steven Thomasb02664d2017-07-26 18:48:28 -0700263
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800264Rect Layer::getContentCrop() const {
265 // this is the crop rectangle that applies to the buffer
266 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 Rect crop;
268 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800269 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700270 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800271 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800272 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800273 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700274 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800275 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700276 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700277 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700278 return crop;
279}
280
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700281static Rect reduce(const Rect& win, const Region& exclude) {
282 if (CC_LIKELY(exclude.isEmpty())) {
283 return win;
284 }
285 if (exclude.isRect()) {
286 return win.reduce(exclude.getBounds());
287 }
288 return Region(win).subtract(exclude).getBounds();
289}
290
Dan Stoza80d61162017-12-20 15:57:52 -0800291static FloatRect reduce(const FloatRect& win, const Region& exclude) {
292 if (CC_LIKELY(exclude.isEmpty())) {
293 return win;
294 }
295 // Convert through Rect (by rounding) for lack of FloatRegion
296 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
297}
298
Vishnu Nair51625fa2018-12-06 13:54:33 -0800299Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
300 const State& s(getDrawingState());
301 Region transparentRegion = reduceTransparentRegion ? getActiveTransparentRegion(s) : Region();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800302 FloatRect bounds = computeBounds(transparentRegion);
303 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800304 // Transform to screen space.
305 bounds = t.transform(bounds);
306 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700307}
308
Dan Stoza80d61162017-12-20 15:57:52 -0800309FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000310 const State& s(getDrawingState());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800311 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700312}
313
Dan Stoza80d61162017-12-20 15:57:52 -0800314FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000315 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800316 Rect bounds = getCroppedBufferSize(s);
317 FloatRect floatBounds = bounds.toFloatRect();
318 if (bounds.isValid()) {
319 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
320 // that they can clip it.
321 floatBounds = cropChildBounds(floatBounds);
322 } else {
323 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
324 // parent bounds and inverting the transform to get the maximum bounds we can have that
325 // will fit within our parent bounds.
326 const auto& p = mDrawingParent.promote();
327 if (p != nullptr) {
328 ui::Transform t = s.active_legacy.transform;
329 // When calculating the parent bounds for purposes of clipping, we don't need to
330 // constrain the parent to its transparent region. The transparent region is an
331 // optimization based on the buffer contents of the layer, but does not affect the
332 // space allocated to it by policy, and thus children should be allowed to extend into
333 // the parent's transparent region.
334 // One of the main uses is a parent window with a child sitting behind the parent
335 // window, marked by a transparent region. When computing the parent bounds from the
336 // parent's perspective we pass in the transparent region to reduce buffer allocation
337 // size. When computing the parent bounds from the child's perspective, we pass in an
338 // empty transparent region in order to extend into the the parent bounds.
339 floatBounds = p->computeBounds(Region());
340 // Transform back to layer space.
341 floatBounds = t.inverse().transform(floatBounds);
342 }
343 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700344
Vishnu Nair60356342018-11-13 13:00:45 -0800345 // Subtract the transparent region and snap to the bounds.
346 return reduce(floatBounds, activeTransparentRegion);
347}
348
349FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
350 const State& s(getDrawingState());
351 Rect bounds = getCroppedBufferSize(s);
352 FloatRect croppedBounds = childBounds;
353
354 // If the layer has bounds, then crop the passed in child bounds and pass
355 // it to our parents so they can crop it as well. If the layer has no bounds,
356 // then pass on the child bounds.
357 if (bounds.isValid()) {
358 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800359 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700360
Chia-I Wue41dbe62017-06-13 14:10:56 -0700361 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700362 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800363 // Transform to parent space and allow parent layer to crop the
364 // child bounds as well.
365 ui::Transform t = s.active_legacy.transform;
366 croppedBounds = t.transform(croppedBounds);
367 croppedBounds = p->cropChildBounds(croppedBounds);
368 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700369 }
Vishnu Nair60356342018-11-13 13:00:45 -0800370 return croppedBounds;
371}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700372
Vishnu Nair60356342018-11-13 13:00:45 -0800373Rect Layer::getCroppedBufferSize(const State& s) const {
374 Rect size = getBufferSize(s);
375 Rect crop = getCrop(s);
376 if (!crop.isEmpty() && size.isValid()) {
377 size.intersect(crop, &size);
378 } else if (!crop.isEmpty()) {
379 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700380 }
Vishnu Nair60356342018-11-13 13:00:45 -0800381 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800382}
383
Alec Mourie0451f72019-01-07 19:42:02 -0800384Rect Layer::computeInitialCrop(const Rect& windowBounds) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700385 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800386 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700387 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800388
389 // apply the projection's clipping to the window crop in
390 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700391 // if there are no window scaling involved, this operation will map to full
392 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700393
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800394 FloatRect activeCropFloat = computeBounds();
Alec Mourie0451f72019-01-07 19:42:02 -0800395
396 // If we have valid window boundaries then we need to crop to the window
397 // boundaries in layer space.
398 if (windowBounds.isValid()) {
399 const ui::Transform t = getTransform();
400 // Transform to screen space.
401 activeCropFloat = t.transform(activeCropFloat);
402 activeCropFloat = activeCropFloat.intersect(windowBounds.toFloatRect());
403 // Back to layer space to work with the content crop.
404 activeCropFloat = t.inverse().transform(activeCropFloat);
405 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800406 // This needs to be here as transform.transform(Rect) computes the
407 // transformed rect and then takes the bounding box of the result before
408 // returning. This means
409 // transform.inverse().transform(transform.transform(Rect)) != Rect
410 // in which case we need to make sure the final rect is clipped to the
411 // display bounds.
412 Rect activeCrop{activeCropFloat};
413 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 activeCrop.clear();
415 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700416 return activeCrop;
417}
418
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700419void Layer::setupRoundedCornersCropCoordinates(Rect win,
420 const FloatRect& roundedCornersCrop) const {
421 // Translate win by the rounded corners rect coordinates, to have all values in
422 // layer coordinate space.
423 win.left -= roundedCornersCrop.left;
424 win.right -= roundedCornersCrop.left;
425 win.top -= roundedCornersCrop.top;
426 win.bottom -= roundedCornersCrop.top;
427
428 renderengine::Mesh::VertexArray<vec2> cropCoords(getBE().mMesh.getCropCoordArray<vec2>());
429 cropCoords[0] = vec2(win.left, win.top);
430 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
431 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
432 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700433}
434
Alec Mourie0451f72019-01-07 19:42:02 -0800435FloatRect Layer::computeCrop(const Rect& windowBounds) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700436 // the content crop is the area of the content that gets scaled to the
437 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800438 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700439
440 // In addition there is a WM-specified crop we pull from our drawing state.
441 const State& s(getDrawingState());
442
Alec Mourie0451f72019-01-07 19:42:02 -0800443 Rect activeCrop = computeInitialCrop(windowBounds);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800444 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700445
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 // Transform the window crop to match the buffer coordinate system,
447 // which means using the inverse of the current transform set on the
448 // SurfaceFlingerConsumer.
449 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800450 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700452 * the code below applies the primary display's inverse transform to the
453 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 */
David Sodman41fdfc92017-11-06 16:09:56 -0800455 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000456 // calculate the inverse transform
457 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800458 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800459 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700461 invTransform = (ui::Transform(invTransformOrient) *
462 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800463 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800465 int winWidth = bufferSize.getWidth();
466 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
468 // If the activeCrop has been rotate the ends are rotated but not
469 // the space itself so when transforming ends back we can't rely on
470 // a modification of the axes of rotation. To account for this we
471 // need to reorient the inverse rotation in terms of the current
472 // axes of rotation.
473 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
474 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
475 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800476 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000477 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800478 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000479 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800480 const Rect winCrop =
481 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000482
483 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800484 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000485 float yScale = crop.getHeight() / float(winHeight);
486
David Sodman41fdfc92017-11-06 16:09:56 -0800487 float insetL = winCrop.left * xScale;
488 float insetT = winCrop.top * yScale;
489 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000490 float insetB = (winHeight - winCrop.bottom) * yScale;
491
David Sodman41fdfc92017-11-06 16:09:56 -0800492 crop.left += insetL;
493 crop.top += insetT;
494 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000495 crop.bottom -= insetB;
496
Mathias Agopian13127d82013-03-05 17:47:11 -0800497 return crop;
498}
499
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700500void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700501 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700502 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700503 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700504 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700505
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700506 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507 hwcInfo.forceClientComposition = false;
508
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800509 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510 hwcInfo.forceClientComposition = true;
511 }
512
David Sodman15094112018-10-11 09:39:37 -0700513 auto& hwcLayer = hwcInfo.layer;
514
Mathias Agopian13127d82013-03-05 17:47:11 -0800515 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700516 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800517 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500518 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800519 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800520 blendMode =
521 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800522 }
David Sodman15094112018-10-11 09:39:37 -0700523 auto error = hwcLayer->setBlendMode(blendMode);
524 ALOGE_IF(error != HWC2::Error::None,
525 "[%s] Failed to set blend mode %s:"
526 " %s (%d)",
527 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
528 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700529 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800530
531 // apply the layer's transform, followed by the display's global transform
532 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700533 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800534 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700535 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800536 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700537 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700538 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000539 activeCrop.clear();
540 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700541 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800542 // This needs to be here as transform.transform(Rect) computes the
543 // transformed rect and then takes the bounding box of the result before
544 // returning. This means
545 // transform.inverse().transform(transform.transform(Rect)) != Rect
546 // in which case we need to make sure the final rect is clipped to the
547 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800548 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000549 activeCrop.clear();
550 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700551 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800552 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
553 activeTransparentRegion.orSelf(
554 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800555 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
556 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800557 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700558 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700559
Dan Stoza80d61162017-12-20 15:57:52 -0800560 // computeBounds returns a FloatRect to provide more accuracy during the
561 // transformation. We then round upon constructing 'frame'.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800562 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700563 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000564 frame.clear();
565 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700566 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700568 error = hwcLayer->setDisplayFrame(transformedFrame);
569 if (error != HWC2::Error::None) {
570 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
571 transformedFrame.left, transformedFrame.top, transformedFrame.right,
572 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
573 } else {
574 hwcInfo.displayFrame = transformedFrame;
575 }
David Sodmanba340492018-08-05 21:51:33 -0700576 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800577
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700578 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700579 error = hwcLayer->setSourceCrop(sourceCrop);
580 if (error != HWC2::Error::None) {
581 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
582 "%s (%d)",
583 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
584 to_string(error).c_str(), static_cast<int32_t>(error));
585 } else {
586 hwcInfo.sourceCrop = sourceCrop;
587 }
David Sodmanba340492018-08-05 21:51:33 -0700588 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800590 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700591 error = hwcLayer->setPlaneAlpha(alpha);
592 ALOGE_IF(error != HWC2::Error::None,
593 "[%s] Failed to set plane alpha %.3f: "
594 "%s (%d)",
595 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700596 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800597
David Sodman15094112018-10-11 09:39:37 -0700598 error = hwcLayer->setZOrder(z);
599 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
600 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700601 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500602
Albert Chaulk2a589632017-05-04 16:59:44 -0400603 int type = s.type;
604 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700605 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400606 if (parent.get()) {
607 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700608 if (parentState.type >= 0 || parentState.appId >= 0) {
609 type = parentState.type;
610 appId = parentState.appId;
611 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400612 }
613
David Sodman15094112018-10-11 09:39:37 -0700614 error = hwcLayer->setInfo(type, appId);
615 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
616 static_cast<int32_t>(error));
617
David Sodmanba340492018-08-05 21:51:33 -0700618 getBE().compositionInfo.hwc.type = type;
619 getBE().compositionInfo.hwc.appId = appId;
620
Mathias Agopian29a367b2011-07-12 14:51:45 -0700621 /*
622 * Transformations are applied in this order:
623 * 1) buffer orientation/flip/mirror
624 * 2) state transformation (window manager)
625 * 3) layer orientation (screen orientation)
626 * (NOTE: the matrices are multiplied in reverse order)
627 */
628
Peiyong Linefefaac2018-08-17 12:27:51 -0700629 const ui::Transform bufferOrientation(mCurrentTransform);
630 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700631
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800632 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700633 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700634 * the code below applies the primary display's inverse transform to the
635 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636 */
David Sodman41fdfc92017-11-06 16:09:56 -0800637 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700638 // calculate the inverse transform
639 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800640 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700641 }
Robert Carrcae605c2017-03-29 12:10:31 -0700642
643 /*
644 * Here we cancel out the orientation component of the WM transform.
645 * The scaling and translate components are already included in our bounds
646 * computation so it's enough to just omit it in the composition.
647 * See comment in onDraw with ref to b/36727915 for why.
648 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700649 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700650 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700651
652 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800653 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700654 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800655 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700656 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700657 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 } else {
659 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800660 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700661 auto error = hwcLayer->setTransform(transform);
662 ALOGE_IF(error != HWC2::Error::None,
663 "[%s] Failed to set transform %s: "
664 "%s (%d)",
665 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
666 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700667 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800668 }
669}
670
Dominik Laskowski075d3172018-05-24 15:50:06 -0700671void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700672 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700673 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674}
Dan Stozaee44edd2015-03-23 15:50:23 -0700675
Dominik Laskowski075d3172018-05-24 15:50:06 -0700676bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700677 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700678 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800679}
680
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700681void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700682 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700683 LOG_ALWAYS_FATAL_IF(!displayId);
684 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685 return;
686 }
687
688 // This gives us only the "orientation" component of the transform
689 const State& s(getCurrentState());
690
691 // Apply the layer's transform, followed by the display's global transform
692 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800693 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700695 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800696 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700697 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700698 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800699 auto position = displayTransform.transform(frame);
700
Dominik Laskowski7e045462018-05-30 13:02:02 -0700701 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700702 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
703
David Sodman41fdfc92017-11-06 16:09:56 -0800704 ALOGE_IF(error != HWC2::Error::None,
705 "[%s] Failed to set cursor position "
706 "to (%d, %d): %s (%d)",
707 mName.string(), position.left, position.top, to_string(error).c_str(),
708 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800709}
Riley Andrews03414a12014-07-01 14:22:59 -0700710
Mathias Agopian13127d82013-03-05 17:47:11 -0800711// ---------------------------------------------------------------------------
712// drawing...
713// ---------------------------------------------------------------------------
714
Marissa Wall61c58622018-07-18 10:12:20 -0700715void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700716 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800717}
718
Marissa Wall61c58622018-07-18 10:12:20 -0700719void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700720 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800721}
722
David Sodman41fdfc92017-11-06 16:09:56 -0800723void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
724 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800725 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700726 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700727 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700728 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
David Sodmanc1498e62018-09-12 14:36:26 -0700731void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
732 clearWithOpenGL(renderArea, 0, 0, 0, 0);
733}
734
Dominik Laskowski075d3172018-05-24 15:50:06 -0700735void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700736 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700737 ALOGE("setCompositionType called without a valid HWC layer");
738 return;
David Sodman15094112018-10-11 09:39:37 -0700739 }
740 auto& hwcInfo = getBE().mHwcLayers[displayId];
741 auto& hwcLayer = hwcInfo.layer;
742 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
743 static_cast<int>(callIntoHwc));
744 if (hwcInfo.compositionType != type) {
745 ALOGV(" actually setting");
746 hwcInfo.compositionType = type;
747 if (callIntoHwc) {
748 auto error = (hwcLayer)->setCompositionType(type);
749 ALOGE_IF(error != HWC2::Error::None,
750 "[%s] Failed to set "
751 "composition type %s: %s (%d)",
752 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
753 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700754 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800755 }
756}
757
Dominik Laskowski075d3172018-05-24 15:50:06 -0700758HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
759 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700760 // If we're querying the composition type for a display that does not
761 // have a HWC counterpart, then it will always be Client
762 return HWC2::Composition::Client;
763 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700764 if (getBE().mHwcLayers.count(*displayId) == 0) {
765 ALOGE("getCompositionType called with an invalid HWC layer");
766 return HWC2::Composition::Invalid;
767 }
768 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769}
770
Dominik Laskowski075d3172018-05-24 15:50:06 -0700771void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700772 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773 ALOGE("setClearClientTarget called without a valid HWC layer");
774 return;
775 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700776 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800777}
778
Dominik Laskowski075d3172018-05-24 15:50:06 -0700779bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700780 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781 ALOGE("getClearClientTarget called without a valid HWC layer");
782 return false;
783 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700784 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800785}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800786
Dan Stozacac35382016-01-27 12:21:06 -0800787bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
788 if (point->getFrameNumber() <= mCurrentFrameNumber) {
789 // Don't bother with a SyncPoint, since we've already latched the
790 // relevant frame
791 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700792 }
Robert Carr2e102c92018-10-23 12:11:15 -0700793 if (isRemovedFromCurrentState()) {
794 return false;
795 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700796
Dan Stozacac35382016-01-27 12:21:06 -0800797 Mutex::Autolock lock(mLocalSyncPointMutex);
798 mLocalSyncPoints.push_back(point);
799 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700800}
801
Mathias Agopian13127d82013-03-05 17:47:11 -0800802// ----------------------------------------------------------------------------
803// local state
804// ----------------------------------------------------------------------------
805
Peiyong Lin833074a2018-08-28 11:53:54 -0700806void Layer::computeGeometry(const RenderArea& renderArea,
807 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700808 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700809 const ui::Transform renderAreaTransform(renderArea.getTransform());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800810 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700811
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000812 vec2 lt = vec2(win.left, win.top);
813 vec2 lb = vec2(win.left, win.bottom);
814 vec2 rb = vec2(win.right, win.bottom);
815 vec2 rt = vec2(win.right, win.top);
816
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800817 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000818 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700819 lt = layerTransform.transform(lt);
820 lb = layerTransform.transform(lb);
821 rb = layerTransform.transform(rb);
822 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000823 }
824
Peiyong Lin833074a2018-08-28 11:53:54 -0700825 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700826 position[0] = renderAreaTransform.transform(lt);
827 position[1] = renderAreaTransform.transform(lb);
828 position[2] = renderAreaTransform.transform(rb);
829 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800830}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800831
David Sodman41fdfc92017-11-06 16:09:56 -0800832bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800833 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700834 return (s.flags & layer_state_t::eLayerSecure);
835}
836
Mathias Agopian13127d82013-03-05 17:47:11 -0800837void Layer::setVisibleRegion(const Region& visibleRegion) {
838 // always called from main thread
839 this->visibleRegion = visibleRegion;
840}
841
842void Layer::setCoveredRegion(const Region& coveredRegion) {
843 // always called from main thread
844 this->coveredRegion = coveredRegion;
845}
846
David Sodman41fdfc92017-11-06 16:09:56 -0800847void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800848 // always called from main thread
849 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
850}
851
Robert Carre5f4f692018-01-12 13:12:28 -0800852void Layer::clearVisibilityRegions() {
853 visibleRegion.clear();
854 visibleNonTransparentRegion.clear();
855 coveredRegion.clear();
856}
857
Mathias Agopian13127d82013-03-05 17:47:11 -0800858// ----------------------------------------------------------------------------
859// transaction
860// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800861
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800862void Layer::pushPendingState() {
863 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700864 return;
865 }
866
Dan Stoza7dde5992015-05-22 09:51:44 -0700867 // If this transaction is waiting on the receipt of a frame, generate a sync
868 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700869 // We don't allow installing sync points after we are removed from the current state
870 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800871 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
872 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800873 if (barrierLayer == nullptr) {
874 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700875 // If we can't promote the layer we are intended to wait on,
876 // then it is expired or otherwise invalid. Allow this transaction
877 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800878 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800879 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800880 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800881 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800882 mRemoteSyncPoints.push_back(std::move(syncPoint));
883 } else {
884 // We already missed the frame we're supposed to synchronize
885 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800886 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800887 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700888 }
889
Dan Stoza7dde5992015-05-22 09:51:44 -0700890 // Wake us up to check if the frame has been received
891 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700892 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700893 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 mPendingStates.push_back(mCurrentState);
895 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700896}
897
Pablo Ceballos05289c22016-04-14 15:49:55 -0700898void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800899 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700900
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 mPendingStates.removeAt(0);
902 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700903}
904
Pablo Ceballos05289c22016-04-14 15:49:55 -0700905bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700906 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800907 while (!mPendingStates.empty()) {
908 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700909 if (mRemoteSyncPoints.empty()) {
910 // If we don't have a sync point for this, apply it anyway. It
911 // will be visually wrong, but it should keep us from getting
912 // into too much trouble.
913 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700914 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 stateUpdateAvailable = true;
916 continue;
917 }
918
Marissa Wallf58c14b2018-07-24 10:50:43 -0700919 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800920 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800921 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800922
923 // Signal our end of the sync point and then dispose of it
924 mRemoteSyncPoints.front()->setTransactionApplied();
925 mRemoteSyncPoints.pop_front();
926 continue;
927 }
928
Dan Stoza7dde5992015-05-22 09:51:44 -0700929 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
930 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700931 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700932 stateUpdateAvailable = true;
933
934 // Signal our end of the sync point and then dispose of it
935 mRemoteSyncPoints.front()->setTransactionApplied();
936 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800937 } else {
938 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700940 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700941 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700942 stateUpdateAvailable = true;
943 }
944 }
945
946 // If we still have pending updates, wake SurfaceFlinger back up and point
947 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800948 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700949 setTransactionFlags(eTransactionNeeded);
950 mFlinger->setTransactionFlags(eTraversalNeeded);
951 }
952
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800953 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700954 return stateUpdateAvailable;
955}
956
Marissa Wall61c58622018-07-18 10:12:20 -0700957uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000958 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959
Marissa Wall61c58622018-07-18 10:12:20 -0700960 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
961 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700962
David Sodmaneb085e02017-10-05 18:49:04 -0700963 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700964 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000965 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800966 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
967 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
968 " requested={ wh={%4u,%4u} }}\n"
969 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
970 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700971 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700972 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
973 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
974 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
975 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
976 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700977 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
978 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
979 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700981
Robert Carre392b552017-09-19 12:16:05 -0700982 // Don't let Layer::doTransaction update the drawing state
983 // if we have a pending resize, unless we are in fixed-size mode.
984 // the drawing state will be updated only once we receive a buffer
985 // with the correct size.
986 //
987 // In particular, we want to make sure the clip (which is part
988 // of the geometry state) is latched together with the size but is
989 // latched immediately when no resizing is involved.
990 //
991 // If a sideband stream is attached, however, we want to skip this
992 // optimization so that transactions aren't missed when a buffer
993 // never arrives
994 //
995 // In the case that we don't have a buffer we ignore other factors
996 // and avoid entering the resizePending state. At a high level the
997 // resizePending state is to avoid applying the state of the new buffer
998 // to the old buffer. However in the state where we don't have an old buffer
999 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001000 const bool resizePending =
1001 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1002 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001003 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001004 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001005 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001006 flags |= eDontUpdateGeometryState;
1007 }
1008 }
1009
Robert Carr7bf247e2017-05-18 14:02:49 -07001010 // Here we apply various requested geometry states, depending on our
1011 // latching configuration. See Layer.h for a detailed discussion of
1012 // how geometry latching is controlled.
1013 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001014 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001015
1016 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1017 // mode, which causes attributes which normally latch regardless of scaling mode,
1018 // to be delayed. We copy the requested state to the active state making sure
1019 // to respect these rules (again see Layer.h for a detailed discussion).
1020 //
1021 // There is an awkward asymmetry in the handling of the crop states in the position
1022 // states, as can be seen below. Largely this arises from position and transform
1023 // being stored in the same data structure while having different latching rules.
1024 // b/38182305
1025 //
Marissa Wall61c58622018-07-18 10:12:20 -07001026 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001027 // applyPendingStates in the presence of deferred transactions.
1028 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001029 float tx = stateToCommit->active_legacy.transform.tx();
1030 float ty = stateToCommit->active_legacy.transform.ty();
1031 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1032 stateToCommit->active_legacy.transform.set(tx, ty);
1033 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001034 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001035 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001036 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001037 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001038 }
1039
Marissa Wall61c58622018-07-18 10:12:20 -07001040 return flags;
1041}
1042
1043uint32_t Layer::doTransaction(uint32_t flags) {
1044 ATRACE_CALL();
1045
chaviw5aedec92018-10-22 10:40:38 -07001046 if (mLayerDetached) {
1047 return 0;
1048 }
1049
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001050 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001051 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001052 if (!applyPendingStates(&c)) {
1053 return 0;
1054 }
1055
1056 flags = doTransactionResize(flags, &c);
1057
Alec Mourib416efd2018-09-06 21:01:59 +00001058 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001059
1060 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 // invalidate and recompute the visible regions if needed
1062 flags |= Layer::eVisibleRegion;
1063 }
1064
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001065 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001066 // invalidate and recompute the visible regions if needed
1067 flags |= eVisibleRegion;
1068 this->contentDirty = true;
1069
1070 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001071 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001072 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 }
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
1241bool Layer::setColor(const half3& color) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001242 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1243 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001244 return false;
1245
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001246 mCurrentState.sequence++;
1247 mCurrentState.color.r = color.r;
1248 mCurrentState.color.g = color.g;
1249 mCurrentState.color.b = color.b;
1250 mCurrentState.modified = true;
chaviw13fdc492017-06-27 12:40:18 -07001251 setTransactionFlags(eTransactionNeeded);
1252 return true;
1253}
1254
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001255bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001256 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001257
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001258 mCurrentState.sequence++;
1259 mCurrentState.cornerRadius = cornerRadius;
1260 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001261 setTransactionFlags(eTransactionNeeded);
1262 return true;
1263}
1264
Robert Carrd4ae7f32018-06-07 16:10:57 -07001265bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1266 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001267 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001268 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1269
1270 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1271 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1272 return false;
1273 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001274 mCurrentState.sequence++;
1275 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1276 matrix.dsdy);
1277 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 setTransactionFlags(eTransactionNeeded);
1279 return true;
1280}
Marissa Wall61c58622018-07-18 10:12:20 -07001281
Mathias Agopian13127d82013-03-05 17:47:11 -08001282bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001283 mCurrentState.requestedTransparentRegion_legacy = transparent;
1284 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001285 setTransactionFlags(eTransactionNeeded);
1286 return true;
1287}
1288bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001289 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1290 if (mCurrentState.flags == newFlags) return false;
1291 mCurrentState.sequence++;
1292 mCurrentState.flags = newFlags;
1293 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001294 setTransactionFlags(eTransactionNeeded);
1295 return true;
1296}
Robert Carr99e27f02016-06-16 15:18:02 -07001297
Marissa Wallf58c14b2018-07-24 10:50:43 -07001298bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001299 if (mCurrentState.requestedCrop_legacy == crop) return false;
1300 mCurrentState.sequence++;
1301 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001302 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001303 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001304 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001305 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1306
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001307 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001308 setTransactionFlags(eTransactionNeeded);
1309 return true;
1310}
Robert Carr8d5227b2017-03-16 15:41:03 -07001311
Robert Carrc3574f72016-03-24 12:19:32 -07001312bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001313 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001314 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001315 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001316 return true;
1317}
1318
rongliucfb187b2018-03-14 12:26:23 -07001319void Layer::setInfo(int32_t type, int32_t appId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001320 mCurrentState.appId = appId;
1321 mCurrentState.type = type;
1322 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001323 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001324}
1325
Mathias Agopian13127d82013-03-05 17:47:11 -08001326bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001327 if (mCurrentState.layerStack == layerStack) return false;
1328 mCurrentState.sequence++;
1329 mCurrentState.layerStack = layerStack;
1330 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001331 setTransactionFlags(eTransactionNeeded);
1332 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001333}
1334
Robert Carr1f0a16a2016-10-24 16:27:39 -07001335uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001336 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001337 if (p == nullptr) {
1338 return getDrawingState().layerStack;
1339 }
1340 return p->getLayerStack();
1341}
1342
Marissa Wallf58c14b2018-07-24 10:50:43 -07001343void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001344 mCurrentState.barrierLayer_legacy = barrierLayer;
1345 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001346 // We don't set eTransactionNeeded, because just receiving a deferral
1347 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001348 mCurrentState.modified = true;
1349 pushPendingState();
1350 mCurrentState.barrierLayer_legacy = nullptr;
1351 mCurrentState.frameNumber_legacy = 0;
1352 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001353}
1354
Marissa Wallf58c14b2018-07-24 10:50:43 -07001355void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001356 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001357 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001358}
1359
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360// ----------------------------------------------------------------------------
1361// pageflip handling...
1362// ----------------------------------------------------------------------------
1363
Robert Carr1f0a16a2016-10-24 16:27:39 -07001364bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001365 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001366 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001367 if (parent != nullptr && parent->isHiddenByPolicy()) {
1368 return true;
1369 }
1370 return s.flags & layer_state_t::eLayerHidden;
1371}
1372
David Sodman41fdfc92017-11-06 16:09:56 -08001373uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001374 // TODO: should we do something special if mSecure is set?
1375 if (mProtectedByApp) {
1376 // need a hardware-protected path to external video sink
1377 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001378 }
Riley Andrews03414a12014-07-01 14:22:59 -07001379 if (mPotentialCursor) {
1380 usage |= GraphicBuffer::USAGE_CURSOR;
1381 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001382 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001383 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001384}
1385
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001386void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001387 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001388 // Disable setting transform hint if the debug flag is set or if the
1389 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1390 // in one orientation.
1391 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001392 // The transform hint is used to improve performance, but we can
1393 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001394 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001395 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001396 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001397 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001398 orientation = 0;
1399 }
1400 }
David Sodmaneb085e02017-10-05 18:49:04 -07001401 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001402}
1403
Mathias Agopian13127d82013-03-05 17:47:11 -08001404// ----------------------------------------------------------------------------
1405// debugging
1406// ----------------------------------------------------------------------------
1407
Marissa Wall61c58622018-07-18 10:12:20 -07001408// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001409LayerDebugInfo Layer::getLayerDebugInfo() const {
1410 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001411 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001412 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001413 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001414 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001415 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001416 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001417 info.mVisibleRegion = visibleRegion;
1418 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001419 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001420 info.mX = ds.active_legacy.transform.tx();
1421 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001422 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001423 info.mWidth = ds.active_legacy.w;
1424 info.mHeight = ds.active_legacy.h;
1425 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001426 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001427 info.mFlags = ds.flags;
1428 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001429 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001430 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1431 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1432 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1433 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001434 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001435 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001436 if (buffer != 0) {
1437 info.mActiveBufferWidth = buffer->getWidth();
1438 info.mActiveBufferHeight = buffer->getHeight();
1439 info.mActiveBufferStride = buffer->getStride();
1440 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001441 } else {
1442 info.mActiveBufferWidth = 0;
1443 info.mActiveBufferHeight = 0;
1444 info.mActiveBufferStride = 0;
1445 info.mActiveBufferFormat = 0;
1446 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001447 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001448 info.mNumQueuedFrames = getQueuedFrameCount();
1449 info.mRefreshPending = isBufferLatched();
1450 info.mIsOpaque = isOpaque(ds);
1451 info.mContentDirty = contentDirty;
1452 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001453}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001454
Yiwei Zhang5434a782018-12-05 18:06:32 -08001455void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001456 result.append("-------------------------------");
1457 result.append("-------------------------------");
1458 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001459 result.append(" Layer name\n");
1460 result.append(" Z | ");
1461 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001462 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001463 result.append(" Disp Frame (LTRB) | ");
1464 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001465 result.append("-------------------------------");
1466 result.append("-------------------------------");
1467 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001468}
1469
Yiwei Zhang5434a782018-12-05 18:06:32 -08001470void Layer::miniDump(std::string& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001471 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001472 return;
1473 }
1474
Yiwei Zhang5434a782018-12-05 18:06:32 -08001475 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001476 if (mName.length() > 77) {
1477 std::string shortened;
1478 shortened.append(mName.string(), 36);
1479 shortened.append("[...]");
1480 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001481 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001482 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001483 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001484 }
1485
Yiwei Zhang5434a782018-12-05 18:06:32 -08001486 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001487
Alec Mourib416efd2018-09-06 21:01:59 +00001488 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001489 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001490 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001491 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001492 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001493 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001494 }
Yiwei Zhang5434a782018-12-05 18:06:32 -08001495 StringAppendF(&result, "%10s | ", to_string(getCompositionType(displayId)).c_str());
1496 StringAppendF(&result, "%10s | ", to_string(hwcInfo.transform).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001497 const Rect& frame = hwcInfo.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001498 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001499 const FloatRect& crop = hwcInfo.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001500 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1501 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001502
David Sodman7e4ae112018-02-09 15:02:28 -08001503 result.append("- - - - - - - - - - - - - - - -\n");
1504
1505 std::string compositionInfoStr;
1506 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001507 result.append(compositionInfoStr);
David Sodman7e4ae112018-02-09 15:02:28 -08001508
Yichi Chen6ca35192018-05-29 12:20:43 +08001509 result.append("- - - - - - - - - - - - - - - -");
1510 result.append("- - - - - - - - - - - - - - - -");
1511 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001512}
Dan Stozae22aec72016-08-01 13:20:59 -07001513
Yiwei Zhang5434a782018-12-05 18:06:32 -08001514void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001515 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001516}
1517
Svetoslavd85084b2014-03-20 10:28:31 -07001518void Layer::clearFrameStats() {
1519 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001520}
1521
Jamie Gennis6547ff42013-07-16 20:12:42 -07001522void Layer::logFrameStats() {
1523 mFrameTracker.logAndResetStats(mName);
1524}
1525
Svetoslavd85084b2014-03-20 10:28:31 -07001526void Layer::getFrameStats(FrameStats* outStats) const {
1527 mFrameTracker.getStats(outStats);
1528}
1529
Yiwei Zhang5434a782018-12-05 18:06:32 -08001530void Layer::dumpFrameEvents(std::string& result) {
1531 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001532 Mutex::Autolock lock(mFrameEventHistoryMutex);
1533 mFrameEventHistory.checkFencesForCompletion();
1534 mFrameEventHistory.dump(result);
1535}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001536
Brian Anderson5ea5e592016-12-01 16:54:33 -08001537void Layer::onDisconnect() {
1538 Mutex::Autolock lock(mFrameEventHistoryMutex);
1539 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001540 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001541}
1542
Brian Anderson3890c392016-07-25 12:48:08 -07001543void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001544 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001545 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001546 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1547 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001548 }
1549
Brian Andersond6927fb2016-07-23 23:37:30 -07001550 Mutex::Autolock lock(mFrameEventHistoryMutex);
1551 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001552 // If there are any unsignaled fences in the aquire timeline at this
1553 // point, the previously queued frame hasn't been latched yet. Go ahead
1554 // and try to get the signal time here so the syscall is taken out of
1555 // the main thread's critical path.
1556 mAcquireTimeline.updateSignalTimes();
1557 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001558 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001559 mFrameEventHistory.addQueue(*newTimestamps);
1560 }
1561
Brian Anderson3890c392016-07-25 12:48:08 -07001562 if (outDelta) {
1563 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001564 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001565}
Dan Stozae77c7662016-05-13 11:37:28 -07001566
Chia-I Wu98f1c102017-05-30 14:54:08 -07001567size_t Layer::getChildrenCount() const {
1568 size_t count = 0;
1569 for (const sp<Layer>& child : mCurrentChildren) {
1570 count += 1 + child->getChildrenCount();
1571 }
1572 return count;
1573}
1574
Robert Carr1f0a16a2016-10-24 16:27:39 -07001575void Layer::addChild(const sp<Layer>& layer) {
1576 mCurrentChildren.add(layer);
1577 layer->setParent(this);
1578}
1579
1580ssize_t Layer::removeChild(const sp<Layer>& layer) {
1581 layer->setParent(nullptr);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001582
Robert Carr1f0a16a2016-10-24 16:27:39 -07001583 return mCurrentChildren.remove(layer);
1584}
1585
Robert Carr1db73f62016-12-21 12:58:51 -08001586bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1587 sp<Handle> handle = nullptr;
1588 sp<Layer> newParent = nullptr;
1589 if (newParentHandle == nullptr) {
1590 return false;
1591 }
1592 handle = static_cast<Handle*>(newParentHandle.get());
1593 newParent = handle->owner.promote();
1594 if (newParent == nullptr) {
1595 ALOGE("Unable to promote Layer handle");
1596 return false;
1597 }
1598
chaviw5aedec92018-10-22 10:40:38 -07001599 if (attachChildren()) {
1600 setTransactionFlags(eTransactionNeeded);
1601 }
Robert Carr1db73f62016-12-21 12:58:51 -08001602 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001603 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001604 }
1605 mCurrentChildren.clear();
1606
1607 return true;
1608}
1609
Robert Carr15eae092018-03-23 13:43:53 -07001610void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001611 for (const sp<Layer>& child : mDrawingChildren) {
1612 child->mDrawingParent = newParent;
1613 }
1614}
1615
chaviwf1961f72017-09-18 16:41:07 -07001616bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001617 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001618
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001619 // While layers are detached, we allow most operations
1620 // and simply halt performing the actual transaction. However
1621 // for reparent != null we would enter the mRemovedFromCurrentState
1622 // state, regardless of whether doTransaction was called, and
1623 // so we need to prevent the update here.
1624 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001625 return false;
1626 }
1627
chaviwf1961f72017-09-18 16:41:07 -07001628 sp<Layer> parent = getParent();
1629 if (parent != nullptr) {
1630 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001631 }
1632
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001633 if (newParentHandle != nullptr) {
1634 auto handle = static_cast<Handle*>(newParentHandle.get());
1635 sp<Layer> newParent = handle->owner.promote();
1636 if (newParent == nullptr) {
1637 ALOGE("Unable to promote Layer handle");
1638 return false;
1639 }
1640
1641 newParent->addChild(this);
1642 if (!newParent->isRemovedFromCurrentState()) {
1643 addToCurrentState();
1644 } else {
1645 onRemovedFromCurrentState();
1646 }
1647
1648 if (mLayerDetached) {
1649 mLayerDetached = false;
1650 callSetTransactionFlags = true;
1651 }
1652 } else {
1653 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001654 }
1655
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001656 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001657 setTransactionFlags(eTransactionNeeded);
1658 }
chaviw06178942017-07-27 10:25:59 -07001659 return true;
1660}
1661
Robert Carr9524cb32017-02-13 11:32:32 -08001662bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001663 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001664 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001665 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001666 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001667 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001668 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001669 }
Robert Carr7f619b22017-11-06 12:56:35 -08001670 }
Robert Carr9524cb32017-02-13 11:32:32 -08001671
1672 return true;
1673}
1674
chaviw5aedec92018-10-22 10:40:38 -07001675bool Layer::attachChildren() {
1676 bool changed = false;
1677 for (const sp<Layer>& child : mCurrentChildren) {
1678 sp<Client> parentClient = mClientRef.promote();
1679 sp<Client> client(child->mClientRef.promote());
1680 if (client != nullptr && parentClient != client) {
1681 if (child->mLayerDetached) {
1682 child->mLayerDetached = false;
1683 changed = true;
1684 }
1685 changed |= child->attachChildren();
1686 }
1687 }
1688
1689 return changed;
1690}
1691
Peiyong Lind3788632018-09-18 16:01:31 -07001692bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001693 static const mat4 identityMatrix = mat4();
1694
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001695 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001696 return false;
1697 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001698 ++mCurrentState.sequence;
1699 mCurrentState.colorTransform = matrix;
1700 mCurrentState.hasColorTransform = matrix != identityMatrix;
1701 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001702 setTransactionFlags(eTransactionNeeded);
1703 return true;
1704}
1705
chaviwf66724d2018-11-28 16:35:21 -08001706mat4 Layer::getColorTransform() const {
1707 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1708 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1709 colorTransform = parent->getColorTransform() * colorTransform;
1710 }
1711 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001712}
1713
1714bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001715 bool hasColorTransform = getDrawingState().hasColorTransform;
1716 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1717 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1718 }
1719 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001720}
1721
Chia-I Wu11481472018-05-04 10:43:19 -07001722bool Layer::isLegacyDataSpace() const {
1723 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001724 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001725 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001726}
1727
Robert Carr1f0a16a2016-10-24 16:27:39 -07001728void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001729 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001730}
1731
1732void Layer::clearSyncPoints() {
1733 for (const auto& child : mCurrentChildren) {
1734 child->clearSyncPoints();
1735 }
1736
1737 Mutex::Autolock lock(mLocalSyncPointMutex);
1738 for (auto& point : mLocalSyncPoints) {
1739 point->setFrameAvailable();
1740 }
1741 mLocalSyncPoints.clear();
1742}
1743
1744int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001745 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001746}
1747
Robert Carr29abff82017-12-04 13:51:20 -08001748bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1749 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001750 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001751 return state.zOrderRelativeOf != nullptr;
1752}
1753
David Sodman41fdfc92017-11-06 16:09:56 -08001754__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001755 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001756 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1757 "makeTraversalList received invalid stateSet");
1758 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1759 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001760 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001761
Robert Carr29abff82017-12-04 13:51:20 -08001762 if (state.zOrderRelatives.size() == 0) {
1763 *outSkipRelativeZUsers = true;
1764 return children;
1765 }
1766
chaviwfd462612018-05-31 16:11:27 -07001767 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001768 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001769 sp<Layer> strongRelative = weakRelative.promote();
1770 if (strongRelative != nullptr) {
1771 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001772 }
1773 }
1774
Dan Stoza412903f2017-04-27 13:42:17 -07001775 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001776 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001777 if (childState.zOrderRelativeOf != nullptr) {
1778 continue;
1779 }
Robert Carrdb66e622017-04-10 16:55:57 -07001780 traverse.add(child);
1781 }
1782
1783 return traverse;
1784}
1785
Robert Carr1f0a16a2016-10-24 16:27:39 -07001786/**
Robert Carrdb66e622017-04-10 16:55:57 -07001787 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001788 */
Dan Stoza412903f2017-04-27 13:42:17 -07001789void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001790 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1791 // produce a new list for traversing, including our relatives, and not including our children
1792 // who are relatives of another surface. In the case that there are no relative Z,
1793 // makeTraversalList returns our children directly to avoid significant overhead.
1794 // However in this case we need to take the responsibility for filtering children which
1795 // are relatives of another surface here.
1796 bool skipRelativeZUsers = false;
1797 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001798
Robert Carr1f0a16a2016-10-24 16:27:39 -07001799 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001800 for (; i < list.size(); i++) {
1801 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001802 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1803 continue;
1804 }
1805
Robert Carrdb66e622017-04-10 16:55:57 -07001806 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001807 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001808 }
Dan Stoza412903f2017-04-27 13:42:17 -07001809 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001810 }
Robert Carr29abff82017-12-04 13:51:20 -08001811
Dan Stoza412903f2017-04-27 13:42:17 -07001812 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001813 for (; i < list.size(); i++) {
1814 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001815
Robert Carr29abff82017-12-04 13:51:20 -08001816 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1817 continue;
1818 }
Dan Stoza412903f2017-04-27 13:42:17 -07001819 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001820 }
1821}
1822
1823/**
Robert Carrdb66e622017-04-10 16:55:57 -07001824 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001825 */
Dan Stoza412903f2017-04-27 13:42:17 -07001826void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1827 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001828 // See traverseInZOrder for documentation.
1829 bool skipRelativeZUsers = false;
1830 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001831
Robert Carr1f0a16a2016-10-24 16:27:39 -07001832 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001833 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001834 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001835
1836 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1837 continue;
1838 }
1839
Robert Carrdb66e622017-04-10 16:55:57 -07001840 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841 break;
1842 }
Dan Stoza412903f2017-04-27 13:42:17 -07001843 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844 }
Dan Stoza412903f2017-04-27 13:42:17 -07001845 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001846 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001847 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001848
1849 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1850 continue;
1851 }
1852
Dan Stoza412903f2017-04-27 13:42:17 -07001853 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001854 }
1855}
1856
chaviw4b129c22018-04-09 16:19:43 -07001857LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1858 const std::vector<Layer*>& layersInTree) {
1859 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1860 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001861 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1862 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001863 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001864
chaviwfd462612018-05-31 16:11:27 -07001865 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001866 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1867 sp<Layer> strongRelative = weakRelative.promote();
1868 // Only add relative layers that are also descendents of the top most parent of the tree.
1869 // If a relative layer is not a descendent, then it should be ignored.
1870 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1871 traverse.add(strongRelative);
1872 }
1873 }
1874
1875 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001876 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001877 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1878 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1879 // the child here since it won't be added later as a relative.
1880 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1881 childState.zOrderRelativeOf.promote().get())) {
1882 continue;
1883 }
1884 traverse.add(child);
1885 }
1886
1887 return traverse;
1888}
1889
1890void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1891 LayerVector::StateSet stateSet,
1892 const LayerVector::Visitor& visitor) {
1893 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001894
1895 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001896 for (; i < list.size(); i++) {
1897 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001898 if (relative->getZ() >= 0) {
1899 break;
1900 }
chaviw4b129c22018-04-09 16:19:43 -07001901 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001902 }
chaviw4b129c22018-04-09 16:19:43 -07001903
chaviwa76b2712017-09-20 12:02:26 -07001904 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001905 for (; i < list.size(); i++) {
1906 const auto& relative = list[i];
1907 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001908 }
1909}
1910
chaviw4b129c22018-04-09 16:19:43 -07001911std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1912 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1913 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1914
1915 std::vector<Layer*> layersInTree = {this};
1916 for (size_t i = 0; i < children.size(); i++) {
1917 const auto& child = children[i];
1918 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1919 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1920 }
1921
1922 return layersInTree;
1923}
1924
1925void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1926 const LayerVector::Visitor& visitor) {
1927 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1928 std::sort(layersInTree.begin(), layersInTree.end());
1929 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1930}
1931
Peiyong Linefefaac2018-08-17 12:27:51 -07001932ui::Transform Layer::getTransform() const {
1933 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001934 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001935 if (p != nullptr) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001936 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001937
1938 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1939 // it isFixedSize) then there may be additional scaling not accounted
1940 // for in the transform. We need to mirror this scaling in child surfaces
1941 // or we will break the contract where WM can treat child surfaces as
1942 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001943 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001944 int bufferWidth;
1945 int bufferHeight;
1946 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001947 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1948 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001949 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001950 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1951 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001952 }
Marissa Wall61c58622018-07-18 10:12:20 -07001953 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1954 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001955 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001956 extraParentScaling.set(sx, 0, 0, sy);
1957 t = t * extraParentScaling;
1958 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001959 }
Marissa Wall61c58622018-07-18 10:12:20 -07001960 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001961}
1962
chaviw13fdc492017-06-27 12:40:18 -07001963half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001964 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001965
chaviw13fdc492017-06-27 12:40:18 -07001966 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1967 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001968}
Robert Carr6452f122017-03-21 10:41:29 -07001969
chaviw13fdc492017-06-27 12:40:18 -07001970half4 Layer::getColor() const {
1971 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001972 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001973}
Robert Carr6452f122017-03-21 10:41:29 -07001974
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001975Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1976 const auto& p = mDrawingParent.promote();
1977 if (p != nullptr) {
1978 RoundedCornerState parentState = p->getRoundedCornerState();
1979 if (parentState.radius > 0) {
1980 ui::Transform t = getActiveTransform(getDrawingState());
1981 t = t.inverse();
1982 parentState.cropRect = t.transform(parentState.cropRect);
1983 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1984 // but a transform matrix can define horizontal and vertical scales.
1985 // Let's take the average between both of them and pass into the shader, practically we
1986 // never do this type of transformation on windows anyway.
1987 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1988 return parentState;
1989 }
1990 }
1991 const float radius = getDrawingState().cornerRadius;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001992 return radius > 0 ? RoundedCornerState(computeBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001993}
1994
Robert Carr1f0a16a2016-10-24 16:27:39 -07001995void Layer::commitChildList() {
1996 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1997 const auto& child = mCurrentChildren[i];
1998 child->commitChildList();
1999 }
2000 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002001 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002002}
2003
Robert Carr720e5062018-07-30 17:45:14 -07002004void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002005 mCurrentState.inputInfo = info;
2006 mCurrentState.modified = true;
2007 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002008 setTransactionFlags(eTransactionNeeded);
2009}
2010
chaviw1d044282017-09-27 12:19:28 -07002011void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2012 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2013 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002014 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002015
Peiyong Linefefaac2018-08-17 12:27:51 -07002016 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002017 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002018
2019 layerInfo->set_id(sequence);
2020 layerInfo->set_name(getName().c_str());
2021 layerInfo->set_type(String8(getTypeId()));
2022
2023 for (const auto& child : children) {
2024 layerInfo->add_children(child->sequence);
2025 }
2026
2027 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2028 sp<Layer> strongRelative = weakRelative.promote();
2029 if (strongRelative != nullptr) {
2030 layerInfo->add_relatives(strongRelative->sequence);
2031 }
2032 }
2033
Marissa Wallf58c14b2018-07-24 10:50:43 -07002034 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002035 layerInfo->mutable_transparent_region());
2036 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2037 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2038
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002039 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002040 layerInfo->set_z(state.z);
2041
2042 PositionProto* position = layerInfo->mutable_position();
2043 position->set_x(transform.tx());
2044 position->set_y(transform.ty());
2045
2046 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2047 requestedPosition->set_x(requestedTransform.tx());
2048 requestedPosition->set_y(requestedTransform.ty());
2049
2050 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002051 size->set_w(state.active_legacy.w);
2052 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002053
Marissa Wallf58c14b2018-07-24 10:50:43 -07002054 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002055 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002056
2057 layerInfo->set_is_opaque(isOpaque(state));
2058 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002059
2060 // XXX (b/79210409) mCurrentDataSpace is not protected
2061 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2062
chaviw1d044282017-09-27 12:19:28 -07002063 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2064 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2065 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2066 layerInfo->set_flags(state.flags);
2067
2068 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2069 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2070
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002071 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002072 if (parent != nullptr) {
2073 layerInfo->set_parent(parent->sequence);
2074 }
2075
2076 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2077 if (zOrderRelativeOf != nullptr) {
2078 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2079 }
2080
Chia-I Wu01591c92018-05-22 12:03:00 -07002081 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002082 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002083 if (buffer != nullptr) {
2084 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002085 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002086 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002087 }
2088
2089 layerInfo->set_queued_frames(getQueuedFrameCount());
2090 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002091 layerInfo->set_window_type(state.type);
2092 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002093 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002094 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002095
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002096 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002097 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002098 if (barrierLayer != nullptr) {
2099 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2100 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002101 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002102 }
2103 }
chaviw1d044282017-09-27 12:19:28 -07002104}
2105
Dominik Laskowski075d3172018-05-24 15:50:06 -07002106void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002107 if (!hasHwcLayer(displayId)) {
2108 return;
2109 }
2110
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002111 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2112
Dominik Laskowski7e045462018-05-30 13:02:02 -07002113 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002114
2115 const Rect& frame = hwcInfo.displayFrame;
2116 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2117
2118 const FloatRect& crop = hwcInfo.sourceCrop;
2119 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2120
2121 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2122 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002123
2124 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2125 layerInfo->set_hwc_composition_type(compositionType);
2126
2127 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2128 static_cast<BufferLayer*>(this)->isProtected()) {
2129 layerInfo->set_is_protected(true);
2130 } else {
2131 layerInfo->set_is_protected(false);
2132 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002133}
2134
Robert Carr2e102c92018-10-23 12:11:15 -07002135bool Layer::isRemovedFromCurrentState() const {
2136 return mRemovedFromCurrentState;
2137}
2138
Arthur Hungd20b2702019-01-14 18:16:16 +08002139InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002140 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002141
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002142 ui::Transform t = getTransform();
2143 const float xScale = t.sx();
2144 const float yScale = t.sy();
2145 if (xScale != 1.0f || yScale != 1.0f) {
2146 info.windowXScale *= 1.0f / xScale;
2147 info.windowYScale *= 1.0f / yScale;
2148 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002149 }
Robert Carre07e1032018-11-26 12:55:53 -08002150
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002151 // Transform layer size to screen space and inset it by surface insets.
Arthur Hungd20b2702019-01-14 18:16:16 +08002152 Rect layerBounds = getBufferSize(getDrawingState());
2153 if (!layerBounds.isValid()) {
2154 layerBounds = getCroppedBufferSize(getDrawingState());
2155 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002156 layerBounds = t.transform(layerBounds);
2157 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2158
Arthur Hungd20b2702019-01-14 18:16:16 +08002159 // Input coordinate should match the layer bounds.
2160 info.frameLeft = layerBounds.left;
2161 info.frameTop = layerBounds.top;
2162 info.frameRight = layerBounds.right;
2163 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002164
2165 // Position the touchable region relative to frame screen location and restrict it to frame
2166 // bounds.
2167 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
Robert Carr720e5062018-07-30 17:45:14 -07002168 info.visible = isVisible();
2169 return info;
2170}
2171
2172bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002173 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002174}
2175
Mathias Agopian13127d82013-03-05 17:47:11 -08002176// ---------------------------------------------------------------------------
2177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002178}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002179
2180#if defined(__gl_h_)
2181#error "don't include gl/gl.h in this file"
2182#endif
2183
2184#if defined(__gl2_h_)
2185#error "don't include gl2/gl2.h in this file"
2186#endif