blob: 688c5ee2a769142685df531557836cd511721ac0 [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
Alec Mourie60041e2019-06-14 18:59:51 -070022#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Yiwei Zhang5434a782018-12-05 18:06:32 -080024#include <android-base/stringprintf.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070025#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080026#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080027#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080028#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080030#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080031#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070032#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070034#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <gui/BufferItem.h>
36#include <gui/LayerDebugInfo.h>
37#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070038#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070040#include <stdint.h>
41#include <stdlib.h>
42#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080043#include <ui/DebugUtils.h>
44#include <ui/GraphicBuffer.h>
45#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/Errors.h>
47#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080048#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Alec Mourie60041e2019-06-14 18:59:51 -070052#include <algorithm>
53#include <mutex>
54#include <sstream>
55
Yiwei Zhang60d1a192018-03-07 14:52:28 -080056#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080057#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020058#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070059#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080060#include "DisplayHardware/HWComposer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080061#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070062#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070063#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080065#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070066
David Sodman41fdfc92017-11-06 16:09:56 -080067#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069namespace android {
70
Yiwei Zhang5434a782018-12-05 18:06:32 -080071using base::StringAppendF;
72
Lloyd Piquef1c675b2018-09-12 20:45:39 -070073std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080074
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070076 : mFlinger(args.flinger),
77 mName(args.name),
78 mClientRef(args.client),
79 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070080 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081
82 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070083 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
84 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
85 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070086
Dan Stozaf7ba41a2017-05-10 15:11:11 -070087 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070088
Lloyd Pique0449b0f2018-12-20 16:23:45 -080089 mCurrentState.active_legacy.w = args.w;
90 mCurrentState.active_legacy.h = args.h;
91 mCurrentState.flags = layerFlags;
92 mCurrentState.active_legacy.transform.set(0, 0);
93 mCurrentState.crop_legacy.makeInvalid();
94 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
95 mCurrentState.z = 0;
96 mCurrentState.color.a = 1.0f;
97 mCurrentState.layerStack = 0;
98 mCurrentState.sequence = 0;
99 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800100 mCurrentState.active.w = UINT32_MAX;
101 mCurrentState.active.h = UINT32_MAX;
102 mCurrentState.active.transform.set(0, 0);
103 mCurrentState.transform = 0;
104 mCurrentState.transformToDisplayInverse = false;
105 mCurrentState.crop.makeInvalid();
106 mCurrentState.acquireFence = new Fence(-1);
107 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
108 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700109 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mCurrentState.cornerRadius = 0.0f;
111 mCurrentState.api = -1;
112 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800113 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800114 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115
116 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700118
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800119 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700120 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800121 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200122 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700123
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700124 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Vishnu Nair0f085c62019-08-30 08:49:12 -0700125 mCallingPid = args.callingPid;
126 mCallingUid = args.callingUid;
Robert Carr2e102c92018-10-23 12:11:15 -0700127 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700128}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700129
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700130Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800131 sp<Client> c(mClientRef.promote());
132 if (c != 0) {
133 c->detachLayer(this);
134 }
135
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000136 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700137 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700138}
139
Vishnu Nair0f085c62019-08-30 08:49:12 -0700140LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
141 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
142 LayerMetadata metadata)
143 : flinger(flinger),
144 client(client),
145 name(name),
146 w(w),
147 h(h),
148 flags(flags),
149 metadata(std::move(metadata)) {
150 IPCThreadState* ipc = IPCThreadState::self();
151 callingPid = ipc->getCallingPid();
152 callingUid = ipc->getCallingUid();
153}
154
Mathias Agopian13127d82013-03-05 17:47:11 -0800155// ---------------------------------------------------------------------------
156// callbacks
157// ---------------------------------------------------------------------------
158
David Sodmaneb085e02017-10-05 18:49:04 -0700159/*
160 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
161 * Layer. So, the implementation is done in BufferLayer. When called on a
162 * ColorLayer object, it's essentially a NOP.
163 */
David Sodmaneb085e02017-10-05 18:49:04 -0700164void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800165
chaviw43cb3cb2019-05-31 15:23:41 -0700166void Layer::removeRemoteSyncPoints() {
167 for (auto& point : mRemoteSyncPoints) {
168 point->setTransactionApplied();
169 }
170 mRemoteSyncPoints.clear();
171
172 {
chaviw43cb3cb2019-05-31 15:23:41 -0700173 for (State pendingState : mPendingStates) {
174 pendingState.barrierLayer_legacy = nullptr;
175 }
176 }
177}
178
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700179void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
180 if (mCurrentState.zOrderRelativeOf == nullptr) {
181 return;
182 }
Robert Carr2e102c92018-10-23 12:11:15 -0700183
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700184 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
185 if (strongRelative == nullptr) {
186 setZOrderRelativeOf(nullptr);
187 return;
188 }
189
190 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
191 strongRelative->removeZOrderRelative(this);
192 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800193 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700194 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700195}
196
197void Layer::removeFromCurrentState() {
198 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000199
Robert Carr2e102c92018-10-23 12:11:15 -0700200 // Since we are no longer reachable from CurrentState SurfaceFlinger
201 // will no longer invoke doTransaction for us, and so we will
202 // never finish applying transactions. We signal the sync point
203 // now so that another layer will not become indefinitely
204 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700205 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700206
207 {
208 Mutex::Autolock syncLock(mLocalSyncPointMutex);
209 for (auto& point : mLocalSyncPoints) {
210 point->setFrameAvailable();
211 }
212 mLocalSyncPoints.clear();
213 }
214
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800215 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700216}
Chia-I Wu38512252017-05-17 14:36:16 -0700217
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700218void Layer::onRemovedFromCurrentState() {
219 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
220 std::sort(layersInTree.begin(), layersInTree.end());
221 for (const auto& layer : layersInTree) {
222 layer->removeFromCurrentState();
223 layer->removeRelativeZ(layersInTree);
224 }
225}
226
chaviw61626f22018-11-15 16:26:27 -0800227void Layer::addToCurrentState() {
228 mRemovedFromCurrentState = false;
229
230 for (const auto& child : mCurrentChildren) {
231 child->addToCurrentState();
232 }
233}
234
Mathias Agopian13127d82013-03-05 17:47:11 -0800235// ---------------------------------------------------------------------------
236// set-up
237// ---------------------------------------------------------------------------
238
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700239const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800240 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241}
242
chaviw13fdc492017-06-27 12:40:18 -0700243bool Layer::getPremultipledAlpha() const {
244 return mPremultipliedAlpha;
245}
246
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700247sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800248 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700249 if (mGetHandleCalled) {
250 ALOGE("Get handle called twice" );
251 return nullptr;
252 }
253 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700254 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800255}
256
257// ---------------------------------------------------------------------------
258// h/w composer set-up
259// ---------------------------------------------------------------------------
260
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800261Rect Layer::getContentCrop() const {
262 // this is the crop rectangle that applies to the buffer
263 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700264 Rect crop;
265 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800266 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800268 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800269 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800270 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700271 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800272 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700273 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700274 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 return crop;
276}
277
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700278static Rect reduce(const Rect& win, const Region& exclude) {
279 if (CC_LIKELY(exclude.isEmpty())) {
280 return win;
281 }
282 if (exclude.isRect()) {
283 return win.reduce(exclude.getBounds());
284 }
285 return Region(win).subtract(exclude).getBounds();
286}
287
Dan Stoza80d61162017-12-20 15:57:52 -0800288static FloatRect reduce(const FloatRect& win, const Region& exclude) {
289 if (CC_LIKELY(exclude.isEmpty())) {
290 return win;
291 }
292 // Convert through Rect (by rounding) for lack of FloatRegion
293 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
294}
295
Vishnu Nair4351ad52019-02-11 14:13:02 -0800296Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800297 if (!reduceTransparentRegion) {
298 return Rect{mScreenBounds};
299 }
300
301 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800302 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800303 // Transform to screen space.
304 bounds = t.transform(bounds);
305 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700306}
307
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000309 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800310 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700311}
312
Vishnu Nairf0c28512019-02-08 12:40:28 -0800313FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
314 // Subtract the transparent region and snap to the bounds.
315 return reduce(mBounds, activeTransparentRegion);
316}
317
Vishnu Nairc652ff82019-03-15 12:48:54 -0700318ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800319 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
320 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700321 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800322 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700323 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800324 }
325
Marissa Wall290ad082019-03-06 13:23:47 -0800326 // If the layer is a buffer state layer, the active width and height
327 // could be infinite. In that case, return the effective transform.
328 const uint32_t activeWidth = getActiveWidth(getDrawingState());
329 const uint32_t activeHeight = getActiveHeight(getDrawingState());
330 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700331 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800332 }
333
Vishnu Nairc652ff82019-03-15 12:48:54 -0700334 int bufferWidth = mActiveBuffer->getWidth();
335 int bufferHeight = mActiveBuffer->getHeight();
336
337 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
338 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800339 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700340
Marissa Wall290ad082019-03-06 13:23:47 -0800341 float sx = activeWidth / static_cast<float>(bufferWidth);
342 float sy = activeHeight / static_cast<float>(bufferHeight);
343
Vishnu Nair4351ad52019-02-11 14:13:02 -0800344 ui::Transform extraParentScaling;
345 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700346 return extraParentScaling;
347}
348
349ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
350 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
351 // treat child surfaces as pixels in the parent surface.
352 if (!isFixedSize() || !mActiveBuffer) {
353 return mEffectiveTransform;
354 }
355 return mEffectiveTransform * bufferScaleTransform;
356}
357
358FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
359 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
360 // cropped to its parent layer after any buffer transform scaling is applied.
361 if (!isFixedSize() || !mActiveBuffer) {
362 return mBounds;
363 }
364 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800365}
366
367void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
368 const State& s(getDrawingState());
369
370 // Calculate effective layer transform
371 mEffectiveTransform = parentTransform * getActiveTransform(s);
372
373 // Transform parent bounds to layer space
374 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
375
Vishnu Nairc652ff82019-03-15 12:48:54 -0700376 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800377 mSourceBounds = computeSourceBounds(parentBounds);
378
379 // Calculate bounds by croping diplay frame with layer crop and parent bounds
380 FloatRect bounds = mSourceBounds;
381 const Rect layerCrop = getCrop(s);
382 if (!layerCrop.isEmpty()) {
383 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
384 }
385 bounds = bounds.intersect(parentBounds);
386
387 mBounds = bounds;
388 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700389
390 // Add any buffer scaling to the layer's children.
391 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800392 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700393 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
394 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800395 }
396}
397
Vishnu Nair60356342018-11-13 13:00:45 -0800398Rect Layer::getCroppedBufferSize(const State& s) const {
399 Rect size = getBufferSize(s);
400 Rect crop = getCrop(s);
401 if (!crop.isEmpty() && size.isValid()) {
402 size.intersect(crop, &size);
403 } else if (!crop.isEmpty()) {
404 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700405 }
Vishnu Nair60356342018-11-13 13:00:45 -0800406 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800407}
408
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700409void Layer::setupRoundedCornersCropCoordinates(Rect win,
410 const FloatRect& roundedCornersCrop) const {
411 // Translate win by the rounded corners rect coordinates, to have all values in
412 // layer coordinate space.
413 win.left -= roundedCornersCrop.left;
414 win.right -= roundedCornersCrop.left;
415 win.top -= roundedCornersCrop.top;
416 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700417}
418
Lloyd Piquea83776c2019-01-29 18:42:32 -0800419void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
420 const auto& drawingState{getDrawingState()};
421 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500422 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800423 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800424 blendMode =
425 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800426 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800427
Lloyd Piquea83776c2019-01-29 18:42:32 -0800428 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
429 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700430 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400431 if (parent.get()) {
432 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800433 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
434 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700435 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800436 type = parentType;
437 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700438 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400439 }
440
Lloyd Piquea83776c2019-01-29 18:42:32 -0800441 compositionState.geomLayerTransform = getTransform();
442 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
443 compositionState.geomBufferSize = getBufferSize(drawingState);
444 compositionState.geomContentCrop = getContentCrop();
445 compositionState.geomCrop = getCrop(drawingState);
446 compositionState.geomBufferTransform = mCurrentTransform;
447 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
448 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
449 compositionState.geomLayerBounds = mBounds;
450 compositionState.geomUsesSourceCrop = usesSourceCrop();
451 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700452
Lloyd Piquea83776c2019-01-29 18:42:32 -0800453 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
454 compositionState.alpha = alpha;
455 compositionState.type = type;
456 compositionState.appId = appId;
457}
David Sodmanba340492018-08-05 21:51:33 -0700458
Lloyd Piquef5275482019-01-29 18:42:42 -0800459void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800460 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800461 compositionState.forceClientComposition = false;
462
463 // TODO(lpique): b/121291683 Remove this one we are sure we don't need the
464 // value recomputed / set every frame.
465 compositionState.geomVisibleRegion = visibleRegion;
466
467 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
468 compositionState.dataspace = mCurrentDataSpace;
469 compositionState.colorTransform = getColorTransform();
470 compositionState.colorTransformIsIdentity = !hasColorTransform();
471 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800472 compositionState.hasProtectedContent = isProtected();
473
474 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
475 compositionState.isOpaque =
476 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800477
478 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800479 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800480 compositionState.forceClientComposition = true;
481 }
482}
483
Lloyd Piquef16688f2019-02-19 17:47:57 -0800484bool Layer::onPreComposition(nsecs_t) {
485 return false;
486}
487
Lloyd Piquea83776c2019-01-29 18:42:32 -0800488void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
489 bool includeGeometry) const {
490 if (includeGeometry) {
491 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700492 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800493
494 latchPerFrameState(compositionState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800495}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700496
Lloyd Piquea83776c2019-01-29 18:42:32 -0800497const char* Layer::getDebugName() const {
498 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800499}
500
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700501void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800502 const auto outputLayer = findOutputLayerForDisplay(display);
503 LOG_FATAL_IF(!outputLayer);
504
505 if (!outputLayer->getState().hwc ||
506 (*outputLayer->getState().hwc).hwcCompositionType !=
507 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800508 return;
509 }
510
511 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800512 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800513
514 // Apply the layer's transform, followed by the display's global transform
515 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800516 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800517 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700518 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800519 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700520 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700521 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800522 auto position = displayTransform.transform(frame);
523
Dominik Laskowski7e045462018-05-30 13:02:02 -0700524 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800525 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800526 ALOGE_IF(error != HWC2::Error::None,
527 "[%s] Failed to set cursor position "
528 "to (%d, %d): %s (%d)",
529 mName.string(), position.left, position.top, to_string(error).c_str(),
530 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531}
Riley Andrews03414a12014-07-01 14:22:59 -0700532
Mathias Agopian13127d82013-03-05 17:47:11 -0800533// ---------------------------------------------------------------------------
534// drawing...
535// ---------------------------------------------------------------------------
536
Lloyd Piquef16688f2019-02-19 17:47:57 -0800537std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
538 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
539 if (!getCompositionLayer()) {
540 return {};
541 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800542
Vishnu Nair4351ad52019-02-11 14:13:02 -0800543 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000544 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800545 renderengine::LayerSettings layerSettings;
546 layerSettings.geometry.boundaries = bounds;
547 if (targetSettings.useIdentityTransform) {
548 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000549 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800550 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000551 }
552
553 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800554 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000555 }
556
557 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800558 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
559 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000560
Lloyd Piquef16688f2019-02-19 17:47:57 -0800561 layerSettings.alpha = alpha;
562 layerSettings.sourceDataspace = mCurrentDataSpace;
563 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800564}
565
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800566Hwc2::IComposerClient::Composition Layer::getCompositionType(
567 const sp<const DisplayDevice>& display) const {
568 const auto outputLayer = findOutputLayerForDisplay(display);
569 LOG_FATAL_IF(!outputLayer);
570 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
571 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800572}
573
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800574bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
575 const auto outputLayer = findOutputLayerForDisplay(display);
576 LOG_FATAL_IF(!outputLayer);
577 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800578}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579
Dan Stozacac35382016-01-27 12:21:06 -0800580bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
581 if (point->getFrameNumber() <= mCurrentFrameNumber) {
582 // Don't bother with a SyncPoint, since we've already latched the
583 // relevant frame
584 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700585 }
Robert Carr2e102c92018-10-23 12:11:15 -0700586 if (isRemovedFromCurrentState()) {
587 return false;
588 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700589
Dan Stozacac35382016-01-27 12:21:06 -0800590 Mutex::Autolock lock(mLocalSyncPointMutex);
591 mLocalSyncPoints.push_back(point);
592 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700593}
594
Mathias Agopian13127d82013-03-05 17:47:11 -0800595// ----------------------------------------------------------------------------
596// local state
597// ----------------------------------------------------------------------------
598
David Sodman41fdfc92017-11-06 16:09:56 -0800599bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800600 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700601 return (s.flags & layer_state_t::eLayerSecure);
602}
603
Mathias Agopian13127d82013-03-05 17:47:11 -0800604void Layer::setVisibleRegion(const Region& visibleRegion) {
605 // always called from main thread
606 this->visibleRegion = visibleRegion;
607}
608
609void Layer::setCoveredRegion(const Region& coveredRegion) {
610 // always called from main thread
611 this->coveredRegion = coveredRegion;
612}
613
David Sodman41fdfc92017-11-06 16:09:56 -0800614void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800615 // always called from main thread
616 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
617}
618
Robert Carre5f4f692018-01-12 13:12:28 -0800619void Layer::clearVisibilityRegions() {
620 visibleRegion.clear();
621 visibleNonTransparentRegion.clear();
622 coveredRegion.clear();
623}
624
Mathias Agopian13127d82013-03-05 17:47:11 -0800625// ----------------------------------------------------------------------------
626// transaction
627// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800628
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800629void Layer::pushPendingState() {
630 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700631 return;
632 }
Alec Mourie60041e2019-06-14 18:59:51 -0700633 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700634
Dan Stoza7dde5992015-05-22 09:51:44 -0700635 // If this transaction is waiting on the receipt of a frame, generate a sync
636 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700637 // We don't allow installing sync points after we are removed from the current state
638 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800639 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
640 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800641 if (barrierLayer == nullptr) {
642 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700643 // If we can't promote the layer we are intended to wait on,
644 // then it is expired or otherwise invalid. Allow this transaction
645 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800646 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800647 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700648 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800649 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700650 std::stringstream ss;
651 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
652 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800653 mRemoteSyncPoints.push_back(std::move(syncPoint));
654 } else {
655 // We already missed the frame we're supposed to synchronize
656 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800657 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800658 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700659 }
660
Dan Stoza7dde5992015-05-22 09:51:44 -0700661 // Wake us up to check if the frame has been received
662 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700663 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700664 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800665 mPendingStates.push_back(mCurrentState);
666 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700667}
668
Pablo Ceballos05289c22016-04-14 15:49:55 -0700669void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700670 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800671 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700672
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800673 mPendingStates.removeAt(0);
674 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700675}
676
Pablo Ceballos05289c22016-04-14 15:49:55 -0700677bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700678 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800679 while (!mPendingStates.empty()) {
680 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700681 if (mRemoteSyncPoints.empty()) {
682 // If we don't have a sync point for this, apply it anyway. It
683 // will be visually wrong, but it should keep us from getting
684 // into too much trouble.
685 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700686 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 stateUpdateAvailable = true;
688 continue;
689 }
690
Marissa Wallf58c14b2018-07-24 10:50:43 -0700691 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800692 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800693 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800694
695 // Signal our end of the sync point and then dispose of it
696 mRemoteSyncPoints.front()->setTransactionApplied();
697 mRemoteSyncPoints.pop_front();
698 continue;
699 }
700
Dan Stoza7dde5992015-05-22 09:51:44 -0700701 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700702 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700703 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700704 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700705 stateUpdateAvailable = true;
706
707 // Signal our end of the sync point and then dispose of it
708 mRemoteSyncPoints.front()->setTransactionApplied();
709 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800710 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700711 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800712 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700713 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700714 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700715 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700716 stateUpdateAvailable = true;
717 }
718 }
719
720 // If we still have pending updates, wake SurfaceFlinger back up and point
721 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800722 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700723 setTransactionFlags(eTransactionNeeded);
724 mFlinger->setTransactionFlags(eTraversalNeeded);
725 }
726
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800727 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700728 return stateUpdateAvailable;
729}
730
Marissa Wall61c58622018-07-18 10:12:20 -0700731uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000732 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733
Marissa Wall61c58622018-07-18 10:12:20 -0700734 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
735 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700736
David Sodmaneb085e02017-10-05 18:49:04 -0700737 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700738 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000739 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800740 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
741 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
742 " requested={ wh={%4u,%4u} }}\n"
743 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
744 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700745 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700746 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
747 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
748 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
749 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
750 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700751 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
752 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
753 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700755
Robert Carre392b552017-09-19 12:16:05 -0700756 // Don't let Layer::doTransaction update the drawing state
757 // if we have a pending resize, unless we are in fixed-size mode.
758 // the drawing state will be updated only once we receive a buffer
759 // with the correct size.
760 //
761 // In particular, we want to make sure the clip (which is part
762 // of the geometry state) is latched together with the size but is
763 // latched immediately when no resizing is involved.
764 //
765 // If a sideband stream is attached, however, we want to skip this
766 // optimization so that transactions aren't missed when a buffer
767 // never arrives
768 //
769 // In the case that we don't have a buffer we ignore other factors
770 // and avoid entering the resizePending state. At a high level the
771 // resizePending state is to avoid applying the state of the new buffer
772 // to the old buffer. However in the state where we don't have an old buffer
773 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700774 const bool resizePending =
775 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
776 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800777 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700778 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800779 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700780 flags |= eDontUpdateGeometryState;
781 }
782 }
783
Robert Carr7bf247e2017-05-18 14:02:49 -0700784 // Here we apply various requested geometry states, depending on our
785 // latching configuration. See Layer.h for a detailed discussion of
786 // how geometry latching is controlled.
787 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000788 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700789
790 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
791 // mode, which causes attributes which normally latch regardless of scaling mode,
792 // to be delayed. We copy the requested state to the active state making sure
793 // to respect these rules (again see Layer.h for a detailed discussion).
794 //
795 // There is an awkward asymmetry in the handling of the crop states in the position
796 // states, as can be seen below. Largely this arises from position and transform
797 // being stored in the same data structure while having different latching rules.
798 // b/38182305
799 //
Marissa Wall61c58622018-07-18 10:12:20 -0700800 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700801 // applyPendingStates in the presence of deferred transactions.
802 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700803 float tx = stateToCommit->active_legacy.transform.tx();
804 float ty = stateToCommit->active_legacy.transform.ty();
805 stateToCommit->active_legacy = stateToCommit->requested_legacy;
806 stateToCommit->active_legacy.transform.set(tx, ty);
807 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700808 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700809 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700810 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700811 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800812 }
813
Marissa Wall61c58622018-07-18 10:12:20 -0700814 return flags;
815}
816
817uint32_t Layer::doTransaction(uint32_t flags) {
818 ATRACE_CALL();
819
chaviw5aedec92018-10-22 10:40:38 -0700820 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800821 return flags;
822 }
823
824 if (mChildrenChanged) {
825 flags |= eVisibleRegion;
826 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700827 }
828
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800829 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000830 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700831 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800832 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700833 }
834
835 flags = doTransactionResize(flags, &c);
836
Alec Mourib416efd2018-09-06 21:01:59 +0000837 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700838
839 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800840 // invalidate and recompute the visible regions if needed
841 flags |= Layer::eVisibleRegion;
842 }
843
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700844 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800845 // invalidate and recompute the visible regions if needed
846 flags |= eVisibleRegion;
847 this->contentDirty = true;
848
849 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700850 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700851 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800852 }
853
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800854 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700855 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800856 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700857 }
858
Mathias Agopian13127d82013-03-05 17:47:11 -0800859 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700860 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700861 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800862 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800863 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864}
865
Pablo Ceballos05289c22016-04-14 15:49:55 -0700866void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800867 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700868}
869
Mathias Agopian13127d82013-03-05 17:47:11 -0800870uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800871 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800872}
873
874uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800875 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800876}
877
Robert Carr82364e32016-05-15 11:27:47 -0700878bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800879 if (mCurrentState.requested_legacy.transform.tx() == x &&
880 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800881 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800882 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700883
884 // We update the requested and active position simultaneously because
885 // we want to apply the position portion of the transform matrix immediately,
886 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800887 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700888 if (immediate && !mFreezeGeometryUpdates) {
889 // Here we directly update the active state
890 // unlike other setters, because we store it within
891 // the transform, but use different latching rules.
892 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800893 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700894 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700895 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700896
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800898 setTransactionFlags(eTransactionNeeded);
899 return true;
900}
Robert Carr82364e32016-05-15 11:27:47 -0700901
Robert Carr1f0a16a2016-10-24 16:27:39 -0700902bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
903 ssize_t idx = mCurrentChildren.indexOf(childLayer);
904 if (idx < 0) {
905 return false;
906 }
907 if (childLayer->setLayer(z)) {
908 mCurrentChildren.removeAt(idx);
909 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800910 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700911 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800912 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700913}
914
Robert Carr503c7042017-09-27 15:06:08 -0700915bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
916 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
917 ssize_t idx = mCurrentChildren.indexOf(childLayer);
918 if (idx < 0) {
919 return false;
920 }
921 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
922 mCurrentChildren.removeAt(idx);
923 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800924 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700925 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800926 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700927}
928
Robert Carrae060832016-11-28 10:51:00 -0800929bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800930 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
931 mCurrentState.sequence++;
932 mCurrentState.z = z;
933 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700934
935 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800936 if (mCurrentState.zOrderRelativeOf != nullptr) {
937 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700938 if (strongRelative != nullptr) {
939 strongRelative->removeZOrderRelative(this);
940 }
chaviw606e5cf2019-03-01 10:12:10 -0800941 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700942 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800943 setTransactionFlags(eTransactionNeeded);
944 return true;
945}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700946
Robert Carrdb66e622017-04-10 16:55:57 -0700947void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800948 mCurrentState.zOrderRelatives.remove(relative);
949 mCurrentState.sequence++;
950 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700951 setTransactionFlags(eTransactionNeeded);
952}
953
954void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800955 mCurrentState.zOrderRelatives.add(relative);
956 mCurrentState.modified = true;
957 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700958 setTransactionFlags(eTransactionNeeded);
959}
960
chaviw606e5cf2019-03-01 10:12:10 -0800961void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
962 mCurrentState.zOrderRelativeOf = relativeOf;
963 mCurrentState.sequence++;
964 mCurrentState.modified = true;
965 setTransactionFlags(eTransactionNeeded);
966}
967
Robert Carr503d2bd2017-12-04 15:49:47 -0800968bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700969 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
970 if (handle == nullptr) {
971 return false;
972 }
973 sp<Layer> relative = handle->owner.promote();
974 if (relative == nullptr) {
975 return false;
976 }
977
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800978 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
979 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800980 return false;
981 }
982
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800983 mCurrentState.sequence++;
984 mCurrentState.modified = true;
985 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700986
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800987 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700988 if (oldZOrderRelativeOf != nullptr) {
989 oldZOrderRelativeOf->removeZOrderRelative(this);
990 }
chaviw606e5cf2019-03-01 10:12:10 -0800991 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700992 relative->addZOrderRelative(this);
993
994 setTransactionFlags(eTransactionNeeded);
995
996 return true;
997}
998
Mathias Agopian13127d82013-03-05 17:47:11 -0800999bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001000 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001001 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001002 mCurrentState.requested_legacy.w = w;
1003 mCurrentState.requested_legacy.h = h;
1004 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001005 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001006
1007 // record the new size, from this point on, when the client request
1008 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001009 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001010 return true;
1011}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001012bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001013 if (mCurrentState.color.a == alpha) return false;
1014 mCurrentState.sequence++;
1015 mCurrentState.color.a = alpha;
1016 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001017 setTransactionFlags(eTransactionNeeded);
1018 return true;
1019}
chaviw13fdc492017-06-27 12:40:18 -07001020
Valerie Haudd0b7572019-01-29 14:59:27 -08001021bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1022 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001023 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001024 }
1025 mCurrentState.sequence++;
1026 mCurrentState.modified = true;
1027 setTransactionFlags(eTransactionNeeded);
1028
1029 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001030 // create background color layer if one does not yet exist
1031 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1032 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001033 mCurrentState.bgColorLayer = new ColorLayer(
1034 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001035
Valerie Haudd0b7572019-01-29 14:59:27 -08001036 // add to child list
1037 addChild(mCurrentState.bgColorLayer);
1038 mFlinger->mLayersAdded = true;
1039 // set up SF to handle added color layer
1040 if (isRemovedFromCurrentState()) {
1041 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1042 }
1043 mFlinger->setTransactionFlags(eTransactionNeeded);
1044 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1045 mCurrentState.bgColorLayer->reparent(nullptr);
1046 mCurrentState.bgColorLayer = nullptr;
1047 return true;
1048 }
1049
1050 mCurrentState.bgColorLayer->setColor(color);
1051 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1052 mCurrentState.bgColorLayer->setAlpha(alpha);
1053 mCurrentState.bgColorLayer->setDataspace(dataspace);
1054
chaviw13fdc492017-06-27 12:40:18 -07001055 return true;
1056}
1057
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001058bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001059 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001060
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001061 mCurrentState.sequence++;
1062 mCurrentState.cornerRadius = cornerRadius;
1063 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001064 setTransactionFlags(eTransactionNeeded);
1065 return true;
1066}
1067
Robert Carrd4ae7f32018-06-07 16:10:57 -07001068bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1069 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001070 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001071 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1072
1073 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1074 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1075 return false;
1076 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001077 mCurrentState.sequence++;
1078 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1079 matrix.dsdy);
1080 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 setTransactionFlags(eTransactionNeeded);
1082 return true;
1083}
Marissa Wall61c58622018-07-18 10:12:20 -07001084
Mathias Agopian13127d82013-03-05 17:47:11 -08001085bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 mCurrentState.requestedTransparentRegion_legacy = transparent;
1087 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001088 setTransactionFlags(eTransactionNeeded);
1089 return true;
1090}
1091bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1093 if (mCurrentState.flags == newFlags) return false;
1094 mCurrentState.sequence++;
1095 mCurrentState.flags = newFlags;
1096 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001097 setTransactionFlags(eTransactionNeeded);
1098 return true;
1099}
Robert Carr99e27f02016-06-16 15:18:02 -07001100
Marissa Wallf58c14b2018-07-24 10:50:43 -07001101bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001102 if (mCurrentState.requestedCrop_legacy == crop) return false;
1103 mCurrentState.sequence++;
1104 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001105 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001106 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001107 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001108 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1109
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001110 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001111 setTransactionFlags(eTransactionNeeded);
1112 return true;
1113}
Robert Carr8d5227b2017-03-16 15:41:03 -07001114
Robert Carrc3574f72016-03-24 12:19:32 -07001115bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001116 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001117 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001118 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001119 return true;
1120}
1121
Evan Roskyef876c92019-01-25 17:46:06 -08001122bool Layer::setMetadata(const LayerMetadata& data) {
1123 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001124 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001125 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001126 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001127 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001128}
1129
Mathias Agopian13127d82013-03-05 17:47:11 -08001130bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001131 if (mCurrentState.layerStack == layerStack) return false;
1132 mCurrentState.sequence++;
1133 mCurrentState.layerStack = layerStack;
1134 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001135 setTransactionFlags(eTransactionNeeded);
1136 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001137}
1138
Peiyong Linc502cb72019-03-01 15:00:23 -08001139bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1140 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1141 return false;
1142 }
1143 mCurrentState.sequence++;
1144 mCurrentState.colorSpaceAgnostic = agnostic;
1145 mCurrentState.modified = true;
1146 setTransactionFlags(eTransactionNeeded);
1147 return true;
1148}
1149
Robert Carr1f0a16a2016-10-24 16:27:39 -07001150uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001151 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001152 if (p == nullptr) {
1153 return getDrawingState().layerStack;
1154 }
1155 return p->getLayerStack();
1156}
1157
Marissa Wallf58c14b2018-07-24 10:50:43 -07001158void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001159 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001160 mCurrentState.barrierLayer_legacy = barrierLayer;
1161 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001162 // We don't set eTransactionNeeded, because just receiving a deferral
1163 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001164 mCurrentState.modified = true;
1165 pushPendingState();
1166 mCurrentState.barrierLayer_legacy = nullptr;
1167 mCurrentState.frameNumber_legacy = 0;
1168 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001169}
1170
Marissa Wallf58c14b2018-07-24 10:50:43 -07001171void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001172 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001173 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001174}
1175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176// ----------------------------------------------------------------------------
1177// pageflip handling...
1178// ----------------------------------------------------------------------------
1179
Robert Carr1f0a16a2016-10-24 16:27:39 -07001180bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001181 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001182 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001183 if (parent != nullptr && parent->isHiddenByPolicy()) {
1184 return true;
1185 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001186 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1187 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1188 if (zOrderRelativeOf != nullptr) {
1189 if (zOrderRelativeOf->isHiddenByPolicy()) {
1190 return true;
1191 }
1192 }
1193 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001194 return s.flags & layer_state_t::eLayerHidden;
1195}
1196
David Sodman41fdfc92017-11-06 16:09:56 -08001197uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001198 // TODO: should we do something special if mSecure is set?
1199 if (mProtectedByApp) {
1200 // need a hardware-protected path to external video sink
1201 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001202 }
Riley Andrews03414a12014-07-01 14:22:59 -07001203 if (mPotentialCursor) {
1204 usage |= GraphicBuffer::USAGE_CURSOR;
1205 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001206 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001207 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001208}
1209
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001210void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001211 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001212 // Disable setting transform hint if the debug flag is set.
1213 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001214 // The transform hint is used to improve performance, but we can
1215 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001216 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001217 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001218 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001219 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001220 orientation = 0;
1221 }
1222 }
David Sodmaneb085e02017-10-05 18:49:04 -07001223 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001224}
1225
Mathias Agopian13127d82013-03-05 17:47:11 -08001226// ----------------------------------------------------------------------------
1227// debugging
1228// ----------------------------------------------------------------------------
1229
Marissa Wall61c58622018-07-18 10:12:20 -07001230// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001231LayerDebugInfo Layer::getLayerDebugInfo() const {
1232 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001233 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001234 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001235 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001236 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
chaviw8a01fa42019-08-19 12:39:31 -07001237 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001238 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001239 info.mVisibleRegion = visibleRegion;
1240 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001241 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001242 info.mX = ds.active_legacy.transform.tx();
1243 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001244 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001245 info.mWidth = ds.active_legacy.w;
1246 info.mHeight = ds.active_legacy.h;
1247 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001248 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001249 info.mFlags = ds.flags;
1250 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001251 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001252 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1253 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1254 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1255 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001256 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001257 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001258 if (buffer != 0) {
1259 info.mActiveBufferWidth = buffer->getWidth();
1260 info.mActiveBufferHeight = buffer->getHeight();
1261 info.mActiveBufferStride = buffer->getStride();
1262 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001263 } else {
1264 info.mActiveBufferWidth = 0;
1265 info.mActiveBufferHeight = 0;
1266 info.mActiveBufferStride = 0;
1267 info.mActiveBufferFormat = 0;
1268 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001270 info.mNumQueuedFrames = getQueuedFrameCount();
1271 info.mRefreshPending = isBufferLatched();
1272 info.mIsOpaque = isOpaque(ds);
1273 info.mContentDirty = contentDirty;
1274 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001275}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001276
Yiwei Zhang5434a782018-12-05 18:06:32 -08001277void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001278 result.append("-------------------------------");
1279 result.append("-------------------------------");
1280 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001281 result.append(" Layer name\n");
1282 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001283 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001284 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001285 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001286 result.append(" Disp Frame (LTRB) | ");
1287 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001288 result.append("-------------------------------");
1289 result.append("-------------------------------");
1290 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001291}
1292
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001293void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1294 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1295 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001296 return;
1297 }
1298
Yiwei Zhang5434a782018-12-05 18:06:32 -08001299 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001300 if (mName.length() > 77) {
1301 std::string shortened;
1302 shortened.append(mName.string(), 36);
1303 shortened.append("[...]");
1304 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001305 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001306 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001307 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001308 }
1309
Yiwei Zhang5434a782018-12-05 18:06:32 -08001310 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001311
Alec Mourib416efd2018-09-06 21:01:59 +00001312 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001313 const auto& compositionState = outputLayer->getState();
1314
Chia-I Wu1e043612018-03-01 09:45:09 -08001315 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001316 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001317 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001318 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001319 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001320 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001321 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1322 StringAppendF(&result, "%10s | ",
1323 toString(getCompositionLayer() ? compositionState.bufferTransform
1324 : static_cast<Hwc2::Transform>(0))
1325 .c_str());
1326 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001327 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001328 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001329 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1330 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001331
Yichi Chen6ca35192018-05-29 12:20:43 +08001332 result.append("- - - - - - - - - - - - - - - -");
1333 result.append("- - - - - - - - - - - - - - - -");
1334 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001335}
Dan Stozae22aec72016-08-01 13:20:59 -07001336
Yiwei Zhang5434a782018-12-05 18:06:32 -08001337void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001338 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001339}
1340
Svetoslavd85084b2014-03-20 10:28:31 -07001341void Layer::clearFrameStats() {
1342 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001343}
1344
Jamie Gennis6547ff42013-07-16 20:12:42 -07001345void Layer::logFrameStats() {
1346 mFrameTracker.logAndResetStats(mName);
1347}
1348
Svetoslavd85084b2014-03-20 10:28:31 -07001349void Layer::getFrameStats(FrameStats* outStats) const {
1350 mFrameTracker.getStats(outStats);
1351}
1352
Yiwei Zhang5434a782018-12-05 18:06:32 -08001353void Layer::dumpFrameEvents(std::string& result) {
chaviw8a01fa42019-08-19 12:39:31 -07001354 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001355 Mutex::Autolock lock(mFrameEventHistoryMutex);
1356 mFrameEventHistory.checkFencesForCompletion();
1357 mFrameEventHistory.dump(result);
1358}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001359
Vishnu Nair0f085c62019-08-30 08:49:12 -07001360void Layer::dumpCallingUidPid(std::string& result) const {
1361 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().string(), getType(),
1362 mCallingPid, mCallingUid);
1363}
1364
Brian Anderson5ea5e592016-12-01 16:54:33 -08001365void Layer::onDisconnect() {
1366 Mutex::Autolock lock(mFrameEventHistoryMutex);
1367 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001368 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001369}
1370
Brian Anderson3890c392016-07-25 12:48:08 -07001371void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001372 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001373 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001374 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1375 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001376 }
1377
Brian Andersond6927fb2016-07-23 23:37:30 -07001378 Mutex::Autolock lock(mFrameEventHistoryMutex);
1379 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001380 // If there are any unsignaled fences in the aquire timeline at this
1381 // point, the previously queued frame hasn't been latched yet. Go ahead
1382 // and try to get the signal time here so the syscall is taken out of
1383 // the main thread's critical path.
1384 mAcquireTimeline.updateSignalTimes();
1385 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001386 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001387 mFrameEventHistory.addQueue(*newTimestamps);
1388 }
1389
Brian Anderson3890c392016-07-25 12:48:08 -07001390 if (outDelta) {
1391 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001392 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001393}
Dan Stozae77c7662016-05-13 11:37:28 -07001394
Chia-I Wu98f1c102017-05-30 14:54:08 -07001395size_t Layer::getChildrenCount() const {
1396 size_t count = 0;
1397 for (const sp<Layer>& child : mCurrentChildren) {
1398 count += 1 + child->getChildrenCount();
1399 }
1400 return count;
1401}
1402
Robert Carr1f0a16a2016-10-24 16:27:39 -07001403void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001404 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001405 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001406
Robert Carr1f0a16a2016-10-24 16:27:39 -07001407 mCurrentChildren.add(layer);
1408 layer->setParent(this);
1409}
1410
1411ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001412 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001413 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001414
Robert Carr1323c952019-01-28 18:13:27 -08001415 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001416 return mCurrentChildren.remove(layer);
1417}
1418
Robert Carr1db73f62016-12-21 12:58:51 -08001419bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1420 sp<Handle> handle = nullptr;
1421 sp<Layer> newParent = nullptr;
1422 if (newParentHandle == nullptr) {
1423 return false;
1424 }
1425 handle = static_cast<Handle*>(newParentHandle.get());
1426 newParent = handle->owner.promote();
1427 if (newParent == nullptr) {
1428 ALOGE("Unable to promote Layer handle");
1429 return false;
1430 }
1431
chaviw5aedec92018-10-22 10:40:38 -07001432 if (attachChildren()) {
1433 setTransactionFlags(eTransactionNeeded);
1434 }
Robert Carr1db73f62016-12-21 12:58:51 -08001435 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001436 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001437 }
1438 mCurrentChildren.clear();
1439
1440 return true;
1441}
1442
Robert Carr15eae092018-03-23 13:43:53 -07001443void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001444 for (const sp<Layer>& child : mDrawingChildren) {
1445 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001446 child->computeBounds(newParent->mBounds,
1447 newParent->getTransformWithScale(
1448 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001449 }
1450}
1451
chaviwf1961f72017-09-18 16:41:07 -07001452bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001453 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001454
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001455 // While layers are detached, we allow most operations
1456 // and simply halt performing the actual transaction. However
1457 // for reparent != null we would enter the mRemovedFromCurrentState
1458 // state, regardless of whether doTransaction was called, and
1459 // so we need to prevent the update here.
1460 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001461 return false;
1462 }
1463
Robert Carr54cf5b12019-01-25 14:02:28 -08001464 sp<Layer> newParent;
1465 if (newParentHandle != nullptr) {
1466 auto handle = static_cast<Handle*>(newParentHandle.get());
1467 newParent = handle->owner.promote();
1468 if (newParent == nullptr) {
1469 ALOGE("Unable to promote Layer handle");
1470 return false;
1471 }
1472 if (newParent == this) {
1473 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1474 return false;
1475 }
1476 }
1477
chaviwf1961f72017-09-18 16:41:07 -07001478 sp<Layer> parent = getParent();
1479 if (parent != nullptr) {
1480 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001481 }
1482
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001483 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001484 newParent->addChild(this);
1485 if (!newParent->isRemovedFromCurrentState()) {
1486 addToCurrentState();
1487 } else {
1488 onRemovedFromCurrentState();
1489 }
1490
1491 if (mLayerDetached) {
1492 mLayerDetached = false;
1493 callSetTransactionFlags = true;
1494 }
1495 } else {
1496 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001497 }
1498
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001499 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001500 setTransactionFlags(eTransactionNeeded);
1501 }
chaviw06178942017-07-27 10:25:59 -07001502 return true;
1503}
1504
Robert Carr9524cb32017-02-13 11:32:32 -08001505bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001506 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001507 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001508 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001509 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001510 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001511 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001512 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001513 }
Robert Carr7f619b22017-11-06 12:56:35 -08001514 }
Robert Carr9524cb32017-02-13 11:32:32 -08001515
1516 return true;
1517}
1518
chaviw5aedec92018-10-22 10:40:38 -07001519bool Layer::attachChildren() {
1520 bool changed = false;
1521 for (const sp<Layer>& child : mCurrentChildren) {
1522 sp<Client> parentClient = mClientRef.promote();
1523 sp<Client> client(child->mClientRef.promote());
1524 if (client != nullptr && parentClient != client) {
1525 if (child->mLayerDetached) {
1526 child->mLayerDetached = false;
1527 changed = true;
1528 }
1529 changed |= child->attachChildren();
1530 }
1531 }
1532
1533 return changed;
1534}
1535
Peiyong Lind3788632018-09-18 16:01:31 -07001536bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001537 static const mat4 identityMatrix = mat4();
1538
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001539 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001540 return false;
1541 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001542 ++mCurrentState.sequence;
1543 mCurrentState.colorTransform = matrix;
1544 mCurrentState.hasColorTransform = matrix != identityMatrix;
1545 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001546 setTransactionFlags(eTransactionNeeded);
1547 return true;
1548}
1549
chaviwf66724d2018-11-28 16:35:21 -08001550mat4 Layer::getColorTransform() const {
1551 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1552 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1553 colorTransform = parent->getColorTransform() * colorTransform;
1554 }
1555 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001556}
1557
1558bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001559 bool hasColorTransform = getDrawingState().hasColorTransform;
1560 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1561 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1562 }
1563 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001564}
1565
Chia-I Wu11481472018-05-04 10:43:19 -07001566bool Layer::isLegacyDataSpace() const {
1567 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001568 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001569 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001570}
1571
Robert Carr1f0a16a2016-10-24 16:27:39 -07001572void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001573 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001574}
1575
Robert Carr1f0a16a2016-10-24 16:27:39 -07001576int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001577 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001578}
1579
Robert Carr1c5481e2019-07-01 14:42:27 -07001580bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001581 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001582 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001583 return state.zOrderRelativeOf != nullptr;
1584}
1585
David Sodman41fdfc92017-11-06 16:09:56 -08001586__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001587 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001588 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1589 "makeTraversalList received invalid stateSet");
1590 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1591 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001592 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001593
Robert Carr29abff82017-12-04 13:51:20 -08001594 if (state.zOrderRelatives.size() == 0) {
1595 *outSkipRelativeZUsers = true;
1596 return children;
1597 }
1598
chaviwfd462612018-05-31 16:11:27 -07001599 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001600 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001601 sp<Layer> strongRelative = weakRelative.promote();
1602 if (strongRelative != nullptr) {
1603 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001604 }
1605 }
1606
Dan Stoza412903f2017-04-27 13:42:17 -07001607 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001608 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001609 if (childState.zOrderRelativeOf != nullptr) {
1610 continue;
1611 }
Robert Carrdb66e622017-04-10 16:55:57 -07001612 traverse.add(child);
1613 }
1614
1615 return traverse;
1616}
1617
Robert Carr1f0a16a2016-10-24 16:27:39 -07001618/**
Robert Carrdb66e622017-04-10 16:55:57 -07001619 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620 */
Dan Stoza412903f2017-04-27 13:42:17 -07001621void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001622 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1623 // produce a new list for traversing, including our relatives, and not including our children
1624 // who are relatives of another surface. In the case that there are no relative Z,
1625 // makeTraversalList returns our children directly to avoid significant overhead.
1626 // However in this case we need to take the responsibility for filtering children which
1627 // are relatives of another surface here.
1628 bool skipRelativeZUsers = false;
1629 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001630
Robert Carr1f0a16a2016-10-24 16:27:39 -07001631 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001632 for (; i < list.size(); i++) {
1633 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001634 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1635 continue;
1636 }
1637
Robert Carrdb66e622017-04-10 16:55:57 -07001638 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001640 }
Dan Stoza412903f2017-04-27 13:42:17 -07001641 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642 }
Robert Carr29abff82017-12-04 13:51:20 -08001643
Dan Stoza412903f2017-04-27 13:42:17 -07001644 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001645 for (; i < list.size(); i++) {
1646 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001647
Robert Carr29abff82017-12-04 13:51:20 -08001648 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1649 continue;
1650 }
Dan Stoza412903f2017-04-27 13:42:17 -07001651 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001652 }
1653}
1654
1655/**
Robert Carrdb66e622017-04-10 16:55:57 -07001656 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657 */
Dan Stoza412903f2017-04-27 13:42:17 -07001658void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1659 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001660 // See traverseInZOrder for documentation.
1661 bool skipRelativeZUsers = false;
1662 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001663
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001665 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001666 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001667
1668 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1669 continue;
1670 }
1671
Robert Carrdb66e622017-04-10 16:55:57 -07001672 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673 break;
1674 }
Dan Stoza412903f2017-04-27 13:42:17 -07001675 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 }
Dan Stoza412903f2017-04-27 13:42:17 -07001677 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001678 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001679 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001680
1681 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1682 continue;
1683 }
1684
Dan Stoza412903f2017-04-27 13:42:17 -07001685 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001686 }
1687}
1688
chaviw4b129c22018-04-09 16:19:43 -07001689LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1690 const std::vector<Layer*>& layersInTree) {
1691 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1692 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001693 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1694 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001695 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001696
chaviwfd462612018-05-31 16:11:27 -07001697 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001698 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1699 sp<Layer> strongRelative = weakRelative.promote();
1700 // Only add relative layers that are also descendents of the top most parent of the tree.
1701 // If a relative layer is not a descendent, then it should be ignored.
1702 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1703 traverse.add(strongRelative);
1704 }
1705 }
1706
1707 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001708 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001709 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1710 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1711 // the child here since it won't be added later as a relative.
1712 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1713 childState.zOrderRelativeOf.promote().get())) {
1714 continue;
1715 }
1716 traverse.add(child);
1717 }
1718
1719 return traverse;
1720}
1721
1722void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1723 LayerVector::StateSet stateSet,
1724 const LayerVector::Visitor& visitor) {
1725 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001726
1727 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001728 for (; i < list.size(); i++) {
1729 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001730 if (relative->getZ() >= 0) {
1731 break;
1732 }
chaviw4b129c22018-04-09 16:19:43 -07001733 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001734 }
chaviw4b129c22018-04-09 16:19:43 -07001735
chaviwa76b2712017-09-20 12:02:26 -07001736 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001737 for (; i < list.size(); i++) {
1738 const auto& relative = list[i];
1739 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001740 }
1741}
1742
chaviw4b129c22018-04-09 16:19:43 -07001743std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1744 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1745 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1746
1747 std::vector<Layer*> layersInTree = {this};
1748 for (size_t i = 0; i < children.size(); i++) {
1749 const auto& child = children[i];
1750 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1751 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1752 }
1753
1754 return layersInTree;
1755}
1756
1757void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1758 const LayerVector::Visitor& visitor) {
1759 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1760 std::sort(layersInTree.begin(), layersInTree.end());
1761 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1762}
1763
Peiyong Linefefaac2018-08-17 12:27:51 -07001764ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001765 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766}
1767
chaviw13fdc492017-06-27 12:40:18 -07001768half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001769 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001770
chaviw13fdc492017-06-27 12:40:18 -07001771 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1772 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001773}
Robert Carr6452f122017-03-21 10:41:29 -07001774
chaviw13fdc492017-06-27 12:40:18 -07001775half4 Layer::getColor() const {
1776 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001777 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001778}
Robert Carr6452f122017-03-21 10:41:29 -07001779
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001780Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1781 const auto& p = mDrawingParent.promote();
1782 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001783 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001784 if (parentState.radius > 0) {
1785 ui::Transform t = getActiveTransform(getDrawingState());
1786 t = t.inverse();
1787 parentState.cropRect = t.transform(parentState.cropRect);
1788 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1789 // but a transform matrix can define horizontal and vertical scales.
1790 // Let's take the average between both of them and pass into the shader, practically we
1791 // never do this type of transformation on windows anyway.
1792 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1793 return parentState;
1794 }
1795 }
1796 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001797 return radius > 0 && getCrop(getDrawingState()).isValid()
1798 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1799 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001800}
1801
Robert Carr1f0a16a2016-10-24 16:27:39 -07001802void Layer::commitChildList() {
1803 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1804 const auto& child = mCurrentChildren[i];
1805 child->commitChildList();
1806 }
1807 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001808 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809}
1810
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001811static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1812 if (weakBinderHandle == nullptr) {
1813 return nullptr;
1814 }
1815 sp<IBinder> binderHandle = weakBinderHandle.promote();
1816 if (binderHandle == nullptr) {
1817 return nullptr;
1818 }
1819 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1820 if (handle == nullptr) {
1821 return nullptr;
1822 }
1823 return handle->owner;
1824}
1825
Robert Carr720e5062018-07-30 17:45:14 -07001826void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001827 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001828 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001829 mCurrentState.modified = true;
1830 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001831 setTransactionFlags(eTransactionNeeded);
1832}
1833
Vishnu Nair8406fd72019-07-30 11:29:31 -07001834void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1835 ui::Transform transform = getTransform();
1836
1837 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1838 for (const auto& pendingState : mPendingStatesSnapshot) {
1839 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1840 if (barrierLayer != nullptr) {
1841 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1842 barrierLayerProto->set_id(barrierLayer->sequence);
1843 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1844 }
1845 }
1846
1847 auto buffer = mActiveBuffer;
1848 if (buffer != nullptr) {
1849 LayerProtoHelper::writeToProto(buffer,
1850 [&]() { return layerInfo->mutable_active_buffer(); });
1851 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1852 layerInfo->mutable_buffer_transform());
1853 }
1854 layerInfo->set_invalidate(contentDirty);
1855 layerInfo->set_is_protected(isProtected());
1856 layerInfo->set_dataspace(
1857 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1858 layerInfo->set_queued_frames(getQueuedFrameCount());
1859 layerInfo->set_refresh_pending(isBufferLatched());
1860 layerInfo->set_curr_frame(mCurrentFrameNumber);
1861 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1862
1863 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1864 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1865 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1866 [&]() { return layerInfo->mutable_position(); });
1867 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
1868 LayerProtoHelper::writeToProto(visibleRegion,
1869 [&]() { return layerInfo->mutable_visible_region(); });
1870 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1871 [&]() { return layerInfo->mutable_damage_region(); });
1872 }
1873
1874 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1875 LayerProtoHelper::writeToProto(mSourceBounds,
1876 [&]() { return layerInfo->mutable_source_bounds(); });
1877 LayerProtoHelper::writeToProto(mScreenBounds,
1878 [&]() { return layerInfo->mutable_screen_bounds(); });
1879 }
1880}
1881
1882void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1883 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001884 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1885 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001886 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001887
Peiyong Linefefaac2018-08-17 12:27:51 -07001888 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001889
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001890 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1891 layerInfo->set_id(sequence);
1892 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001893 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001894
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001895 for (const auto& child : children) {
1896 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001897 }
chaviw1d044282017-09-27 12:19:28 -07001898
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001899 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1900 sp<Layer> strongRelative = weakRelative.promote();
1901 if (strongRelative != nullptr) {
1902 layerInfo->add_relatives(strongRelative->sequence);
1903 }
chaviwadc40c22018-07-10 16:57:27 -07001904 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001905
1906 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1907 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001908
1909 layerInfo->set_layer_stack(getLayerStack());
1910 layerInfo->set_z(state.z);
1911
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001912 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1913 [&]() {
1914 return layerInfo->mutable_requested_position();
1915 });
1916
1917 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1918 [&]() { return layerInfo->mutable_size(); });
1919
1920 LayerProtoHelper::writeToProto(state.crop_legacy,
1921 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001922
1923 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001924
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001925
1926 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1927 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1928 LayerProtoHelper::writeToProto(state.color,
1929 [&]() { return layerInfo->mutable_requested_color(); });
1930 layerInfo->set_flags(state.flags);
1931
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001932 LayerProtoHelper::writeToProto(requestedTransform,
1933 layerInfo->mutable_requested_transform());
1934
1935 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1936 if (parent != nullptr) {
1937 layerInfo->set_parent(parent->sequence);
1938 } else {
1939 layerInfo->set_parent(-1);
1940 }
1941
1942 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1943 if (zOrderRelativeOf != nullptr) {
1944 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1945 } else {
1946 layerInfo->set_z_order_relative_of(-1);
1947 }
chaviwadc40c22018-07-10 16:57:27 -07001948 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001949
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001950 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1951 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1952 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001953 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001954
1955 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1956 auto protoMap = layerInfo->mutable_metadata();
1957 for (const auto& entry : state.metadata.mMap) {
1958 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1959 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001960 }
chaviw1d044282017-09-27 12:19:28 -07001961}
1962
Robert Carr2e102c92018-10-23 12:11:15 -07001963bool Layer::isRemovedFromCurrentState() const {
1964 return mRemovedFromCurrentState;
1965}
1966
Arthur Hungd20b2702019-01-14 18:16:16 +08001967InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001968 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001969
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001970 if (info.displayId == ADISPLAY_ID_NONE) {
1971 info.displayId = mDrawingState.layerStack;
1972 }
1973
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001974 ui::Transform t = getTransform();
1975 const float xScale = t.sx();
1976 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001977 int32_t xSurfaceInset = info.surfaceInset;
1978 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001979 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001980 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1981 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001982 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001983 xSurfaceInset = std::round(xSurfaceInset * xScale);
1984 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001985 }
Robert Carre07e1032018-11-26 12:55:53 -08001986
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001987 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001988 // If this is a portal window, set the touchableRegion to the layerBounds.
1989 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1990 ? getBufferSize(getDrawingState())
1991 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001992 if (!layerBounds.isValid()) {
1993 layerBounds = getCroppedBufferSize(getDrawingState());
1994 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001995 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001996
1997 // clamp inset to layer bounds
1998 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1999 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2000
Arthur Hung118b1142019-05-08 21:25:59 +08002001 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002002
Arthur Hungd20b2702019-01-14 18:16:16 +08002003 // Input coordinate should match the layer bounds.
2004 info.frameLeft = layerBounds.left;
2005 info.frameTop = layerBounds.top;
2006 info.frameRight = layerBounds.right;
2007 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002008
2009 // Position the touchable region relative to frame screen location and restrict it to frame
2010 // bounds.
2011 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002012 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002013
2014 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2015 if (info.replaceTouchableRegionWithCrop) {
2016 if (cropLayer == nullptr) {
2017 info.touchableRegion = Region(Rect{mScreenBounds});
2018 } else {
2019 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2020 }
2021 } else if (cropLayer != nullptr) {
2022 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2023 }
2024
Robert Carr720e5062018-07-30 17:45:14 -07002025 return info;
2026}
2027
2028bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002029 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002030}
2031
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002032std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2033 return nullptr;
2034}
2035
chaviw3e727cd2019-01-31 13:41:05 -08002036bool Layer::canReceiveInput() const {
2037 return isVisible();
2038}
2039
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002040compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2041 const sp<const DisplayDevice>& display) const {
2042 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2043}
2044
Mathias Agopian13127d82013-03-05 17:47:11 -08002045// ---------------------------------------------------------------------------
2046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002047}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002048
2049#if defined(__gl_h_)
2050#error "don't include gl/gl.h in this file"
2051#endif
2052
2053#if defined(__gl2_h_)
2054#error "don't include gl2/gl2.h in this file"
2055#endif