blob: 7b8ad7129a0456401151a3ba640ca0ac83e0fb72 [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>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080033#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <gui/BufferItem.h>
39#include <gui/LayerDebugInfo.h>
40#include <gui/Surface.h>
41#include <renderengine/RenderEngine.h>
42#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Yiwei Zhang60d1a192018-03-07 14:52:28 -080051#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080052#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020053#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080055#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080057#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070058#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070059#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080061#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070062
David Sodman41fdfc92017-11-06 16:09:56 -080063#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065namespace android {
66
Yiwei Zhang5434a782018-12-05 18:06:32 -080067using base::StringAppendF;
68
Lloyd Piquef1c675b2018-09-12 20:45:39 -070069std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080070
Lloyd Pique42ab75e2018-09-12 20:46:03 -070071Layer::Layer(const LayerCreationArgs& args)
72 : mFlinger(args.flinger),
73 mName(args.name),
74 mClientRef(args.client),
75 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070077
78 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070079 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
80 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
81 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082
Dan Stozaf7ba41a2017-05-10 15:11:11 -070083 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070084
Lloyd Pique0449b0f2018-12-20 16:23:45 -080085 mCurrentState.active_legacy.w = args.w;
86 mCurrentState.active_legacy.h = args.h;
87 mCurrentState.flags = layerFlags;
88 mCurrentState.active_legacy.transform.set(0, 0);
89 mCurrentState.crop_legacy.makeInvalid();
90 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
91 mCurrentState.z = 0;
92 mCurrentState.color.a = 1.0f;
93 mCurrentState.layerStack = 0;
94 mCurrentState.sequence = 0;
95 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080096 mCurrentState.active.w = UINT32_MAX;
97 mCurrentState.active.h = UINT32_MAX;
98 mCurrentState.active.transform.set(0, 0);
99 mCurrentState.transform = 0;
100 mCurrentState.transformToDisplayInverse = false;
101 mCurrentState.crop.makeInvalid();
102 mCurrentState.acquireFence = new Fence(-1);
103 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
104 mCurrentState.hdrMetadata.validTypes = 0;
105 mCurrentState.surfaceDamageRegion.clear();
106 mCurrentState.cornerRadius = 0.0f;
107 mCurrentState.api = -1;
108 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109
110 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700112
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800113 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700114 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800115 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200116 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700117
118 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700119}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700120
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800122 sp<Client> c(mClientRef.promote());
123 if (c != 0) {
124 c->detachLayer(this);
125 }
126
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000127 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700128
chaviw61626f22018-11-15 16:26:27 -0800129 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700130
131 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700132}
133
Mathias Agopian13127d82013-03-05 17:47:11 -0800134// ---------------------------------------------------------------------------
135// callbacks
136// ---------------------------------------------------------------------------
137
David Sodmaneb085e02017-10-05 18:49:04 -0700138/*
139 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
140 * Layer. So, the implementation is done in BufferLayer. When called on a
141 * ColorLayer object, it's essentially a NOP.
142 */
David Sodmaneb085e02017-10-05 18:49:04 -0700143void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800144
Chia-I Wuc6657022017-08-15 11:18:17 -0700145void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700146 mRemovedFromCurrentState = true;
147
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800148 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
149 if (mCurrentState.zOrderRelativeOf != nullptr) {
150 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
151 if (strongRelative != nullptr) {
152 strongRelative->removeZOrderRelative(this);
153 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700154 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800155 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carr5edb1ad2017-04-25 10:54:24 -0700156 }
Rob Carr4bba3702018-10-08 21:53:30 +0000157
Robert Carr2e102c92018-10-23 12:11:15 -0700158 // Since we are no longer reachable from CurrentState SurfaceFlinger
159 // will no longer invoke doTransaction for us, and so we will
160 // never finish applying transactions. We signal the sync point
161 // now so that another layer will not become indefinitely
162 // blocked.
163 for (auto& point: mRemoteSyncPoints) {
164 point->setTransactionApplied();
165 }
166 mRemoteSyncPoints.clear();
167
168 {
169 Mutex::Autolock syncLock(mLocalSyncPointMutex);
170 for (auto& point : mLocalSyncPoints) {
171 point->setFrameAvailable();
172 }
173 mLocalSyncPoints.clear();
174 }
175
Chia-I Wuc6657022017-08-15 11:18:17 -0700176 for (const auto& child : mCurrentChildren) {
177 child->onRemovedFromCurrentState();
178 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800179
180 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700181}
Chia-I Wu38512252017-05-17 14:36:16 -0700182
chaviw61626f22018-11-15 16:26:27 -0800183void Layer::addToCurrentState() {
184 mRemovedFromCurrentState = false;
185
186 for (const auto& child : mCurrentChildren) {
187 child->addToCurrentState();
188 }
189}
190
Mathias Agopian13127d82013-03-05 17:47:11 -0800191// ---------------------------------------------------------------------------
192// set-up
193// ---------------------------------------------------------------------------
194
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700195const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800196 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
198
chaviw13fdc492017-06-27 12:40:18 -0700199bool Layer::getPremultipledAlpha() const {
200 return mPremultipliedAlpha;
201}
202
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700203sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800204 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700205 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800206}
207
208// ---------------------------------------------------------------------------
209// h/w composer set-up
210// ---------------------------------------------------------------------------
211
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800212bool Layer::createHwcLayer(HWComposer* hwc, const sp<DisplayDevice>& displayDevice) {
213 LOG_ALWAYS_FATAL_IF(!displayDevice->getId());
214 auto displayId = *displayDevice->getId();
215 auto outputLayer = findOutputLayerForDisplay(displayDevice);
216 LOG_ALWAYS_FATAL_IF(!outputLayer);
217
218 LOG_ALWAYS_FATAL_IF(outputLayer->getState().hwc.has_value(),
219 "Already have a layer for display %s",
220 displayDevice->getDisplayName().c_str());
221
David Sodmanb8aaea12017-12-14 15:54:51 -0800222 auto layer = std::shared_ptr<HWC2::Layer>(
223 hwc->createLayer(displayId),
224 [hwc, displayId](HWC2::Layer* layer) {
225 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700226 if (!layer) {
227 return false;
228 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800229 auto& state = outputLayer->editState();
230 state.hwc.emplace(layer);
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 return true;
232}
233
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800234bool Layer::destroyHwcLayer(const sp<DisplayDevice>& displayDevice) {
235 auto outputLayer = findOutputLayerForDisplay(displayDevice);
236 if (outputLayer == nullptr) {
Chia-I Wu83806892017-11-16 10:50:20 -0800237 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700238 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800239 auto& state = outputLayer->editState();
240 bool result = state.hwc.has_value();
241 state.hwc.reset();
242 return result;
Steven Thomasb02664d2017-07-26 18:48:28 -0700243}
244
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800245bool Layer::destroyHwcLayersForAllDisplays() {
246 bool destroyedAnyLayers = false;
247
248 for (const auto& [token, displayDevice] : mFlinger->mDisplays) {
249 if (destroyHwcLayer(displayDevice)) {
250 destroyedAnyLayers = true;
251 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700252 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800253
254 return destroyedAnyLayers;
chaviw61626f22018-11-15 16:26:27 -0800255}
256
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800257bool Layer::destroyAllHwcLayersPlusChildren() {
258 bool result = destroyHwcLayersForAllDisplays();
Robert Carr2e102c92018-10-23 12:11:15 -0700259
260 for (const sp<Layer>& child : mDrawingChildren) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800261 result |= child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700262 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800263
264 return result;
265}
266
267bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
268 auto outputLayer = findOutputLayerForDisplay(displayDevice);
269 LOG_FATAL_IF(!outputLayer);
270 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
271}
272
273HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
274 auto outputLayer = findOutputLayerForDisplay(displayDevice);
275 if (!outputLayer || !outputLayer->getState().hwc) {
276 return nullptr;
277 }
278 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700279}
Steven Thomasb02664d2017-07-26 18:48:28 -0700280
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800281Rect Layer::getContentCrop() const {
282 // this is the crop rectangle that applies to the buffer
283 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700284 Rect crop;
285 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800286 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700287 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800288 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800289 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800290 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700291 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800292 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700293 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700294 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700295 return crop;
296}
297
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700298static Rect reduce(const Rect& win, const Region& exclude) {
299 if (CC_LIKELY(exclude.isEmpty())) {
300 return win;
301 }
302 if (exclude.isRect()) {
303 return win.reduce(exclude.getBounds());
304 }
305 return Region(win).subtract(exclude).getBounds();
306}
307
Dan Stoza80d61162017-12-20 15:57:52 -0800308static FloatRect reduce(const FloatRect& win, const Region& exclude) {
309 if (CC_LIKELY(exclude.isEmpty())) {
310 return win;
311 }
312 // Convert through Rect (by rounding) for lack of FloatRegion
313 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
314}
315
Vishnu Nair4351ad52019-02-11 14:13:02 -0800316Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800317 if (!reduceTransparentRegion) {
318 return Rect{mScreenBounds};
319 }
320
321 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800322 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800323 // Transform to screen space.
324 bounds = t.transform(bounds);
325 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700326}
327
Vishnu Nair4351ad52019-02-11 14:13:02 -0800328FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000329 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800330 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700331}
332
Vishnu Nairf0c28512019-02-08 12:40:28 -0800333FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
334 // Subtract the transparent region and snap to the bounds.
335 return reduce(mBounds, activeTransparentRegion);
336}
337
Vishnu Nair4351ad52019-02-11 14:13:02 -0800338ui::Transform Layer::getTransformWithScale() const {
339 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
340 // it isFixedSize) then there may be additional scaling not accounted
341 // for in the transform. We need to mirror this scaling to child surfaces
342 // or we will break the contract where WM can treat child surfaces as
343 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800344 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800345 return mEffectiveTransform;
346 }
347
348 int bufferWidth;
349 int bufferHeight;
350 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800351 bufferWidth = mActiveBuffer->getWidth();
352 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800353 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800354 bufferHeight = mActiveBuffer->getWidth();
355 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800356 }
357 float sx = getActiveWidth(getDrawingState()) / static_cast<float>(bufferWidth);
358 float sy = getActiveHeight(getDrawingState()) / static_cast<float>(bufferHeight);
359 ui::Transform extraParentScaling;
360 extraParentScaling.set(sx, 0, 0, sy);
361 return mEffectiveTransform * extraParentScaling;
362}
363
364void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
365 const State& s(getDrawingState());
366
367 // Calculate effective layer transform
368 mEffectiveTransform = parentTransform * getActiveTransform(s);
369
370 // Transform parent bounds to layer space
371 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
372
373 // Calculate display frame
374 mSourceBounds = computeSourceBounds(parentBounds);
375
376 // Calculate bounds by croping diplay frame with layer crop and parent bounds
377 FloatRect bounds = mSourceBounds;
378 const Rect layerCrop = getCrop(s);
379 if (!layerCrop.isEmpty()) {
380 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
381 }
382 bounds = bounds.intersect(parentBounds);
383
384 mBounds = bounds;
385 mScreenBounds = mEffectiveTransform.transform(mBounds);
386 for (const sp<Layer>& child : mDrawingChildren) {
387 child->computeBounds(mBounds, getTransformWithScale());
388 }
389}
390
Robert Carrb5d3d262016-03-25 15:08:13 -0700391
Robert Carr1f0a16a2016-10-24 16:27:39 -0700392
Vishnu Nair60356342018-11-13 13:00:45 -0800393Rect Layer::getCroppedBufferSize(const State& s) const {
394 Rect size = getBufferSize(s);
395 Rect crop = getCrop(s);
396 if (!crop.isEmpty() && size.isValid()) {
397 size.intersect(crop, &size);
398 } else if (!crop.isEmpty()) {
399 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700400 }
Vishnu Nair60356342018-11-13 13:00:45 -0800401 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800402}
403
Alec Mouri1f3bd182019-01-24 15:20:18 +0000404Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700405 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800406 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700407 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800408
409 // apply the projection's clipping to the window crop in
410 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700411 // if there are no window scaling involved, this operation will map to full
412 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700413
Vishnu Nair4351ad52019-02-11 14:13:02 -0800414 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000415 ui::Transform t = getTransform();
416 // Transform to screen space.
417 activeCropFloat = t.transform(activeCropFloat);
418 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
419 // Back to layer space to work with the content crop.
420 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800421 // This needs to be here as transform.transform(Rect) computes the
422 // transformed rect and then takes the bounding box of the result before
423 // returning. This means
424 // transform.inverse().transform(transform.transform(Rect)) != Rect
425 // in which case we need to make sure the final rect is clipped to the
426 // display bounds.
427 Rect activeCrop{activeCropFloat};
428 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 activeCrop.clear();
430 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700431 return activeCrop;
432}
433
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700434void Layer::setupRoundedCornersCropCoordinates(Rect win,
435 const FloatRect& roundedCornersCrop) const {
436 // Translate win by the rounded corners rect coordinates, to have all values in
437 // layer coordinate space.
438 win.left -= roundedCornersCrop.left;
439 win.right -= roundedCornersCrop.left;
440 win.top -= roundedCornersCrop.top;
441 win.bottom -= roundedCornersCrop.top;
442
Lloyd Pique0b785d82018-12-04 17:25:27 -0800443 renderengine::Mesh::VertexArray<vec2> cropCoords(
444 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700445 cropCoords[0] = vec2(win.left, win.top);
446 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
447 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
448 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700449}
450
Alec Mouri1f3bd182019-01-24 15:20:18 +0000451FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700452 // the content crop is the area of the content that gets scaled to the
453 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800454 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700455
456 // In addition there is a WM-specified crop we pull from our drawing state.
457 const State& s(getDrawingState());
458
Alec Mouri1f3bd182019-01-24 15:20:18 +0000459 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800460 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700461
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000462 // Transform the window crop to match the buffer coordinate system,
463 // which means using the inverse of the current transform set on the
464 // SurfaceFlingerConsumer.
465 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800466 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700468 * the code below applies the primary display's inverse transform to the
469 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470 */
David Sodman41fdfc92017-11-06 16:09:56 -0800471 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000472 // calculate the inverse transform
473 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800474 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800475 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000476 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700477 invTransform = (ui::Transform(invTransformOrient) *
478 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800479 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000480
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800481 int winWidth = bufferSize.getWidth();
482 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000483 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
484 // If the activeCrop has been rotate the ends are rotated but not
485 // the space itself so when transforming ends back we can't rely on
486 // a modification of the axes of rotation. To account for this we
487 // need to reorient the inverse rotation in terms of the current
488 // axes of rotation.
489 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
490 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
491 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800492 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000493 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800494 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000495 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800496 const Rect winCrop =
497 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000498
499 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800500 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000501 float yScale = crop.getHeight() / float(winHeight);
502
David Sodman41fdfc92017-11-06 16:09:56 -0800503 float insetL = winCrop.left * xScale;
504 float insetT = winCrop.top * yScale;
505 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000506 float insetB = (winHeight - winCrop.bottom) * yScale;
507
David Sodman41fdfc92017-11-06 16:09:56 -0800508 crop.left += insetL;
509 crop.top += insetT;
510 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000511 crop.bottom -= insetB;
512
Mathias Agopian13127d82013-03-05 17:47:11 -0800513 return crop;
514}
515
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700516void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800517 const auto outputLayer = findOutputLayerForDisplay(display);
518 LOG_FATAL_IF(!outputLayer);
519 LOG_FATAL_IF(!outputLayer->getState().hwc);
520 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700521
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800522 if (!hasHwcLayer(display)) {
523 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
524 display->getDebugName().c_str());
525 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800526 }
527
Lloyd Pique0b785d82018-12-04 17:25:27 -0800528 LOG_FATAL_IF(!getCompositionLayer());
529 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800530 auto& compositionState = outputLayer->editState();
531
532 // enable this layer
533 compositionState.forceClientComposition = false;
534
535 if (isSecure() && !display->isSecure()) {
536 compositionState.forceClientComposition = true;
537 }
David Sodman15094112018-10-11 09:39:37 -0700538
Mathias Agopian13127d82013-03-05 17:47:11 -0800539 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700540 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800541 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500542 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800543 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800544 blendMode =
545 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800546 }
David Sodman15094112018-10-11 09:39:37 -0700547 auto error = hwcLayer->setBlendMode(blendMode);
548 ALOGE_IF(error != HWC2::Error::None,
549 "[%s] Failed to set blend mode %s:"
550 " %s (%d)",
551 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
552 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800553 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800554
555 // apply the layer's transform, followed by the display's global transform
556 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700557 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800558 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700559 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800560 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700561 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700562 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000563 activeCrop.clear();
564 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700565 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800566 // This needs to be here as transform.transform(Rect) computes the
567 // transformed rect and then takes the bounding box of the result before
568 // returning. This means
569 // transform.inverse().transform(transform.transform(Rect)) != Rect
570 // in which case we need to make sure the final rect is clipped to the
571 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800572 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000573 activeCrop.clear();
574 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700575 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800576 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
577 activeTransparentRegion.orSelf(
578 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800579 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
580 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800581 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700582 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700583
Vishnu Nair4351ad52019-02-11 14:13:02 -0800584 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800585 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800586 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700587 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000588 frame.clear();
589 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700590 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700592 error = hwcLayer->setDisplayFrame(transformedFrame);
593 if (error != HWC2::Error::None) {
594 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
595 transformedFrame.left, transformedFrame.top, transformedFrame.right,
596 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
597 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800598 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700599 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800600
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700601 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700602 error = hwcLayer->setSourceCrop(sourceCrop);
603 if (error != HWC2::Error::None) {
604 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
605 "%s (%d)",
606 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
607 to_string(error).c_str(), static_cast<int32_t>(error));
608 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800609 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700610 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800611
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800612 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700613 error = hwcLayer->setPlaneAlpha(alpha);
614 ALOGE_IF(error != HWC2::Error::None,
615 "[%s] Failed to set plane alpha %.3f: "
616 "%s (%d)",
617 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800618 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800619
David Sodman15094112018-10-11 09:39:37 -0700620 error = hwcLayer->setZOrder(z);
621 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
622 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800623 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500624
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800625 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
626 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700627 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400628 if (parent.get()) {
629 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800630 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
631 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
632 if (parentType >= 0 || parentAppId >= 0) {
633 type = parentType;
634 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700635 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400636 }
637
David Sodman15094112018-10-11 09:39:37 -0700638 error = hwcLayer->setInfo(type, appId);
639 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
640 static_cast<int32_t>(error));
641
Lloyd Pique0b785d82018-12-04 17:25:27 -0800642 commonCompositionState.type = type;
643 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700644
Mathias Agopian29a367b2011-07-12 14:51:45 -0700645 /*
646 * Transformations are applied in this order:
647 * 1) buffer orientation/flip/mirror
648 * 2) state transformation (window manager)
649 * 3) layer orientation (screen orientation)
650 * (NOTE: the matrices are multiplied in reverse order)
651 */
652
Peiyong Linefefaac2018-08-17 12:27:51 -0700653 const ui::Transform bufferOrientation(mCurrentTransform);
654 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700655
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800656 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700657 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700658 * the code below applies the primary display's inverse transform to the
659 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700660 */
David Sodman41fdfc92017-11-06 16:09:56 -0800661 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700662 // calculate the inverse transform
663 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800664 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700665 }
Robert Carrcae605c2017-03-29 12:10:31 -0700666
667 /*
668 * Here we cancel out the orientation component of the WM transform.
669 * The scaling and translate components are already included in our bounds
670 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000671 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700672 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700673 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700674 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700675
676 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800677 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700678 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800680 compositionState.forceClientComposition = true;
681 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682 } else {
683 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700684 auto error = hwcLayer->setTransform(transform);
685 ALOGE_IF(error != HWC2::Error::None,
686 "[%s] Failed to set transform %s: "
687 "%s (%d)",
688 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
689 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800690 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800691 }
692}
693
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800694void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
695 const auto outputLayer = findOutputLayerForDisplay(display);
696 LOG_FATAL_IF(!outputLayer);
697 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698}
Dan Stozaee44edd2015-03-23 15:50:23 -0700699
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800700bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
701 const auto outputLayer = findOutputLayerForDisplay(display);
702 LOG_FATAL_IF(!outputLayer);
703 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800704}
705
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700706void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800707 const auto outputLayer = findOutputLayerForDisplay(display);
708 LOG_FATAL_IF(!outputLayer);
709
710 if (!outputLayer->getState().hwc ||
711 (*outputLayer->getState().hwc).hwcCompositionType !=
712 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800713 return;
714 }
715
716 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800717 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718
719 // Apply the layer's transform, followed by the display's global transform
720 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800721 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700723 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800724 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700725 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700726 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800727 auto position = displayTransform.transform(frame);
728
Dominik Laskowski7e045462018-05-30 13:02:02 -0700729 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800730 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800731 ALOGE_IF(error != HWC2::Error::None,
732 "[%s] Failed to set cursor position "
733 "to (%d, %d): %s (%d)",
734 mName.string(), position.left, position.top, to_string(error).c_str(),
735 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800736}
Riley Andrews03414a12014-07-01 14:22:59 -0700737
Mathias Agopian13127d82013-03-05 17:47:11 -0800738// ---------------------------------------------------------------------------
739// drawing...
740// ---------------------------------------------------------------------------
741
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000742bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
743 Region& clearRegion, renderengine::LayerSettings& layer) {
744 return prepareClientLayer(renderArea, clip, false, clearRegion, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800745}
746
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000747bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
748 Region& clearRegion, renderengine::LayerSettings& layer) {
749 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
750 clearRegion, layer);
751}
752
753bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
754 bool useIdentityTransform, Region& /*clearRegion*/,
755 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800756 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000757 half alpha = getAlpha();
758 layer.geometry.boundaries = bounds;
759 if (useIdentityTransform) {
760 layer.geometry.positionTransform = mat4();
761 } else {
762 const ui::Transform transform = getTransform();
763 mat4 m;
764 m[0][0] = transform[0][0];
765 m[0][1] = transform[0][1];
766 m[0][3] = transform[0][2];
767 m[1][0] = transform[1][0];
768 m[1][1] = transform[1][1];
769 m[1][3] = transform[1][2];
770 m[3][0] = transform[2][0];
771 m[3][1] = transform[2][1];
772 m[3][3] = transform[2][2];
773 layer.geometry.positionTransform = m;
774 }
775
776 if (hasColorTransform()) {
777 layer.colorTransform = getColorTransform();
778 }
779
780 const auto roundedCornerState = getRoundedCornerState();
781 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
782 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
783
784 layer.alpha = alpha;
785 layer.sourceDataspace = mCurrentDataSpace;
786 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800787}
788
David Sodman41fdfc92017-11-06 16:09:56 -0800789void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
790 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800791 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800792 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700793 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800794 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800795}
796
David Sodmanc1498e62018-09-12 14:36:26 -0700797void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
798 clearWithOpenGL(renderArea, 0, 0, 0, 0);
799}
800
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800801void Layer::setCompositionType(const sp<const DisplayDevice>& display,
802 Hwc2::IComposerClient::Composition type) {
803 const auto outputLayer = findOutputLayerForDisplay(display);
804 LOG_FATAL_IF(!outputLayer);
805 LOG_FATAL_IF(!outputLayer->getState().hwc);
806 auto& compositionState = outputLayer->editState();
807
808 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
809 toString(type).c_str(), 1);
810 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700811 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800812 (*compositionState.hwc).hwcCompositionType = type;
813
814 auto error = (*compositionState.hwc)
815 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
816 ALOGE_IF(error != HWC2::Error::None,
817 "[%s] Failed to set "
818 "composition type %s: %s (%d)",
819 mName.string(), toString(type).c_str(), to_string(error).c_str(),
820 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800821 }
822}
823
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800824Hwc2::IComposerClient::Composition Layer::getCompositionType(
825 const sp<const DisplayDevice>& display) const {
826 const auto outputLayer = findOutputLayerForDisplay(display);
827 LOG_FATAL_IF(!outputLayer);
828 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
829 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800830}
831
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800832bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
833 const auto outputLayer = findOutputLayerForDisplay(display);
834 LOG_FATAL_IF(!outputLayer);
835 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800836}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800837
Dan Stozacac35382016-01-27 12:21:06 -0800838bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
839 if (point->getFrameNumber() <= mCurrentFrameNumber) {
840 // Don't bother with a SyncPoint, since we've already latched the
841 // relevant frame
842 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700843 }
Robert Carr2e102c92018-10-23 12:11:15 -0700844 if (isRemovedFromCurrentState()) {
845 return false;
846 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700847
Dan Stozacac35382016-01-27 12:21:06 -0800848 Mutex::Autolock lock(mLocalSyncPointMutex);
849 mLocalSyncPoints.push_back(point);
850 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700851}
852
Mathias Agopian13127d82013-03-05 17:47:11 -0800853// ----------------------------------------------------------------------------
854// local state
855// ----------------------------------------------------------------------------
856
Peiyong Lin833074a2018-08-28 11:53:54 -0700857void Layer::computeGeometry(const RenderArea& renderArea,
858 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700859 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700860 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800861 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700862
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000863 vec2 lt = vec2(win.left, win.top);
864 vec2 lb = vec2(win.left, win.bottom);
865 vec2 rb = vec2(win.right, win.bottom);
866 vec2 rt = vec2(win.right, win.top);
867
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800868 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000869 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700870 lt = layerTransform.transform(lt);
871 lb = layerTransform.transform(lb);
872 rb = layerTransform.transform(rb);
873 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000874 }
875
Peiyong Lin833074a2018-08-28 11:53:54 -0700876 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700877 position[0] = renderAreaTransform.transform(lt);
878 position[1] = renderAreaTransform.transform(lb);
879 position[2] = renderAreaTransform.transform(rb);
880 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800881}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800882
David Sodman41fdfc92017-11-06 16:09:56 -0800883bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800884 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700885 return (s.flags & layer_state_t::eLayerSecure);
886}
887
Mathias Agopian13127d82013-03-05 17:47:11 -0800888void Layer::setVisibleRegion(const Region& visibleRegion) {
889 // always called from main thread
890 this->visibleRegion = visibleRegion;
891}
892
893void Layer::setCoveredRegion(const Region& coveredRegion) {
894 // always called from main thread
895 this->coveredRegion = coveredRegion;
896}
897
David Sodman41fdfc92017-11-06 16:09:56 -0800898void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 // always called from main thread
900 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
901}
902
Robert Carre5f4f692018-01-12 13:12:28 -0800903void Layer::clearVisibilityRegions() {
904 visibleRegion.clear();
905 visibleNonTransparentRegion.clear();
906 coveredRegion.clear();
907}
908
Mathias Agopian13127d82013-03-05 17:47:11 -0800909// ----------------------------------------------------------------------------
910// transaction
911// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800912
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800913void Layer::pushPendingState() {
914 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 return;
916 }
917
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 // If this transaction is waiting on the receipt of a frame, generate a sync
919 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700920 // We don't allow installing sync points after we are removed from the current state
921 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800922 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
923 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800924 if (barrierLayer == nullptr) {
925 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 // If we can't promote the layer we are intended to wait on,
927 // then it is expired or otherwise invalid. Allow this transaction
928 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800929 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800930 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800932 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800933 mRemoteSyncPoints.push_back(std::move(syncPoint));
934 } else {
935 // We already missed the frame we're supposed to synchronize
936 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800937 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800938 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 }
940
Dan Stoza7dde5992015-05-22 09:51:44 -0700941 // Wake us up to check if the frame has been received
942 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700943 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800945 mPendingStates.push_back(mCurrentState);
946 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700947}
948
Pablo Ceballos05289c22016-04-14 15:49:55 -0700949void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800950 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700951
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800952 mPendingStates.removeAt(0);
953 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700954}
955
Pablo Ceballos05289c22016-04-14 15:49:55 -0700956bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700957 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800958 while (!mPendingStates.empty()) {
959 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 if (mRemoteSyncPoints.empty()) {
961 // If we don't have a sync point for this, apply it anyway. It
962 // will be visually wrong, but it should keep us from getting
963 // into too much trouble.
964 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700965 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 stateUpdateAvailable = true;
967 continue;
968 }
969
Marissa Wallf58c14b2018-07-24 10:50:43 -0700970 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800971 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800972 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800973
974 // Signal our end of the sync point and then dispose of it
975 mRemoteSyncPoints.front()->setTransactionApplied();
976 mRemoteSyncPoints.pop_front();
977 continue;
978 }
979
Dan Stoza7dde5992015-05-22 09:51:44 -0700980 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
981 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700982 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700983 stateUpdateAvailable = true;
984
985 // Signal our end of the sync point and then dispose of it
986 mRemoteSyncPoints.front()->setTransactionApplied();
987 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800988 } else {
989 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700990 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700991 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700992 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700993 stateUpdateAvailable = true;
994 }
995 }
996
997 // If we still have pending updates, wake SurfaceFlinger back up and point
998 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800999 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001000 setTransactionFlags(eTransactionNeeded);
1001 mFlinger->setTransactionFlags(eTraversalNeeded);
1002 }
1003
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001004 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001005 return stateUpdateAvailable;
1006}
1007
Marissa Wall61c58622018-07-18 10:12:20 -07001008uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +00001009 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010
Marissa Wall61c58622018-07-18 10:12:20 -07001011 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
1012 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001013
David Sodmaneb085e02017-10-05 18:49:04 -07001014 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001015 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001016 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001017 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1018 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1019 " requested={ wh={%4u,%4u} }}\n"
1020 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1021 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -07001022 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -07001023 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
1024 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
1025 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
1026 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
1027 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -07001028 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1029 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1030 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001032
Robert Carre392b552017-09-19 12:16:05 -07001033 // Don't let Layer::doTransaction update the drawing state
1034 // if we have a pending resize, unless we are in fixed-size mode.
1035 // the drawing state will be updated only once we receive a buffer
1036 // with the correct size.
1037 //
1038 // In particular, we want to make sure the clip (which is part
1039 // of the geometry state) is latched together with the size but is
1040 // latched immediately when no resizing is involved.
1041 //
1042 // If a sideband stream is attached, however, we want to skip this
1043 // optimization so that transactions aren't missed when a buffer
1044 // never arrives
1045 //
1046 // In the case that we don't have a buffer we ignore other factors
1047 // and avoid entering the resizePending state. At a high level the
1048 // resizePending state is to avoid applying the state of the new buffer
1049 // to the old buffer. However in the state where we don't have an old buffer
1050 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001051 const bool resizePending =
1052 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1053 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -08001054 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001055 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -08001056 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001057 flags |= eDontUpdateGeometryState;
1058 }
1059 }
1060
Robert Carr7bf247e2017-05-18 14:02:49 -07001061 // Here we apply various requested geometry states, depending on our
1062 // latching configuration. See Layer.h for a detailed discussion of
1063 // how geometry latching is controlled.
1064 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001065 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001066
1067 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1068 // mode, which causes attributes which normally latch regardless of scaling mode,
1069 // to be delayed. We copy the requested state to the active state making sure
1070 // to respect these rules (again see Layer.h for a detailed discussion).
1071 //
1072 // There is an awkward asymmetry in the handling of the crop states in the position
1073 // states, as can be seen below. Largely this arises from position and transform
1074 // being stored in the same data structure while having different latching rules.
1075 // b/38182305
1076 //
Marissa Wall61c58622018-07-18 10:12:20 -07001077 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001078 // applyPendingStates in the presence of deferred transactions.
1079 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001080 float tx = stateToCommit->active_legacy.transform.tx();
1081 float ty = stateToCommit->active_legacy.transform.ty();
1082 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1083 stateToCommit->active_legacy.transform.set(tx, ty);
1084 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001085 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001086 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001087 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001088 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 }
1090
Marissa Wall61c58622018-07-18 10:12:20 -07001091 return flags;
1092}
1093
1094uint32_t Layer::doTransaction(uint32_t flags) {
1095 ATRACE_CALL();
1096
chaviw5aedec92018-10-22 10:40:38 -07001097 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001098 return flags;
1099 }
1100
1101 if (mChildrenChanged) {
1102 flags |= eVisibleRegion;
1103 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001104 }
1105
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001106 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001107 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001108 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001109 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001110 }
1111
1112 flags = doTransactionResize(flags, &c);
1113
Alec Mourib416efd2018-09-06 21:01:59 +00001114 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001115
1116 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 // invalidate and recompute the visible regions if needed
1118 flags |= Layer::eVisibleRegion;
1119 }
1120
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001121 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001122 // invalidate and recompute the visible regions if needed
1123 flags |= eVisibleRegion;
1124 this->contentDirty = true;
1125
1126 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001127 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001128 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001129 }
1130
Dan Stozac8145172016-04-28 16:29:06 -07001131 // If the layer is hidden, signal and clear out all local sync points so
1132 // that transactions for layers depending on this layer's frames becoming
1133 // visible are not blocked
1134 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001135 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001136 }
1137
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001138 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001139 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001140 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001141 }
1142
Mathias Agopian13127d82013-03-05 17:47:11 -08001143 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001144 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001145 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001146 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147}
1148
Pablo Ceballos05289c22016-04-14 15:49:55 -07001149void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001150 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001151}
1152
Mathias Agopian13127d82013-03-05 17:47:11 -08001153uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001154 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001155}
1156
1157uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001158 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001159}
1160
Robert Carr82364e32016-05-15 11:27:47 -07001161bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001162 if (mCurrentState.requested_legacy.transform.tx() == x &&
1163 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001164 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001165 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001166
1167 // We update the requested and active position simultaneously because
1168 // we want to apply the position portion of the transform matrix immediately,
1169 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001170 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001171 if (immediate && !mFreezeGeometryUpdates) {
1172 // Here we directly update the active state
1173 // unlike other setters, because we store it within
1174 // the transform, but use different latching rules.
1175 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001176 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001177 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001178 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001179
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001180 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001181 setTransactionFlags(eTransactionNeeded);
1182 return true;
1183}
Robert Carr82364e32016-05-15 11:27:47 -07001184
Robert Carr1f0a16a2016-10-24 16:27:39 -07001185bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1186 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1187 if (idx < 0) {
1188 return false;
1189 }
1190 if (childLayer->setLayer(z)) {
1191 mCurrentChildren.removeAt(idx);
1192 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001193 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001194 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001195 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001196}
1197
Robert Carr503c7042017-09-27 15:06:08 -07001198bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1199 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1200 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1201 if (idx < 0) {
1202 return false;
1203 }
1204 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1205 mCurrentChildren.removeAt(idx);
1206 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001207 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001208 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001209 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001210}
1211
Robert Carrae060832016-11-28 10:51:00 -08001212bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001213 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1214 mCurrentState.sequence++;
1215 mCurrentState.z = z;
1216 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001217
1218 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001219 if (mCurrentState.zOrderRelativeOf != nullptr) {
1220 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001221 if (strongRelative != nullptr) {
1222 strongRelative->removeZOrderRelative(this);
1223 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001224 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001225 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 setTransactionFlags(eTransactionNeeded);
1227 return true;
1228}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001229
Robert Carrdb66e622017-04-10 16:55:57 -07001230void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001231 mCurrentState.zOrderRelatives.remove(relative);
1232 mCurrentState.sequence++;
1233 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001234 setTransactionFlags(eTransactionNeeded);
1235}
1236
1237void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001238 mCurrentState.zOrderRelatives.add(relative);
1239 mCurrentState.modified = true;
1240 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001241 setTransactionFlags(eTransactionNeeded);
1242}
1243
Robert Carr503d2bd2017-12-04 15:49:47 -08001244bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001245 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1246 if (handle == nullptr) {
1247 return false;
1248 }
1249 sp<Layer> relative = handle->owner.promote();
1250 if (relative == nullptr) {
1251 return false;
1252 }
1253
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001254 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1255 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001256 return false;
1257 }
1258
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001259 mCurrentState.sequence++;
1260 mCurrentState.modified = true;
1261 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001262
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001263 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001264 if (oldZOrderRelativeOf != nullptr) {
1265 oldZOrderRelativeOf->removeZOrderRelative(this);
1266 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001267 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001268 relative->addZOrderRelative(this);
1269
1270 setTransactionFlags(eTransactionNeeded);
1271
1272 return true;
1273}
1274
Mathias Agopian13127d82013-03-05 17:47:11 -08001275bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001276 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001277 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001278 mCurrentState.requested_legacy.w = w;
1279 mCurrentState.requested_legacy.h = h;
1280 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001282
1283 // record the new size, from this point on, when the client request
1284 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001285 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 return true;
1287}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001288bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001289 if (mCurrentState.color.a == alpha) return false;
1290 mCurrentState.sequence++;
1291 mCurrentState.color.a = alpha;
1292 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293 setTransactionFlags(eTransactionNeeded);
1294 return true;
1295}
chaviw13fdc492017-06-27 12:40:18 -07001296
Valerie Haudd0b7572019-01-29 14:59:27 -08001297bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1298 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001299 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001300 }
1301 mCurrentState.sequence++;
1302 mCurrentState.modified = true;
1303 setTransactionFlags(eTransactionNeeded);
1304
1305 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001306 // create background color layer if one does not yet exist
1307 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1308 const String8& name = mName + "BackgroundColorLayer";
1309 mCurrentState.bgColorLayer =
1310 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001311
Valerie Haudd0b7572019-01-29 14:59:27 -08001312 // add to child list
1313 addChild(mCurrentState.bgColorLayer);
1314 mFlinger->mLayersAdded = true;
1315 // set up SF to handle added color layer
1316 if (isRemovedFromCurrentState()) {
1317 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1318 }
1319 mFlinger->setTransactionFlags(eTransactionNeeded);
1320 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1321 mCurrentState.bgColorLayer->reparent(nullptr);
1322 mCurrentState.bgColorLayer = nullptr;
1323 return true;
1324 }
1325
1326 mCurrentState.bgColorLayer->setColor(color);
1327 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1328 mCurrentState.bgColorLayer->setAlpha(alpha);
1329 mCurrentState.bgColorLayer->setDataspace(dataspace);
1330
chaviw13fdc492017-06-27 12:40:18 -07001331 return true;
1332}
1333
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001334bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001335 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001336
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001337 mCurrentState.sequence++;
1338 mCurrentState.cornerRadius = cornerRadius;
1339 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001340 setTransactionFlags(eTransactionNeeded);
1341 return true;
1342}
1343
Robert Carrd4ae7f32018-06-07 16:10:57 -07001344bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1345 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001346 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001347 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1348
1349 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1350 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1351 return false;
1352 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001353 mCurrentState.sequence++;
1354 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1355 matrix.dsdy);
1356 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001357 setTransactionFlags(eTransactionNeeded);
1358 return true;
1359}
Marissa Wall61c58622018-07-18 10:12:20 -07001360
Mathias Agopian13127d82013-03-05 17:47:11 -08001361bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001362 mCurrentState.requestedTransparentRegion_legacy = transparent;
1363 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001364 setTransactionFlags(eTransactionNeeded);
1365 return true;
1366}
1367bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001368 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1369 if (mCurrentState.flags == newFlags) return false;
1370 mCurrentState.sequence++;
1371 mCurrentState.flags = newFlags;
1372 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001373 setTransactionFlags(eTransactionNeeded);
1374 return true;
1375}
Robert Carr99e27f02016-06-16 15:18:02 -07001376
Marissa Wallf58c14b2018-07-24 10:50:43 -07001377bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001378 if (mCurrentState.requestedCrop_legacy == crop) return false;
1379 mCurrentState.sequence++;
1380 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001381 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001382 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001383 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001384 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1385
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001386 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001387 setTransactionFlags(eTransactionNeeded);
1388 return true;
1389}
Robert Carr8d5227b2017-03-16 15:41:03 -07001390
Robert Carrc3574f72016-03-24 12:19:32 -07001391bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001392 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001393 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001394 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001395 return true;
1396}
1397
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001398bool Layer::setMetadata(LayerMetadata data) {
1399 bool changed = data.mMap != mCurrentState.metadata.mMap;
1400 if (!changed) return false;
1401 mCurrentState.metadata = std::move(data);
1402 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001403 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001404 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001405 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001406}
1407
Mathias Agopian13127d82013-03-05 17:47:11 -08001408bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001409 if (mCurrentState.layerStack == layerStack) return false;
1410 mCurrentState.sequence++;
1411 mCurrentState.layerStack = layerStack;
1412 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001413 setTransactionFlags(eTransactionNeeded);
1414 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001415}
1416
Robert Carr1f0a16a2016-10-24 16:27:39 -07001417uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001418 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001419 if (p == nullptr) {
1420 return getDrawingState().layerStack;
1421 }
1422 return p->getLayerStack();
1423}
1424
Marissa Wallf58c14b2018-07-24 10:50:43 -07001425void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001426 mCurrentState.barrierLayer_legacy = barrierLayer;
1427 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001428 // We don't set eTransactionNeeded, because just receiving a deferral
1429 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001430 mCurrentState.modified = true;
1431 pushPendingState();
1432 mCurrentState.barrierLayer_legacy = nullptr;
1433 mCurrentState.frameNumber_legacy = 0;
1434 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001435}
1436
Marissa Wallf58c14b2018-07-24 10:50:43 -07001437void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001438 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001439 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001440}
1441
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442// ----------------------------------------------------------------------------
1443// pageflip handling...
1444// ----------------------------------------------------------------------------
1445
Robert Carr1f0a16a2016-10-24 16:27:39 -07001446bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001447 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001448 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001449 if (parent != nullptr && parent->isHiddenByPolicy()) {
1450 return true;
1451 }
1452 return s.flags & layer_state_t::eLayerHidden;
1453}
1454
David Sodman41fdfc92017-11-06 16:09:56 -08001455uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001456 // TODO: should we do something special if mSecure is set?
1457 if (mProtectedByApp) {
1458 // need a hardware-protected path to external video sink
1459 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001460 }
Riley Andrews03414a12014-07-01 14:22:59 -07001461 if (mPotentialCursor) {
1462 usage |= GraphicBuffer::USAGE_CURSOR;
1463 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001464 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001465 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001466}
1467
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001468void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001469 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001470 // Disable setting transform hint if the debug flag is set or if the
1471 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1472 // in one orientation.
1473 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001474 // The transform hint is used to improve performance, but we can
1475 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001476 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001477 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001478 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001479 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001480 orientation = 0;
1481 }
1482 }
David Sodmaneb085e02017-10-05 18:49:04 -07001483 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001484}
1485
Mathias Agopian13127d82013-03-05 17:47:11 -08001486// ----------------------------------------------------------------------------
1487// debugging
1488// ----------------------------------------------------------------------------
1489
Marissa Wall61c58622018-07-18 10:12:20 -07001490// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001491LayerDebugInfo Layer::getLayerDebugInfo() const {
1492 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001493 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001494 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001495 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001496 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001497 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001498 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001499 info.mVisibleRegion = visibleRegion;
1500 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001501 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001502 info.mX = ds.active_legacy.transform.tx();
1503 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001504 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001505 info.mWidth = ds.active_legacy.w;
1506 info.mHeight = ds.active_legacy.h;
1507 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001508 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001509 info.mFlags = ds.flags;
1510 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001511 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001512 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1513 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1514 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1515 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001516 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001517 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001518 if (buffer != 0) {
1519 info.mActiveBufferWidth = buffer->getWidth();
1520 info.mActiveBufferHeight = buffer->getHeight();
1521 info.mActiveBufferStride = buffer->getStride();
1522 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001523 } else {
1524 info.mActiveBufferWidth = 0;
1525 info.mActiveBufferHeight = 0;
1526 info.mActiveBufferStride = 0;
1527 info.mActiveBufferFormat = 0;
1528 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001529 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001530 info.mNumQueuedFrames = getQueuedFrameCount();
1531 info.mRefreshPending = isBufferLatched();
1532 info.mIsOpaque = isOpaque(ds);
1533 info.mContentDirty = contentDirty;
1534 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001535}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001536
Yiwei Zhang5434a782018-12-05 18:06:32 -08001537void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001538 result.append("-------------------------------");
1539 result.append("-------------------------------");
1540 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001541 result.append(" Layer name\n");
1542 result.append(" Z | ");
1543 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001544 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001545 result.append(" Disp Frame (LTRB) | ");
1546 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001547 result.append("-------------------------------");
1548 result.append("-------------------------------");
1549 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001550}
1551
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001552void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1553 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1554 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001555 return;
1556 }
1557
Yiwei Zhang5434a782018-12-05 18:06:32 -08001558 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001559 if (mName.length() > 77) {
1560 std::string shortened;
1561 shortened.append(mName.string(), 36);
1562 shortened.append("[...]");
1563 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001564 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001565 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001566 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001567 }
1568
Yiwei Zhang5434a782018-12-05 18:06:32 -08001569 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001570
Alec Mourib416efd2018-09-06 21:01:59 +00001571 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001572 const auto& compositionState = outputLayer->getState();
1573
Chia-I Wu1e043612018-03-01 09:45:09 -08001574 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001575 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001576 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001577 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001578 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001579 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1580 StringAppendF(&result, "%10s | ",
1581 toString(getCompositionLayer() ? compositionState.bufferTransform
1582 : static_cast<Hwc2::Transform>(0))
1583 .c_str());
1584 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001585 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001586 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001587 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1588 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001589
Yichi Chen6ca35192018-05-29 12:20:43 +08001590 result.append("- - - - - - - - - - - - - - - -");
1591 result.append("- - - - - - - - - - - - - - - -");
1592 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001593}
Dan Stozae22aec72016-08-01 13:20:59 -07001594
Yiwei Zhang5434a782018-12-05 18:06:32 -08001595void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001596 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001597}
1598
Svetoslavd85084b2014-03-20 10:28:31 -07001599void Layer::clearFrameStats() {
1600 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001601}
1602
Jamie Gennis6547ff42013-07-16 20:12:42 -07001603void Layer::logFrameStats() {
1604 mFrameTracker.logAndResetStats(mName);
1605}
1606
Svetoslavd85084b2014-03-20 10:28:31 -07001607void Layer::getFrameStats(FrameStats* outStats) const {
1608 mFrameTracker.getStats(outStats);
1609}
1610
Yiwei Zhang5434a782018-12-05 18:06:32 -08001611void Layer::dumpFrameEvents(std::string& result) {
1612 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001613 Mutex::Autolock lock(mFrameEventHistoryMutex);
1614 mFrameEventHistory.checkFencesForCompletion();
1615 mFrameEventHistory.dump(result);
1616}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001617
Brian Anderson5ea5e592016-12-01 16:54:33 -08001618void Layer::onDisconnect() {
1619 Mutex::Autolock lock(mFrameEventHistoryMutex);
1620 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001621 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001622}
1623
Brian Anderson3890c392016-07-25 12:48:08 -07001624void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001625 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001626 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001627 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1628 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001629 }
1630
Brian Andersond6927fb2016-07-23 23:37:30 -07001631 Mutex::Autolock lock(mFrameEventHistoryMutex);
1632 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001633 // If there are any unsignaled fences in the aquire timeline at this
1634 // point, the previously queued frame hasn't been latched yet. Go ahead
1635 // and try to get the signal time here so the syscall is taken out of
1636 // the main thread's critical path.
1637 mAcquireTimeline.updateSignalTimes();
1638 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001639 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001640 mFrameEventHistory.addQueue(*newTimestamps);
1641 }
1642
Brian Anderson3890c392016-07-25 12:48:08 -07001643 if (outDelta) {
1644 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001645 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001646}
Dan Stozae77c7662016-05-13 11:37:28 -07001647
Chia-I Wu98f1c102017-05-30 14:54:08 -07001648size_t Layer::getChildrenCount() const {
1649 size_t count = 0;
1650 for (const sp<Layer>& child : mCurrentChildren) {
1651 count += 1 + child->getChildrenCount();
1652 }
1653 return count;
1654}
1655
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001657 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001658 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001659
Robert Carr1f0a16a2016-10-24 16:27:39 -07001660 mCurrentChildren.add(layer);
1661 layer->setParent(this);
1662}
1663
1664ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001665 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001666 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001667
Robert Carr1323c952019-01-28 18:13:27 -08001668 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669 return mCurrentChildren.remove(layer);
1670}
1671
Robert Carr1db73f62016-12-21 12:58:51 -08001672bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1673 sp<Handle> handle = nullptr;
1674 sp<Layer> newParent = nullptr;
1675 if (newParentHandle == nullptr) {
1676 return false;
1677 }
1678 handle = static_cast<Handle*>(newParentHandle.get());
1679 newParent = handle->owner.promote();
1680 if (newParent == nullptr) {
1681 ALOGE("Unable to promote Layer handle");
1682 return false;
1683 }
1684
chaviw5aedec92018-10-22 10:40:38 -07001685 if (attachChildren()) {
1686 setTransactionFlags(eTransactionNeeded);
1687 }
Robert Carr1db73f62016-12-21 12:58:51 -08001688 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001689 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001690 }
1691 mCurrentChildren.clear();
1692
1693 return true;
1694}
1695
Robert Carr15eae092018-03-23 13:43:53 -07001696void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001697 for (const sp<Layer>& child : mDrawingChildren) {
1698 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001699 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001700 }
1701}
1702
chaviwf1961f72017-09-18 16:41:07 -07001703bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001704 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001705
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001706 // While layers are detached, we allow most operations
1707 // and simply halt performing the actual transaction. However
1708 // for reparent != null we would enter the mRemovedFromCurrentState
1709 // state, regardless of whether doTransaction was called, and
1710 // so we need to prevent the update here.
1711 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001712 return false;
1713 }
1714
Robert Carr54cf5b12019-01-25 14:02:28 -08001715 sp<Layer> newParent;
1716 if (newParentHandle != nullptr) {
1717 auto handle = static_cast<Handle*>(newParentHandle.get());
1718 newParent = handle->owner.promote();
1719 if (newParent == nullptr) {
1720 ALOGE("Unable to promote Layer handle");
1721 return false;
1722 }
1723 if (newParent == this) {
1724 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1725 return false;
1726 }
1727 }
1728
chaviwf1961f72017-09-18 16:41:07 -07001729 sp<Layer> parent = getParent();
1730 if (parent != nullptr) {
1731 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001732 }
1733
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001734 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001735 newParent->addChild(this);
1736 if (!newParent->isRemovedFromCurrentState()) {
1737 addToCurrentState();
1738 } else {
1739 onRemovedFromCurrentState();
1740 }
1741
1742 if (mLayerDetached) {
1743 mLayerDetached = false;
1744 callSetTransactionFlags = true;
1745 }
1746 } else {
1747 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001748 }
1749
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001750 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001751 setTransactionFlags(eTransactionNeeded);
1752 }
chaviw06178942017-07-27 10:25:59 -07001753 return true;
1754}
1755
Robert Carr9524cb32017-02-13 11:32:32 -08001756bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001757 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001758 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001759 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001760 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001761 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001762 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001763 }
Robert Carr7f619b22017-11-06 12:56:35 -08001764 }
Robert Carr9524cb32017-02-13 11:32:32 -08001765
1766 return true;
1767}
1768
chaviw5aedec92018-10-22 10:40:38 -07001769bool Layer::attachChildren() {
1770 bool changed = false;
1771 for (const sp<Layer>& child : mCurrentChildren) {
1772 sp<Client> parentClient = mClientRef.promote();
1773 sp<Client> client(child->mClientRef.promote());
1774 if (client != nullptr && parentClient != client) {
1775 if (child->mLayerDetached) {
1776 child->mLayerDetached = false;
1777 changed = true;
1778 }
1779 changed |= child->attachChildren();
1780 }
1781 }
1782
1783 return changed;
1784}
1785
Peiyong Lind3788632018-09-18 16:01:31 -07001786bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001787 static const mat4 identityMatrix = mat4();
1788
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001789 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001790 return false;
1791 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001792 ++mCurrentState.sequence;
1793 mCurrentState.colorTransform = matrix;
1794 mCurrentState.hasColorTransform = matrix != identityMatrix;
1795 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001796 setTransactionFlags(eTransactionNeeded);
1797 return true;
1798}
1799
chaviwf66724d2018-11-28 16:35:21 -08001800mat4 Layer::getColorTransform() const {
1801 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1802 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1803 colorTransform = parent->getColorTransform() * colorTransform;
1804 }
1805 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001806}
1807
1808bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001809 bool hasColorTransform = getDrawingState().hasColorTransform;
1810 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1811 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1812 }
1813 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001814}
1815
Chia-I Wu11481472018-05-04 10:43:19 -07001816bool Layer::isLegacyDataSpace() const {
1817 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001818 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001819 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001820}
1821
Robert Carr1f0a16a2016-10-24 16:27:39 -07001822void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001823 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001824}
1825
1826void Layer::clearSyncPoints() {
1827 for (const auto& child : mCurrentChildren) {
1828 child->clearSyncPoints();
1829 }
1830
1831 Mutex::Autolock lock(mLocalSyncPointMutex);
1832 for (auto& point : mLocalSyncPoints) {
1833 point->setFrameAvailable();
1834 }
1835 mLocalSyncPoints.clear();
1836}
1837
1838int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001839 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001840}
1841
Robert Carr29abff82017-12-04 13:51:20 -08001842bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1843 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001844 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001845 return state.zOrderRelativeOf != nullptr;
1846}
1847
David Sodman41fdfc92017-11-06 16:09:56 -08001848__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001849 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001850 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1851 "makeTraversalList received invalid stateSet");
1852 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1853 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001854 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001855
Robert Carr29abff82017-12-04 13:51:20 -08001856 if (state.zOrderRelatives.size() == 0) {
1857 *outSkipRelativeZUsers = true;
1858 return children;
1859 }
1860
chaviwfd462612018-05-31 16:11:27 -07001861 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001862 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001863 sp<Layer> strongRelative = weakRelative.promote();
1864 if (strongRelative != nullptr) {
1865 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001866 }
1867 }
1868
Dan Stoza412903f2017-04-27 13:42:17 -07001869 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001870 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001871 if (childState.zOrderRelativeOf != nullptr) {
1872 continue;
1873 }
Robert Carrdb66e622017-04-10 16:55:57 -07001874 traverse.add(child);
1875 }
1876
1877 return traverse;
1878}
1879
Robert Carr1f0a16a2016-10-24 16:27:39 -07001880/**
Robert Carrdb66e622017-04-10 16:55:57 -07001881 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001882 */
Dan Stoza412903f2017-04-27 13:42:17 -07001883void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001884 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1885 // produce a new list for traversing, including our relatives, and not including our children
1886 // who are relatives of another surface. In the case that there are no relative Z,
1887 // makeTraversalList returns our children directly to avoid significant overhead.
1888 // However in this case we need to take the responsibility for filtering children which
1889 // are relatives of another surface here.
1890 bool skipRelativeZUsers = false;
1891 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001892
Robert Carr1f0a16a2016-10-24 16:27:39 -07001893 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001894 for (; i < list.size(); i++) {
1895 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001896 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1897 continue;
1898 }
1899
Robert Carrdb66e622017-04-10 16:55:57 -07001900 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001901 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001902 }
Dan Stoza412903f2017-04-27 13:42:17 -07001903 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001904 }
Robert Carr29abff82017-12-04 13:51:20 -08001905
Dan Stoza412903f2017-04-27 13:42:17 -07001906 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001907 for (; i < list.size(); i++) {
1908 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001909
Robert Carr29abff82017-12-04 13:51:20 -08001910 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1911 continue;
1912 }
Dan Stoza412903f2017-04-27 13:42:17 -07001913 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001914 }
1915}
1916
1917/**
Robert Carrdb66e622017-04-10 16:55:57 -07001918 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001919 */
Dan Stoza412903f2017-04-27 13:42:17 -07001920void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1921 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001922 // See traverseInZOrder for documentation.
1923 bool skipRelativeZUsers = false;
1924 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001925
Robert Carr1f0a16a2016-10-24 16:27:39 -07001926 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001927 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001928 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001929
1930 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1931 continue;
1932 }
1933
Robert Carrdb66e622017-04-10 16:55:57 -07001934 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001935 break;
1936 }
Dan Stoza412903f2017-04-27 13:42:17 -07001937 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001938 }
Dan Stoza412903f2017-04-27 13:42:17 -07001939 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001940 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001941 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001942
1943 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1944 continue;
1945 }
1946
Dan Stoza412903f2017-04-27 13:42:17 -07001947 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001948 }
1949}
1950
chaviw4b129c22018-04-09 16:19:43 -07001951LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1952 const std::vector<Layer*>& layersInTree) {
1953 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1954 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001955 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1956 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001957 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001958
chaviwfd462612018-05-31 16:11:27 -07001959 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001960 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1961 sp<Layer> strongRelative = weakRelative.promote();
1962 // Only add relative layers that are also descendents of the top most parent of the tree.
1963 // If a relative layer is not a descendent, then it should be ignored.
1964 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1965 traverse.add(strongRelative);
1966 }
1967 }
1968
1969 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001970 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001971 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1972 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1973 // the child here since it won't be added later as a relative.
1974 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1975 childState.zOrderRelativeOf.promote().get())) {
1976 continue;
1977 }
1978 traverse.add(child);
1979 }
1980
1981 return traverse;
1982}
1983
1984void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1985 LayerVector::StateSet stateSet,
1986 const LayerVector::Visitor& visitor) {
1987 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001988
1989 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001990 for (; i < list.size(); i++) {
1991 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001992 if (relative->getZ() >= 0) {
1993 break;
1994 }
chaviw4b129c22018-04-09 16:19:43 -07001995 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001996 }
chaviw4b129c22018-04-09 16:19:43 -07001997
chaviwa76b2712017-09-20 12:02:26 -07001998 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001999 for (; i < list.size(); i++) {
2000 const auto& relative = list[i];
2001 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002002 }
2003}
2004
chaviw4b129c22018-04-09 16:19:43 -07002005std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
2006 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2007 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2008
2009 std::vector<Layer*> layersInTree = {this};
2010 for (size_t i = 0; i < children.size(); i++) {
2011 const auto& child = children[i];
2012 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
2013 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
2014 }
2015
2016 return layersInTree;
2017}
2018
2019void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2020 const LayerVector::Visitor& visitor) {
2021 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
2022 std::sort(layersInTree.begin(), layersInTree.end());
2023 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2024}
2025
Peiyong Linefefaac2018-08-17 12:27:51 -07002026ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002027 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002028}
2029
chaviw13fdc492017-06-27 12:40:18 -07002030half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002031 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002032
chaviw13fdc492017-06-27 12:40:18 -07002033 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2034 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002035}
Robert Carr6452f122017-03-21 10:41:29 -07002036
chaviw13fdc492017-06-27 12:40:18 -07002037half4 Layer::getColor() const {
2038 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002039 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002040}
Robert Carr6452f122017-03-21 10:41:29 -07002041
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002042Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2043 const auto& p = mDrawingParent.promote();
2044 if (p != nullptr) {
2045 RoundedCornerState parentState = p->getRoundedCornerState();
2046 if (parentState.radius > 0) {
2047 ui::Transform t = getActiveTransform(getDrawingState());
2048 t = t.inverse();
2049 parentState.cropRect = t.transform(parentState.cropRect);
2050 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2051 // but a transform matrix can define horizontal and vertical scales.
2052 // Let's take the average between both of them and pass into the shader, practically we
2053 // never do this type of transformation on windows anyway.
2054 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2055 return parentState;
2056 }
2057 }
2058 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002059 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002060}
2061
Robert Carr1f0a16a2016-10-24 16:27:39 -07002062void Layer::commitChildList() {
2063 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2064 const auto& child = mCurrentChildren[i];
2065 child->commitChildList();
2066 }
2067 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002068 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002069}
2070
Robert Carr720e5062018-07-30 17:45:14 -07002071void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002072 mCurrentState.inputInfo = info;
2073 mCurrentState.modified = true;
2074 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002075 setTransactionFlags(eTransactionNeeded);
2076}
2077
chaviw1d044282017-09-27 12:19:28 -07002078void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2079 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2080 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002081 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002082
Peiyong Linefefaac2018-08-17 12:27:51 -07002083 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002084 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002085
2086 layerInfo->set_id(sequence);
2087 layerInfo->set_name(getName().c_str());
2088 layerInfo->set_type(String8(getTypeId()));
2089
2090 for (const auto& child : children) {
2091 layerInfo->add_children(child->sequence);
2092 }
2093
2094 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2095 sp<Layer> strongRelative = weakRelative.promote();
2096 if (strongRelative != nullptr) {
2097 layerInfo->add_relatives(strongRelative->sequence);
2098 }
2099 }
2100
Marissa Wallf58c14b2018-07-24 10:50:43 -07002101 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002102 layerInfo->mutable_transparent_region());
2103 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2104 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2105
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002106 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002107 layerInfo->set_z(state.z);
2108
2109 PositionProto* position = layerInfo->mutable_position();
2110 position->set_x(transform.tx());
2111 position->set_y(transform.ty());
2112
2113 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2114 requestedPosition->set_x(requestedTransform.tx());
2115 requestedPosition->set_y(requestedTransform.ty());
2116
2117 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002118 size->set_w(state.active_legacy.w);
2119 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002120
Marissa Wallf58c14b2018-07-24 10:50:43 -07002121 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002122 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002123
2124 layerInfo->set_is_opaque(isOpaque(state));
2125 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002126
2127 // XXX (b/79210409) mCurrentDataSpace is not protected
2128 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2129
chaviw1d044282017-09-27 12:19:28 -07002130 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2131 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2132 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2133 layerInfo->set_flags(state.flags);
2134
2135 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2136 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2137
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002138 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002139 if (parent != nullptr) {
2140 layerInfo->set_parent(parent->sequence);
2141 }
2142
2143 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2144 if (zOrderRelativeOf != nullptr) {
2145 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2146 }
2147
Lloyd Pique0b785d82018-12-04 17:25:27 -08002148 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002149 if (buffer != nullptr) {
2150 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002151 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002152 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002153 }
2154
2155 layerInfo->set_queued_frames(getQueuedFrameCount());
2156 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002157 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002158 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002159
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002160 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002161 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002162 if (barrierLayer != nullptr) {
2163 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2164 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002165 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002166 }
2167 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002168
2169 auto protoMap = layerInfo->mutable_metadata();
2170 for (const auto& entry : state.metadata.mMap) {
2171 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2172 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002173 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
2174 LayerProtoHelper::writeToProto(mSourceBounds, layerInfo->mutable_source_bounds());
2175 LayerProtoHelper::writeToProto(mScreenBounds, layerInfo->mutable_screen_bounds());
2176 LayerProtoHelper::writeToProto(mBounds, layerInfo->mutable_bounds());
chaviw1d044282017-09-27 12:19:28 -07002177}
2178
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002179void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2180 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2181 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002182 return;
2183 }
2184
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002185 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2186
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002187 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002188
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002189 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002190 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2191
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002192 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002193 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2194
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002195 const int32_t transform =
2196 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002197 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002198
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002199 const int32_t compositionType =
2200 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2201 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002202 layerInfo->set_hwc_composition_type(compositionType);
2203
2204 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2205 static_cast<BufferLayer*>(this)->isProtected()) {
2206 layerInfo->set_is_protected(true);
2207 } else {
2208 layerInfo->set_is_protected(false);
2209 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002210}
2211
Robert Carr2e102c92018-10-23 12:11:15 -07002212bool Layer::isRemovedFromCurrentState() const {
2213 return mRemovedFromCurrentState;
2214}
2215
Arthur Hungd20b2702019-01-14 18:16:16 +08002216InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002217 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002218
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002219 if (info.displayId == ADISPLAY_ID_NONE) {
2220 info.displayId = mDrawingState.layerStack;
2221 }
2222
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002223 ui::Transform t = getTransform();
2224 const float xScale = t.sx();
2225 const float yScale = t.sy();
2226 if (xScale != 1.0f || yScale != 1.0f) {
2227 info.windowXScale *= 1.0f / xScale;
2228 info.windowYScale *= 1.0f / yScale;
2229 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002230 }
Robert Carre07e1032018-11-26 12:55:53 -08002231
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002232 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002233 // If this is a portal window, set the touchableRegion to the layerBounds.
2234 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2235 ? getBufferSize(getDrawingState())
2236 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002237 if (!layerBounds.isValid()) {
2238 layerBounds = getCroppedBufferSize(getDrawingState());
2239 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002240 layerBounds = t.transform(layerBounds);
2241 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2242
Arthur Hungd20b2702019-01-14 18:16:16 +08002243 // Input coordinate should match the layer bounds.
2244 info.frameLeft = layerBounds.left;
2245 info.frameTop = layerBounds.top;
2246 info.frameRight = layerBounds.right;
2247 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002248
2249 // Position the touchable region relative to frame screen location and restrict it to frame
2250 // bounds.
2251 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002252 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002253 return info;
2254}
2255
2256bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002257 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002258}
2259
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002260std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2261 return nullptr;
2262}
2263
chaviw3e727cd2019-01-31 13:41:05 -08002264bool Layer::canReceiveInput() const {
2265 return isVisible();
2266}
2267
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002268compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2269 const sp<const DisplayDevice>& display) const {
2270 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2271}
2272
Mathias Agopian13127d82013-03-05 17:47:11 -08002273// ---------------------------------------------------------------------------
2274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002275}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002276
2277#if defined(__gl_h_)
2278#error "don't include gl/gl.h in this file"
2279#endif
2280
2281#if defined(__gl2_h_)
2282#error "don't include gl2/gl2.h in this file"
2283#endif