blob: 414c8dd23d1d1ed2c36572497b37f063b3510855 [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>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080025#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080026#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080027#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080029#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070031#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <gui/BufferItem.h>
35#include <gui/LayerDebugInfo.h>
36#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070037#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070039#include <stdint.h>
40#include <stdlib.h>
41#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Alec Mourie60041e2019-06-14 18:59:51 -070051#include <algorithm>
52#include <mutex>
53#include <sstream>
54
Yiwei Zhang60d1a192018-03-07 14:52:28 -080055#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080056#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070058#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080059#include "DisplayHardware/HWComposer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080060#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070061#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070062#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080064#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070065
David Sodman41fdfc92017-11-06 16:09:56 -080066#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068namespace android {
69
Yiwei Zhang5434a782018-12-05 18:06:32 -080070using base::StringAppendF;
71
Lloyd Piquef1c675b2018-09-12 20:45:39 -070072std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080073
Lloyd Pique42ab75e2018-09-12 20:46:03 -070074Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070075 : mFlinger(args.flinger),
76 mName(args.name),
77 mClientRef(args.client),
78 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
81 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070082 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
83 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
84 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Dan Stozaf7ba41a2017-05-10 15:11:11 -070086 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087
Lloyd Pique0449b0f2018-12-20 16:23:45 -080088 mCurrentState.active_legacy.w = args.w;
89 mCurrentState.active_legacy.h = args.h;
90 mCurrentState.flags = layerFlags;
91 mCurrentState.active_legacy.transform.set(0, 0);
92 mCurrentState.crop_legacy.makeInvalid();
93 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
94 mCurrentState.z = 0;
95 mCurrentState.color.a = 1.0f;
96 mCurrentState.layerStack = 0;
97 mCurrentState.sequence = 0;
98 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080099 mCurrentState.active.w = UINT32_MAX;
100 mCurrentState.active.h = UINT32_MAX;
101 mCurrentState.active.transform.set(0, 0);
102 mCurrentState.transform = 0;
103 mCurrentState.transformToDisplayInverse = false;
104 mCurrentState.crop.makeInvalid();
105 mCurrentState.acquireFence = new Fence(-1);
106 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
107 mCurrentState.hdrMetadata.validTypes = 0;
108 mCurrentState.surfaceDamageRegion.clear();
109 mCurrentState.cornerRadius = 0.0f;
110 mCurrentState.api = -1;
111 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800112 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800113 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700114
115 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800116 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700117
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700119 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800120 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200121 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700122
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700123 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Ady Abraham09bd3922019-04-08 10:44:56 -0700124
Robert Carr2e102c92018-10-23 12:11:15 -0700125 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700126}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700127
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800129 sp<Client> c(mClientRef.promote());
130 if (c != 0) {
131 c->detachLayer(this);
132 }
133
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000134 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700135 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700136}
137
Mathias Agopian13127d82013-03-05 17:47:11 -0800138// ---------------------------------------------------------------------------
139// callbacks
140// ---------------------------------------------------------------------------
141
David Sodmaneb085e02017-10-05 18:49:04 -0700142/*
143 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
144 * Layer. So, the implementation is done in BufferLayer. When called on a
145 * ColorLayer object, it's essentially a NOP.
146 */
David Sodmaneb085e02017-10-05 18:49:04 -0700147void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800148
chaviw43cb3cb2019-05-31 15:23:41 -0700149void Layer::removeRemoteSyncPoints() {
150 for (auto& point : mRemoteSyncPoints) {
151 point->setTransactionApplied();
152 }
153 mRemoteSyncPoints.clear();
154
155 {
156 Mutex::Autolock pendingStateLock(mPendingStateMutex);
157 for (State pendingState : mPendingStates) {
158 pendingState.barrierLayer_legacy = nullptr;
159 }
160 }
161}
162
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700163void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
164 if (mCurrentState.zOrderRelativeOf == nullptr) {
165 return;
166 }
Robert Carr2e102c92018-10-23 12:11:15 -0700167
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700168 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
169 if (strongRelative == nullptr) {
170 setZOrderRelativeOf(nullptr);
171 return;
172 }
173
174 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
175 strongRelative->removeZOrderRelative(this);
176 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800177 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700178 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700179}
180
181void Layer::removeFromCurrentState() {
182 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000183
Robert Carr2e102c92018-10-23 12:11:15 -0700184 // Since we are no longer reachable from CurrentState SurfaceFlinger
185 // will no longer invoke doTransaction for us, and so we will
186 // never finish applying transactions. We signal the sync point
187 // now so that another layer will not become indefinitely
188 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700189 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700190
191 {
192 Mutex::Autolock syncLock(mLocalSyncPointMutex);
193 for (auto& point : mLocalSyncPoints) {
194 point->setFrameAvailable();
195 }
196 mLocalSyncPoints.clear();
197 }
198
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800199 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700200}
Chia-I Wu38512252017-05-17 14:36:16 -0700201
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700202void Layer::onRemovedFromCurrentState() {
203 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
204 std::sort(layersInTree.begin(), layersInTree.end());
205 for (const auto& layer : layersInTree) {
206 layer->removeFromCurrentState();
207 layer->removeRelativeZ(layersInTree);
208 }
209}
210
chaviw61626f22018-11-15 16:26:27 -0800211void Layer::addToCurrentState() {
212 mRemovedFromCurrentState = false;
213
214 for (const auto& child : mCurrentChildren) {
215 child->addToCurrentState();
216 }
217}
218
Mathias Agopian13127d82013-03-05 17:47:11 -0800219// ---------------------------------------------------------------------------
220// set-up
221// ---------------------------------------------------------------------------
222
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700223const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800224 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225}
226
chaviw13fdc492017-06-27 12:40:18 -0700227bool Layer::getPremultipledAlpha() const {
228 return mPremultipliedAlpha;
229}
230
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700231sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800232 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700233 if (mGetHandleCalled) {
234 ALOGE("Get handle called twice" );
235 return nullptr;
236 }
237 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700238 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800239}
240
241// ---------------------------------------------------------------------------
242// h/w composer set-up
243// ---------------------------------------------------------------------------
244
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800245bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
246 auto outputLayer = findOutputLayerForDisplay(displayDevice);
247 LOG_FATAL_IF(!outputLayer);
248 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
249}
250
251HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
252 auto outputLayer = findOutputLayerForDisplay(displayDevice);
253 if (!outputLayer || !outputLayer->getState().hwc) {
254 return nullptr;
255 }
256 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700257}
Steven Thomasb02664d2017-07-26 18:48:28 -0700258
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800259Rect Layer::getContentCrop() const {
260 // this is the crop rectangle that applies to the buffer
261 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700262 Rect crop;
263 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800264 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800266 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800268 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700271 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700273 return crop;
274}
275
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700276static Rect reduce(const Rect& win, const Region& exclude) {
277 if (CC_LIKELY(exclude.isEmpty())) {
278 return win;
279 }
280 if (exclude.isRect()) {
281 return win.reduce(exclude.getBounds());
282 }
283 return Region(win).subtract(exclude).getBounds();
284}
285
Dan Stoza80d61162017-12-20 15:57:52 -0800286static FloatRect reduce(const FloatRect& win, const Region& exclude) {
287 if (CC_LIKELY(exclude.isEmpty())) {
288 return win;
289 }
290 // Convert through Rect (by rounding) for lack of FloatRegion
291 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
292}
293
Vishnu Nair4351ad52019-02-11 14:13:02 -0800294Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800295 if (!reduceTransparentRegion) {
296 return Rect{mScreenBounds};
297 }
298
299 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800300 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800301 // Transform to screen space.
302 bounds = t.transform(bounds);
303 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700304}
305
Vishnu Nair4351ad52019-02-11 14:13:02 -0800306FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000307 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700309}
310
Vishnu Nairf0c28512019-02-08 12:40:28 -0800311FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
312 // Subtract the transparent region and snap to the bounds.
313 return reduce(mBounds, activeTransparentRegion);
314}
315
Vishnu Nairc652ff82019-03-15 12:48:54 -0700316ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800317 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
318 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700319 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800320 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700321 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800322 }
323
Marissa Wall290ad082019-03-06 13:23:47 -0800324 // If the layer is a buffer state layer, the active width and height
325 // could be infinite. In that case, return the effective transform.
326 const uint32_t activeWidth = getActiveWidth(getDrawingState());
327 const uint32_t activeHeight = getActiveHeight(getDrawingState());
328 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700329 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800330 }
331
Vishnu Nairc652ff82019-03-15 12:48:54 -0700332 int bufferWidth = mActiveBuffer->getWidth();
333 int bufferHeight = mActiveBuffer->getHeight();
334
335 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
336 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800337 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700338
Marissa Wall290ad082019-03-06 13:23:47 -0800339 float sx = activeWidth / static_cast<float>(bufferWidth);
340 float sy = activeHeight / static_cast<float>(bufferHeight);
341
Vishnu Nair4351ad52019-02-11 14:13:02 -0800342 ui::Transform extraParentScaling;
343 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700344 return extraParentScaling;
345}
346
347ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
348 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
349 // treat child surfaces as pixels in the parent surface.
350 if (!isFixedSize() || !mActiveBuffer) {
351 return mEffectiveTransform;
352 }
353 return mEffectiveTransform * bufferScaleTransform;
354}
355
356FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
357 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
358 // cropped to its parent layer after any buffer transform scaling is applied.
359 if (!isFixedSize() || !mActiveBuffer) {
360 return mBounds;
361 }
362 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800363}
364
365void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
366 const State& s(getDrawingState());
367
368 // Calculate effective layer transform
369 mEffectiveTransform = parentTransform * getActiveTransform(s);
370
371 // Transform parent bounds to layer space
372 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
373
Vishnu Nairc652ff82019-03-15 12:48:54 -0700374 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800375 mSourceBounds = computeSourceBounds(parentBounds);
376
377 // Calculate bounds by croping diplay frame with layer crop and parent bounds
378 FloatRect bounds = mSourceBounds;
379 const Rect layerCrop = getCrop(s);
380 if (!layerCrop.isEmpty()) {
381 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
382 }
383 bounds = bounds.intersect(parentBounds);
384
385 mBounds = bounds;
386 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700387
388 // Add any buffer scaling to the layer's children.
389 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800390 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700391 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
392 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800393 }
394}
395
Vishnu Nair60356342018-11-13 13:00:45 -0800396Rect Layer::getCroppedBufferSize(const State& s) const {
397 Rect size = getBufferSize(s);
398 Rect crop = getCrop(s);
399 if (!crop.isEmpty() && size.isValid()) {
400 size.intersect(crop, &size);
401 } else if (!crop.isEmpty()) {
402 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700403 }
Vishnu Nair60356342018-11-13 13:00:45 -0800404 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800405}
406
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700407void Layer::setupRoundedCornersCropCoordinates(Rect win,
408 const FloatRect& roundedCornersCrop) const {
409 // Translate win by the rounded corners rect coordinates, to have all values in
410 // layer coordinate space.
411 win.left -= roundedCornersCrop.left;
412 win.right -= roundedCornersCrop.left;
413 win.top -= roundedCornersCrop.top;
414 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700415}
416
Lloyd Piquea83776c2019-01-29 18:42:32 -0800417void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
418 const auto& drawingState{getDrawingState()};
419 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500420 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800421 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800422 blendMode =
423 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800424 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800425
Lloyd Piquea83776c2019-01-29 18:42:32 -0800426 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
427 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700428 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400429 if (parent.get()) {
430 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800431 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
432 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700433 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800434 type = parentType;
435 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700436 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400437 }
438
Lloyd Piquea83776c2019-01-29 18:42:32 -0800439 compositionState.geomLayerTransform = getTransform();
440 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
441 compositionState.geomBufferSize = getBufferSize(drawingState);
442 compositionState.geomContentCrop = getContentCrop();
443 compositionState.geomCrop = getCrop(drawingState);
444 compositionState.geomBufferTransform = mCurrentTransform;
445 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
446 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
447 compositionState.geomLayerBounds = mBounds;
448 compositionState.geomUsesSourceCrop = usesSourceCrop();
449 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700450
Lloyd Piquea83776c2019-01-29 18:42:32 -0800451 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
452 compositionState.alpha = alpha;
453 compositionState.type = type;
454 compositionState.appId = appId;
455}
David Sodmanba340492018-08-05 21:51:33 -0700456
Lloyd Piquea83776c2019-01-29 18:42:32 -0800457void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
458 bool includeGeometry) const {
459 if (includeGeometry) {
460 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700461 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800462}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700463
Lloyd Piquea83776c2019-01-29 18:42:32 -0800464const char* Layer::getDebugName() const {
465 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800466}
467
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800468void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
469 const auto outputLayer = findOutputLayerForDisplay(display);
470 LOG_FATAL_IF(!outputLayer);
471 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800472}
Dan Stozaee44edd2015-03-23 15:50:23 -0700473
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800474bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
475 const auto outputLayer = findOutputLayerForDisplay(display);
476 LOG_FATAL_IF(!outputLayer);
477 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800478}
479
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700480void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800481 const auto outputLayer = findOutputLayerForDisplay(display);
482 LOG_FATAL_IF(!outputLayer);
483
484 if (!outputLayer->getState().hwc ||
485 (*outputLayer->getState().hwc).hwcCompositionType !=
486 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 return;
488 }
489
490 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800491 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800492
493 // Apply the layer's transform, followed by the display's global transform
494 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800495 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700497 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800498 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700499 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700500 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501 auto position = displayTransform.transform(frame);
502
Dominik Laskowski7e045462018-05-30 13:02:02 -0700503 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800504 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800505 ALOGE_IF(error != HWC2::Error::None,
506 "[%s] Failed to set cursor position "
507 "to (%d, %d): %s (%d)",
508 mName.string(), position.left, position.top, to_string(error).c_str(),
509 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510}
Riley Andrews03414a12014-07-01 14:22:59 -0700511
Mathias Agopian13127d82013-03-05 17:47:11 -0800512// ---------------------------------------------------------------------------
513// drawing...
514// ---------------------------------------------------------------------------
515
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000516bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800517 Region& clearRegion, const bool supportProtectedContent,
518 renderengine::LayerSettings& layer) {
519 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800520}
521
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000522bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800523 Region& clearRegion, const bool supportProtectedContent,
524 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000525 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800526 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000527}
528
529bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
530 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800531 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000532 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800533 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000534 half alpha = getAlpha();
535 layer.geometry.boundaries = bounds;
536 if (useIdentityTransform) {
537 layer.geometry.positionTransform = mat4();
538 } else {
539 const ui::Transform transform = getTransform();
540 mat4 m;
541 m[0][0] = transform[0][0];
542 m[0][1] = transform[0][1];
543 m[0][3] = transform[0][2];
544 m[1][0] = transform[1][0];
545 m[1][1] = transform[1][1];
546 m[1][3] = transform[1][2];
547 m[3][0] = transform[2][0];
548 m[3][1] = transform[2][1];
549 m[3][3] = transform[2][2];
550 layer.geometry.positionTransform = m;
551 }
552
553 if (hasColorTransform()) {
554 layer.colorTransform = getColorTransform();
555 }
556
557 const auto roundedCornerState = getRoundedCornerState();
558 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
559 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
560
561 layer.alpha = alpha;
562 layer.sourceDataspace = mCurrentDataSpace;
563 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800564}
565
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800566void Layer::setCompositionType(const sp<const DisplayDevice>& display,
567 Hwc2::IComposerClient::Composition type) {
568 const auto outputLayer = findOutputLayerForDisplay(display);
569 LOG_FATAL_IF(!outputLayer);
570 LOG_FATAL_IF(!outputLayer->getState().hwc);
571 auto& compositionState = outputLayer->editState();
572
573 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
574 toString(type).c_str(), 1);
575 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700576 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800577 (*compositionState.hwc).hwcCompositionType = type;
578
579 auto error = (*compositionState.hwc)
580 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
581 ALOGE_IF(error != HWC2::Error::None,
582 "[%s] Failed to set "
583 "composition type %s: %s (%d)",
584 mName.string(), toString(type).c_str(), to_string(error).c_str(),
585 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586 }
587}
588
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800589Hwc2::IComposerClient::Composition Layer::getCompositionType(
590 const sp<const DisplayDevice>& display) const {
591 const auto outputLayer = findOutputLayerForDisplay(display);
592 LOG_FATAL_IF(!outputLayer);
593 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
594 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595}
596
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800597bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
598 const auto outputLayer = findOutputLayerForDisplay(display);
599 LOG_FATAL_IF(!outputLayer);
600 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800602
Dan Stozacac35382016-01-27 12:21:06 -0800603bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
604 if (point->getFrameNumber() <= mCurrentFrameNumber) {
605 // Don't bother with a SyncPoint, since we've already latched the
606 // relevant frame
607 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700608 }
Robert Carr2e102c92018-10-23 12:11:15 -0700609 if (isRemovedFromCurrentState()) {
610 return false;
611 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700612
Dan Stozacac35382016-01-27 12:21:06 -0800613 Mutex::Autolock lock(mLocalSyncPointMutex);
614 mLocalSyncPoints.push_back(point);
615 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700616}
617
Mathias Agopian13127d82013-03-05 17:47:11 -0800618// ----------------------------------------------------------------------------
619// local state
620// ----------------------------------------------------------------------------
621
David Sodman41fdfc92017-11-06 16:09:56 -0800622bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800623 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700624 return (s.flags & layer_state_t::eLayerSecure);
625}
626
Mathias Agopian13127d82013-03-05 17:47:11 -0800627void Layer::setVisibleRegion(const Region& visibleRegion) {
628 // always called from main thread
629 this->visibleRegion = visibleRegion;
630}
631
632void Layer::setCoveredRegion(const Region& coveredRegion) {
633 // always called from main thread
634 this->coveredRegion = coveredRegion;
635}
636
David Sodman41fdfc92017-11-06 16:09:56 -0800637void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800638 // always called from main thread
639 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
640}
641
Robert Carre5f4f692018-01-12 13:12:28 -0800642void Layer::clearVisibilityRegions() {
643 visibleRegion.clear();
644 visibleNonTransparentRegion.clear();
645 coveredRegion.clear();
646}
647
Mathias Agopian13127d82013-03-05 17:47:11 -0800648// ----------------------------------------------------------------------------
649// transaction
650// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800651
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800652void Layer::pushPendingState() {
653 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700654 return;
655 }
Alec Mourie60041e2019-06-14 18:59:51 -0700656 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700657
Dan Stoza7dde5992015-05-22 09:51:44 -0700658 // If this transaction is waiting on the receipt of a frame, generate a sync
659 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700660 // We don't allow installing sync points after we are removed from the current state
661 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800662 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
663 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800664 if (barrierLayer == nullptr) {
665 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700666 // If we can't promote the layer we are intended to wait on,
667 // then it is expired or otherwise invalid. Allow this transaction
668 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800669 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800670 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700671 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800672 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700673 std::stringstream ss;
674 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
675 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800676 mRemoteSyncPoints.push_back(std::move(syncPoint));
677 } else {
678 // We already missed the frame we're supposed to synchronize
679 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800680 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800681 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700682 }
683
Dan Stoza7dde5992015-05-22 09:51:44 -0700684 // Wake us up to check if the frame has been received
685 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700686 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800688 mPendingStates.push_back(mCurrentState);
689 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700690}
691
Pablo Ceballos05289c22016-04-14 15:49:55 -0700692void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700693 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800694 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700695
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800696 mPendingStates.removeAt(0);
697 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700698}
699
Pablo Ceballos05289c22016-04-14 15:49:55 -0700700bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700701 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800702 while (!mPendingStates.empty()) {
703 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700704 if (mRemoteSyncPoints.empty()) {
705 // If we don't have a sync point for this, apply it anyway. It
706 // will be visually wrong, but it should keep us from getting
707 // into too much trouble.
708 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700709 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700710 stateUpdateAvailable = true;
711 continue;
712 }
713
Marissa Wallf58c14b2018-07-24 10:50:43 -0700714 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800715 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800716 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800717
718 // Signal our end of the sync point and then dispose of it
719 mRemoteSyncPoints.front()->setTransactionApplied();
720 mRemoteSyncPoints.pop_front();
721 continue;
722 }
723
Dan Stoza7dde5992015-05-22 09:51:44 -0700724 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700725 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700726 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700727 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700728 stateUpdateAvailable = true;
729
730 // Signal our end of the sync point and then dispose of it
731 mRemoteSyncPoints.front()->setTransactionApplied();
732 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800733 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700734 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800735 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700736 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700737 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700738 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700739 stateUpdateAvailable = true;
740 }
741 }
742
743 // If we still have pending updates, wake SurfaceFlinger back up and point
744 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800745 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700746 setTransactionFlags(eTransactionNeeded);
747 mFlinger->setTransactionFlags(eTraversalNeeded);
748 }
749
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800750 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700751 return stateUpdateAvailable;
752}
753
Marissa Wall61c58622018-07-18 10:12:20 -0700754uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000755 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756
Marissa Wall61c58622018-07-18 10:12:20 -0700757 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
758 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700759
David Sodmaneb085e02017-10-05 18:49:04 -0700760 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700761 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000762 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800763 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
764 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
765 " requested={ wh={%4u,%4u} }}\n"
766 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
767 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700768 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700769 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
770 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
771 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
772 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
773 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700774 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
775 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
776 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700778
Robert Carre392b552017-09-19 12:16:05 -0700779 // Don't let Layer::doTransaction update the drawing state
780 // if we have a pending resize, unless we are in fixed-size mode.
781 // the drawing state will be updated only once we receive a buffer
782 // with the correct size.
783 //
784 // In particular, we want to make sure the clip (which is part
785 // of the geometry state) is latched together with the size but is
786 // latched immediately when no resizing is involved.
787 //
788 // If a sideband stream is attached, however, we want to skip this
789 // optimization so that transactions aren't missed when a buffer
790 // never arrives
791 //
792 // In the case that we don't have a buffer we ignore other factors
793 // and avoid entering the resizePending state. At a high level the
794 // resizePending state is to avoid applying the state of the new buffer
795 // to the old buffer. However in the state where we don't have an old buffer
796 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700797 const bool resizePending =
798 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
799 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800800 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700801 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800802 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700803 flags |= eDontUpdateGeometryState;
804 }
805 }
806
Robert Carr7bf247e2017-05-18 14:02:49 -0700807 // Here we apply various requested geometry states, depending on our
808 // latching configuration. See Layer.h for a detailed discussion of
809 // how geometry latching is controlled.
810 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000811 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700812
813 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
814 // mode, which causes attributes which normally latch regardless of scaling mode,
815 // to be delayed. We copy the requested state to the active state making sure
816 // to respect these rules (again see Layer.h for a detailed discussion).
817 //
818 // There is an awkward asymmetry in the handling of the crop states in the position
819 // states, as can be seen below. Largely this arises from position and transform
820 // being stored in the same data structure while having different latching rules.
821 // b/38182305
822 //
Marissa Wall61c58622018-07-18 10:12:20 -0700823 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700824 // applyPendingStates in the presence of deferred transactions.
825 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700826 float tx = stateToCommit->active_legacy.transform.tx();
827 float ty = stateToCommit->active_legacy.transform.ty();
828 stateToCommit->active_legacy = stateToCommit->requested_legacy;
829 stateToCommit->active_legacy.transform.set(tx, ty);
830 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700831 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700832 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700833 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700834 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800835 }
836
Marissa Wall61c58622018-07-18 10:12:20 -0700837 return flags;
838}
839
840uint32_t Layer::doTransaction(uint32_t flags) {
841 ATRACE_CALL();
842
chaviw5aedec92018-10-22 10:40:38 -0700843 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800844 return flags;
845 }
846
847 if (mChildrenChanged) {
848 flags |= eVisibleRegion;
849 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700850 }
851
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800852 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000853 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700854 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800855 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700856 }
857
858 flags = doTransactionResize(flags, &c);
859
Alec Mourib416efd2018-09-06 21:01:59 +0000860 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700861
862 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800863 // invalidate and recompute the visible regions if needed
864 flags |= Layer::eVisibleRegion;
865 }
866
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700867 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800868 // invalidate and recompute the visible regions if needed
869 flags |= eVisibleRegion;
870 this->contentDirty = true;
871
872 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700873 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700874 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 }
876
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700878 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800879 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700880 }
881
Mathias Agopian13127d82013-03-05 17:47:11 -0800882 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700883 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800884 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800885 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886}
887
Pablo Ceballos05289c22016-04-14 15:49:55 -0700888void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700890}
891
Mathias Agopian13127d82013-03-05 17:47:11 -0800892uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800893 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800894}
895
896uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800898}
899
Robert Carr82364e32016-05-15 11:27:47 -0700900bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 if (mCurrentState.requested_legacy.transform.tx() == x &&
902 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800903 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800904 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700905
906 // We update the requested and active position simultaneously because
907 // we want to apply the position portion of the transform matrix immediately,
908 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700910 if (immediate && !mFreezeGeometryUpdates) {
911 // Here we directly update the active state
912 // unlike other setters, because we store it within
913 // the transform, but use different latching rules.
914 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800915 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700916 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700917 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700918
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800919 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800920 setTransactionFlags(eTransactionNeeded);
921 return true;
922}
Robert Carr82364e32016-05-15 11:27:47 -0700923
Robert Carr1f0a16a2016-10-24 16:27:39 -0700924bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
925 ssize_t idx = mCurrentChildren.indexOf(childLayer);
926 if (idx < 0) {
927 return false;
928 }
929 if (childLayer->setLayer(z)) {
930 mCurrentChildren.removeAt(idx);
931 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800932 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700933 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800934 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700935}
936
Robert Carr503c7042017-09-27 15:06:08 -0700937bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
938 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
939 ssize_t idx = mCurrentChildren.indexOf(childLayer);
940 if (idx < 0) {
941 return false;
942 }
943 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
944 mCurrentChildren.removeAt(idx);
945 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800946 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700947 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800948 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700949}
950
Robert Carrae060832016-11-28 10:51:00 -0800951bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800952 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
953 mCurrentState.sequence++;
954 mCurrentState.z = z;
955 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700956
957 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800958 if (mCurrentState.zOrderRelativeOf != nullptr) {
959 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700960 if (strongRelative != nullptr) {
961 strongRelative->removeZOrderRelative(this);
962 }
chaviw606e5cf2019-03-01 10:12:10 -0800963 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700964 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800965 setTransactionFlags(eTransactionNeeded);
966 return true;
967}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700968
Robert Carrdb66e622017-04-10 16:55:57 -0700969void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800970 mCurrentState.zOrderRelatives.remove(relative);
971 mCurrentState.sequence++;
972 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700973 setTransactionFlags(eTransactionNeeded);
974}
975
976void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800977 mCurrentState.zOrderRelatives.add(relative);
978 mCurrentState.modified = true;
979 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700980 setTransactionFlags(eTransactionNeeded);
981}
982
chaviw606e5cf2019-03-01 10:12:10 -0800983void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
984 mCurrentState.zOrderRelativeOf = relativeOf;
985 mCurrentState.sequence++;
986 mCurrentState.modified = true;
987 setTransactionFlags(eTransactionNeeded);
988}
989
Robert Carr503d2bd2017-12-04 15:49:47 -0800990bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700991 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
992 if (handle == nullptr) {
993 return false;
994 }
995 sp<Layer> relative = handle->owner.promote();
996 if (relative == nullptr) {
997 return false;
998 }
999
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001000 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1001 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001002 return false;
1003 }
1004
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001005 mCurrentState.sequence++;
1006 mCurrentState.modified = true;
1007 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001008
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001009 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001010 if (oldZOrderRelativeOf != nullptr) {
1011 oldZOrderRelativeOf->removeZOrderRelative(this);
1012 }
chaviw606e5cf2019-03-01 10:12:10 -08001013 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001014 relative->addZOrderRelative(this);
1015
1016 setTransactionFlags(eTransactionNeeded);
1017
1018 return true;
1019}
1020
Mathias Agopian13127d82013-03-05 17:47:11 -08001021bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001022 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001023 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001024 mCurrentState.requested_legacy.w = w;
1025 mCurrentState.requested_legacy.h = h;
1026 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001027 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001028
1029 // record the new size, from this point on, when the client request
1030 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001031 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 return true;
1033}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001034bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001035 if (mCurrentState.color.a == alpha) return false;
1036 mCurrentState.sequence++;
1037 mCurrentState.color.a = alpha;
1038 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001039 setTransactionFlags(eTransactionNeeded);
1040 return true;
1041}
chaviw13fdc492017-06-27 12:40:18 -07001042
Valerie Haudd0b7572019-01-29 14:59:27 -08001043bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1044 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001045 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001046 }
1047 mCurrentState.sequence++;
1048 mCurrentState.modified = true;
1049 setTransactionFlags(eTransactionNeeded);
1050
1051 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001052 // create background color layer if one does not yet exist
1053 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1054 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001055 mCurrentState.bgColorLayer = new ColorLayer(
1056 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001057
Valerie Haudd0b7572019-01-29 14:59:27 -08001058 // add to child list
1059 addChild(mCurrentState.bgColorLayer);
1060 mFlinger->mLayersAdded = true;
1061 // set up SF to handle added color layer
1062 if (isRemovedFromCurrentState()) {
1063 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1064 }
1065 mFlinger->setTransactionFlags(eTransactionNeeded);
1066 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1067 mCurrentState.bgColorLayer->reparent(nullptr);
1068 mCurrentState.bgColorLayer = nullptr;
1069 return true;
1070 }
1071
1072 mCurrentState.bgColorLayer->setColor(color);
1073 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1074 mCurrentState.bgColorLayer->setAlpha(alpha);
1075 mCurrentState.bgColorLayer->setDataspace(dataspace);
1076
chaviw13fdc492017-06-27 12:40:18 -07001077 return true;
1078}
1079
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001080bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001081 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001082
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 mCurrentState.sequence++;
1084 mCurrentState.cornerRadius = cornerRadius;
1085 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001086 setTransactionFlags(eTransactionNeeded);
1087 return true;
1088}
1089
Robert Carrd4ae7f32018-06-07 16:10:57 -07001090bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1091 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001092 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001093 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1094
1095 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1096 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1097 return false;
1098 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001099 mCurrentState.sequence++;
1100 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1101 matrix.dsdy);
1102 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001103 setTransactionFlags(eTransactionNeeded);
1104 return true;
1105}
Marissa Wall61c58622018-07-18 10:12:20 -07001106
Mathias Agopian13127d82013-03-05 17:47:11 -08001107bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 mCurrentState.requestedTransparentRegion_legacy = transparent;
1109 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001110 setTransactionFlags(eTransactionNeeded);
1111 return true;
1112}
1113bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001114 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1115 if (mCurrentState.flags == newFlags) return false;
1116 mCurrentState.sequence++;
1117 mCurrentState.flags = newFlags;
1118 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001119 setTransactionFlags(eTransactionNeeded);
1120 return true;
1121}
Robert Carr99e27f02016-06-16 15:18:02 -07001122
Marissa Wallf58c14b2018-07-24 10:50:43 -07001123bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001124 if (mCurrentState.requestedCrop_legacy == crop) return false;
1125 mCurrentState.sequence++;
1126 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001127 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001128 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001129 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001130 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1131
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001132 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001133 setTransactionFlags(eTransactionNeeded);
1134 return true;
1135}
Robert Carr8d5227b2017-03-16 15:41:03 -07001136
Robert Carrc3574f72016-03-24 12:19:32 -07001137bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001138 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001139 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001140 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001141 return true;
1142}
1143
Evan Roskyef876c92019-01-25 17:46:06 -08001144bool Layer::setMetadata(const LayerMetadata& data) {
1145 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001146 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001147 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001148 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001149 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001150}
1151
Mathias Agopian13127d82013-03-05 17:47:11 -08001152bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001153 if (mCurrentState.layerStack == layerStack) return false;
1154 mCurrentState.sequence++;
1155 mCurrentState.layerStack = layerStack;
1156 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001157 setTransactionFlags(eTransactionNeeded);
1158 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001159}
1160
Peiyong Linc502cb72019-03-01 15:00:23 -08001161bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1162 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1163 return false;
1164 }
1165 mCurrentState.sequence++;
1166 mCurrentState.colorSpaceAgnostic = agnostic;
1167 mCurrentState.modified = true;
1168 setTransactionFlags(eTransactionNeeded);
1169 return true;
1170}
1171
Robert Carr1f0a16a2016-10-24 16:27:39 -07001172uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001173 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001174 if (p == nullptr) {
1175 return getDrawingState().layerStack;
1176 }
1177 return p->getLayerStack();
1178}
1179
Marissa Wallf58c14b2018-07-24 10:50:43 -07001180void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001181 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001182 mCurrentState.barrierLayer_legacy = barrierLayer;
1183 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001184 // We don't set eTransactionNeeded, because just receiving a deferral
1185 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001186 mCurrentState.modified = true;
1187 pushPendingState();
1188 mCurrentState.barrierLayer_legacy = nullptr;
1189 mCurrentState.frameNumber_legacy = 0;
1190 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001191}
1192
Marissa Wallf58c14b2018-07-24 10:50:43 -07001193void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001194 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001195 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001196}
1197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198// ----------------------------------------------------------------------------
1199// pageflip handling...
1200// ----------------------------------------------------------------------------
1201
Robert Carr1f0a16a2016-10-24 16:27:39 -07001202bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001203 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001204 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001205 if (parent != nullptr && parent->isHiddenByPolicy()) {
1206 return true;
1207 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001208 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1209 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1210 if (zOrderRelativeOf != nullptr) {
1211 if (zOrderRelativeOf->isHiddenByPolicy()) {
1212 return true;
1213 }
1214 }
1215 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001216 return s.flags & layer_state_t::eLayerHidden;
1217}
1218
David Sodman41fdfc92017-11-06 16:09:56 -08001219uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001220 // TODO: should we do something special if mSecure is set?
1221 if (mProtectedByApp) {
1222 // need a hardware-protected path to external video sink
1223 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001224 }
Riley Andrews03414a12014-07-01 14:22:59 -07001225 if (mPotentialCursor) {
1226 usage |= GraphicBuffer::USAGE_CURSOR;
1227 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001228 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001229 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001230}
1231
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001232void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001233 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001234 // Disable setting transform hint if the debug flag is set.
1235 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001236 // The transform hint is used to improve performance, but we can
1237 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001238 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001239 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001240 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001241 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001242 orientation = 0;
1243 }
1244 }
David Sodmaneb085e02017-10-05 18:49:04 -07001245 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001246}
1247
Mathias Agopian13127d82013-03-05 17:47:11 -08001248// ----------------------------------------------------------------------------
1249// debugging
1250// ----------------------------------------------------------------------------
1251
Marissa Wall61c58622018-07-18 10:12:20 -07001252// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001253LayerDebugInfo Layer::getLayerDebugInfo() const {
1254 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001255 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001256 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001257 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001258 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001259 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001260 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001261 info.mVisibleRegion = visibleRegion;
1262 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001263 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001264 info.mX = ds.active_legacy.transform.tx();
1265 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001266 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001267 info.mWidth = ds.active_legacy.w;
1268 info.mHeight = ds.active_legacy.h;
1269 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001270 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001271 info.mFlags = ds.flags;
1272 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001273 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001274 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1275 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1276 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1277 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001278 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001279 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001280 if (buffer != 0) {
1281 info.mActiveBufferWidth = buffer->getWidth();
1282 info.mActiveBufferHeight = buffer->getHeight();
1283 info.mActiveBufferStride = buffer->getStride();
1284 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001285 } else {
1286 info.mActiveBufferWidth = 0;
1287 info.mActiveBufferHeight = 0;
1288 info.mActiveBufferStride = 0;
1289 info.mActiveBufferFormat = 0;
1290 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001291 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001292 info.mNumQueuedFrames = getQueuedFrameCount();
1293 info.mRefreshPending = isBufferLatched();
1294 info.mIsOpaque = isOpaque(ds);
1295 info.mContentDirty = contentDirty;
1296 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001297}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001298
Yiwei Zhang5434a782018-12-05 18:06:32 -08001299void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001300 result.append("-------------------------------");
1301 result.append("-------------------------------");
1302 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001303 result.append(" Layer name\n");
1304 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001305 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001306 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001307 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001308 result.append(" Disp Frame (LTRB) | ");
1309 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001310 result.append("-------------------------------");
1311 result.append("-------------------------------");
1312 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001313}
1314
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001315void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1316 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1317 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001318 return;
1319 }
1320
Yiwei Zhang5434a782018-12-05 18:06:32 -08001321 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001322 if (mName.length() > 77) {
1323 std::string shortened;
1324 shortened.append(mName.string(), 36);
1325 shortened.append("[...]");
1326 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001327 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001328 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001329 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001330 }
1331
Yiwei Zhang5434a782018-12-05 18:06:32 -08001332 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001333
Alec Mourib416efd2018-09-06 21:01:59 +00001334 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001335 const auto& compositionState = outputLayer->getState();
1336
Chia-I Wu1e043612018-03-01 09:45:09 -08001337 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001338 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001339 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001340 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001341 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001342 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001343 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1344 StringAppendF(&result, "%10s | ",
1345 toString(getCompositionLayer() ? compositionState.bufferTransform
1346 : static_cast<Hwc2::Transform>(0))
1347 .c_str());
1348 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001349 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001350 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001351 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1352 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001353
Yichi Chen6ca35192018-05-29 12:20:43 +08001354 result.append("- - - - - - - - - - - - - - - -");
1355 result.append("- - - - - - - - - - - - - - - -");
1356 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001357}
Dan Stozae22aec72016-08-01 13:20:59 -07001358
Yiwei Zhang5434a782018-12-05 18:06:32 -08001359void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001360 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001361}
1362
Svetoslavd85084b2014-03-20 10:28:31 -07001363void Layer::clearFrameStats() {
1364 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001365}
1366
Jamie Gennis6547ff42013-07-16 20:12:42 -07001367void Layer::logFrameStats() {
1368 mFrameTracker.logAndResetStats(mName);
1369}
1370
Svetoslavd85084b2014-03-20 10:28:31 -07001371void Layer::getFrameStats(FrameStats* outStats) const {
1372 mFrameTracker.getStats(outStats);
1373}
1374
Yiwei Zhang5434a782018-12-05 18:06:32 -08001375void Layer::dumpFrameEvents(std::string& result) {
1376 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001377 Mutex::Autolock lock(mFrameEventHistoryMutex);
1378 mFrameEventHistory.checkFencesForCompletion();
1379 mFrameEventHistory.dump(result);
1380}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001381
Brian Anderson5ea5e592016-12-01 16:54:33 -08001382void Layer::onDisconnect() {
1383 Mutex::Autolock lock(mFrameEventHistoryMutex);
1384 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001385 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001386}
1387
Brian Anderson3890c392016-07-25 12:48:08 -07001388void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001389 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001390 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001391 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1392 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001393 }
1394
Brian Andersond6927fb2016-07-23 23:37:30 -07001395 Mutex::Autolock lock(mFrameEventHistoryMutex);
1396 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001397 // If there are any unsignaled fences in the aquire timeline at this
1398 // point, the previously queued frame hasn't been latched yet. Go ahead
1399 // and try to get the signal time here so the syscall is taken out of
1400 // the main thread's critical path.
1401 mAcquireTimeline.updateSignalTimes();
1402 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001403 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001404 mFrameEventHistory.addQueue(*newTimestamps);
1405 }
1406
Brian Anderson3890c392016-07-25 12:48:08 -07001407 if (outDelta) {
1408 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001409 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001410}
Dan Stozae77c7662016-05-13 11:37:28 -07001411
Chia-I Wu98f1c102017-05-30 14:54:08 -07001412size_t Layer::getChildrenCount() const {
1413 size_t count = 0;
1414 for (const sp<Layer>& child : mCurrentChildren) {
1415 count += 1 + child->getChildrenCount();
1416 }
1417 return count;
1418}
1419
Robert Carr1f0a16a2016-10-24 16:27:39 -07001420void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001421 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001422 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001423
Robert Carr1f0a16a2016-10-24 16:27:39 -07001424 mCurrentChildren.add(layer);
1425 layer->setParent(this);
1426}
1427
1428ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001429 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001430 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001431
Robert Carr1323c952019-01-28 18:13:27 -08001432 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001433 return mCurrentChildren.remove(layer);
1434}
1435
Robert Carr1db73f62016-12-21 12:58:51 -08001436bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1437 sp<Handle> handle = nullptr;
1438 sp<Layer> newParent = nullptr;
1439 if (newParentHandle == nullptr) {
1440 return false;
1441 }
1442 handle = static_cast<Handle*>(newParentHandle.get());
1443 newParent = handle->owner.promote();
1444 if (newParent == nullptr) {
1445 ALOGE("Unable to promote Layer handle");
1446 return false;
1447 }
1448
chaviw5aedec92018-10-22 10:40:38 -07001449 if (attachChildren()) {
1450 setTransactionFlags(eTransactionNeeded);
1451 }
Robert Carr1db73f62016-12-21 12:58:51 -08001452 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001453 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001454 }
1455 mCurrentChildren.clear();
1456
1457 return true;
1458}
1459
Robert Carr15eae092018-03-23 13:43:53 -07001460void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001461 for (const sp<Layer>& child : mDrawingChildren) {
1462 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001463 child->computeBounds(newParent->mBounds,
1464 newParent->getTransformWithScale(
1465 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001466 }
1467}
1468
chaviwf1961f72017-09-18 16:41:07 -07001469bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001470 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001471
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001472 // While layers are detached, we allow most operations
1473 // and simply halt performing the actual transaction. However
1474 // for reparent != null we would enter the mRemovedFromCurrentState
1475 // state, regardless of whether doTransaction was called, and
1476 // so we need to prevent the update here.
1477 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001478 return false;
1479 }
1480
Robert Carr54cf5b12019-01-25 14:02:28 -08001481 sp<Layer> newParent;
1482 if (newParentHandle != nullptr) {
1483 auto handle = static_cast<Handle*>(newParentHandle.get());
1484 newParent = handle->owner.promote();
1485 if (newParent == nullptr) {
1486 ALOGE("Unable to promote Layer handle");
1487 return false;
1488 }
1489 if (newParent == this) {
1490 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1491 return false;
1492 }
1493 }
1494
chaviwf1961f72017-09-18 16:41:07 -07001495 sp<Layer> parent = getParent();
1496 if (parent != nullptr) {
1497 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001498 }
1499
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001500 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001501 newParent->addChild(this);
1502 if (!newParent->isRemovedFromCurrentState()) {
1503 addToCurrentState();
1504 } else {
1505 onRemovedFromCurrentState();
1506 }
1507
1508 if (mLayerDetached) {
1509 mLayerDetached = false;
1510 callSetTransactionFlags = true;
1511 }
1512 } else {
1513 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001514 }
1515
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001516 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001517 setTransactionFlags(eTransactionNeeded);
1518 }
chaviw06178942017-07-27 10:25:59 -07001519 return true;
1520}
1521
Robert Carr9524cb32017-02-13 11:32:32 -08001522bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001523 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001524 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001525 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001526 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001527 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001528 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001529 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001530 }
Robert Carr7f619b22017-11-06 12:56:35 -08001531 }
Robert Carr9524cb32017-02-13 11:32:32 -08001532
1533 return true;
1534}
1535
chaviw5aedec92018-10-22 10:40:38 -07001536bool Layer::attachChildren() {
1537 bool changed = false;
1538 for (const sp<Layer>& child : mCurrentChildren) {
1539 sp<Client> parentClient = mClientRef.promote();
1540 sp<Client> client(child->mClientRef.promote());
1541 if (client != nullptr && parentClient != client) {
1542 if (child->mLayerDetached) {
1543 child->mLayerDetached = false;
1544 changed = true;
1545 }
1546 changed |= child->attachChildren();
1547 }
1548 }
1549
1550 return changed;
1551}
1552
Peiyong Lind3788632018-09-18 16:01:31 -07001553bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001554 static const mat4 identityMatrix = mat4();
1555
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001556 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001557 return false;
1558 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001559 ++mCurrentState.sequence;
1560 mCurrentState.colorTransform = matrix;
1561 mCurrentState.hasColorTransform = matrix != identityMatrix;
1562 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001563 setTransactionFlags(eTransactionNeeded);
1564 return true;
1565}
1566
chaviwf66724d2018-11-28 16:35:21 -08001567mat4 Layer::getColorTransform() const {
1568 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1569 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1570 colorTransform = parent->getColorTransform() * colorTransform;
1571 }
1572 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001573}
1574
1575bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001576 bool hasColorTransform = getDrawingState().hasColorTransform;
1577 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1578 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1579 }
1580 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001581}
1582
Chia-I Wu11481472018-05-04 10:43:19 -07001583bool Layer::isLegacyDataSpace() const {
1584 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001585 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001586 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001587}
1588
Robert Carr1f0a16a2016-10-24 16:27:39 -07001589void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001590 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001591}
1592
Robert Carr1f0a16a2016-10-24 16:27:39 -07001593int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001594 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001595}
1596
Robert Carr1c5481e2019-07-01 14:42:27 -07001597bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001598 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001599 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001600 return state.zOrderRelativeOf != nullptr;
1601}
1602
David Sodman41fdfc92017-11-06 16:09:56 -08001603__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001604 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001605 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1606 "makeTraversalList received invalid stateSet");
1607 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1608 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001609 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001610
Robert Carr29abff82017-12-04 13:51:20 -08001611 if (state.zOrderRelatives.size() == 0) {
1612 *outSkipRelativeZUsers = true;
1613 return children;
1614 }
1615
chaviwfd462612018-05-31 16:11:27 -07001616 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001617 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001618 sp<Layer> strongRelative = weakRelative.promote();
1619 if (strongRelative != nullptr) {
1620 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001621 }
1622 }
1623
Dan Stoza412903f2017-04-27 13:42:17 -07001624 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001625 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001626 if (childState.zOrderRelativeOf != nullptr) {
1627 continue;
1628 }
Robert Carrdb66e622017-04-10 16:55:57 -07001629 traverse.add(child);
1630 }
1631
1632 return traverse;
1633}
1634
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635/**
Robert Carrdb66e622017-04-10 16:55:57 -07001636 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637 */
Dan Stoza412903f2017-04-27 13:42:17 -07001638void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001639 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1640 // produce a new list for traversing, including our relatives, and not including our children
1641 // who are relatives of another surface. In the case that there are no relative Z,
1642 // makeTraversalList returns our children directly to avoid significant overhead.
1643 // However in this case we need to take the responsibility for filtering children which
1644 // are relatives of another surface here.
1645 bool skipRelativeZUsers = false;
1646 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001647
Robert Carr1f0a16a2016-10-24 16:27:39 -07001648 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001649 for (; i < list.size(); i++) {
1650 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001651 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1652 continue;
1653 }
1654
Robert Carrdb66e622017-04-10 16:55:57 -07001655 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001657 }
Dan Stoza412903f2017-04-27 13:42:17 -07001658 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001659 }
Robert Carr29abff82017-12-04 13:51:20 -08001660
Dan Stoza412903f2017-04-27 13:42:17 -07001661 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001662 for (; i < list.size(); i++) {
1663 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001664
Robert Carr29abff82017-12-04 13:51:20 -08001665 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1666 continue;
1667 }
Dan Stoza412903f2017-04-27 13:42:17 -07001668 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669 }
1670}
1671
1672/**
Robert Carrdb66e622017-04-10 16:55:57 -07001673 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001674 */
Dan Stoza412903f2017-04-27 13:42:17 -07001675void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1676 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001677 // See traverseInZOrder for documentation.
1678 bool skipRelativeZUsers = false;
1679 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001680
Robert Carr1f0a16a2016-10-24 16:27:39 -07001681 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001682 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001683 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001684
1685 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1686 continue;
1687 }
1688
Robert Carrdb66e622017-04-10 16:55:57 -07001689 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001690 break;
1691 }
Dan Stoza412903f2017-04-27 13:42:17 -07001692 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693 }
Dan Stoza412903f2017-04-27 13:42:17 -07001694 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001695 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001696 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001697
1698 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1699 continue;
1700 }
1701
Dan Stoza412903f2017-04-27 13:42:17 -07001702 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001703 }
1704}
1705
chaviw4b129c22018-04-09 16:19:43 -07001706LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1707 const std::vector<Layer*>& layersInTree) {
1708 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1709 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001710 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1711 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001712 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001713
chaviwfd462612018-05-31 16:11:27 -07001714 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001715 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1716 sp<Layer> strongRelative = weakRelative.promote();
1717 // Only add relative layers that are also descendents of the top most parent of the tree.
1718 // If a relative layer is not a descendent, then it should be ignored.
1719 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1720 traverse.add(strongRelative);
1721 }
1722 }
1723
1724 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001725 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001726 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1727 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1728 // the child here since it won't be added later as a relative.
1729 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1730 childState.zOrderRelativeOf.promote().get())) {
1731 continue;
1732 }
1733 traverse.add(child);
1734 }
1735
1736 return traverse;
1737}
1738
1739void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1740 LayerVector::StateSet stateSet,
1741 const LayerVector::Visitor& visitor) {
1742 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001743
1744 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001745 for (; i < list.size(); i++) {
1746 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001747 if (relative->getZ() >= 0) {
1748 break;
1749 }
chaviw4b129c22018-04-09 16:19:43 -07001750 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001751 }
chaviw4b129c22018-04-09 16:19:43 -07001752
chaviwa76b2712017-09-20 12:02:26 -07001753 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001754 for (; i < list.size(); i++) {
1755 const auto& relative = list[i];
1756 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001757 }
1758}
1759
chaviw4b129c22018-04-09 16:19:43 -07001760std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1761 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1762 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1763
1764 std::vector<Layer*> layersInTree = {this};
1765 for (size_t i = 0; i < children.size(); i++) {
1766 const auto& child = children[i];
1767 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1768 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1769 }
1770
1771 return layersInTree;
1772}
1773
1774void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1775 const LayerVector::Visitor& visitor) {
1776 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1777 std::sort(layersInTree.begin(), layersInTree.end());
1778 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1779}
1780
Peiyong Linefefaac2018-08-17 12:27:51 -07001781ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001782 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001783}
1784
chaviw13fdc492017-06-27 12:40:18 -07001785half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001786 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001787
chaviw13fdc492017-06-27 12:40:18 -07001788 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1789 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001790}
Robert Carr6452f122017-03-21 10:41:29 -07001791
chaviw13fdc492017-06-27 12:40:18 -07001792half4 Layer::getColor() const {
1793 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001794 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001795}
Robert Carr6452f122017-03-21 10:41:29 -07001796
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001797Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1798 const auto& p = mDrawingParent.promote();
1799 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001800 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001801 if (parentState.radius > 0) {
1802 ui::Transform t = getActiveTransform(getDrawingState());
1803 t = t.inverse();
1804 parentState.cropRect = t.transform(parentState.cropRect);
1805 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1806 // but a transform matrix can define horizontal and vertical scales.
1807 // Let's take the average between both of them and pass into the shader, practically we
1808 // never do this type of transformation on windows anyway.
1809 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1810 return parentState;
1811 }
1812 }
1813 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001814 return radius > 0 && getCrop(getDrawingState()).isValid()
1815 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1816 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001817}
1818
Robert Carr1f0a16a2016-10-24 16:27:39 -07001819void Layer::commitChildList() {
1820 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1821 const auto& child = mCurrentChildren[i];
1822 child->commitChildList();
1823 }
1824 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001825 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001826}
1827
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001828static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1829 if (weakBinderHandle == nullptr) {
1830 return nullptr;
1831 }
1832 sp<IBinder> binderHandle = weakBinderHandle.promote();
1833 if (binderHandle == nullptr) {
1834 return nullptr;
1835 }
1836 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1837 if (handle == nullptr) {
1838 return nullptr;
1839 }
1840 return handle->owner;
1841}
1842
Robert Carr720e5062018-07-30 17:45:14 -07001843void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001844 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001845 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001846 mCurrentState.modified = true;
1847 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001848 setTransactionFlags(eTransactionNeeded);
1849}
1850
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001851void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1852 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001853 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1854 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001855 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001856
Peiyong Linefefaac2018-08-17 12:27:51 -07001857 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001858 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001859
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001860 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1861 layerInfo->set_id(sequence);
1862 layerInfo->set_name(getName().c_str());
1863 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001864
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001865 for (const auto& child : children) {
1866 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001867 }
chaviw1d044282017-09-27 12:19:28 -07001868
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001869 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1870 sp<Layer> strongRelative = weakRelative.promote();
1871 if (strongRelative != nullptr) {
1872 layerInfo->add_relatives(strongRelative->sequence);
1873 }
chaviwadc40c22018-07-10 16:57:27 -07001874 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001875
1876 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1877 [&]() { return layerInfo->mutable_transparent_region(); });
1878 LayerProtoHelper::writeToProto(visibleRegion,
1879 [&]() { return layerInfo->mutable_visible_region(); });
1880 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1881 [&]() { return layerInfo->mutable_damage_region(); });
1882
1883 layerInfo->set_layer_stack(getLayerStack());
1884 layerInfo->set_z(state.z);
1885
1886 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1887 [&]() { return layerInfo->mutable_position(); });
1888
1889 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1890 [&]() {
1891 return layerInfo->mutable_requested_position();
1892 });
1893
1894 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1895 [&]() { return layerInfo->mutable_size(); });
1896
1897 LayerProtoHelper::writeToProto(state.crop_legacy,
1898 [&]() { return layerInfo->mutable_crop(); });
1899 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1900
1901 layerInfo->set_is_opaque(isOpaque(state));
1902 layerInfo->set_invalidate(contentDirty);
Peiyong Lin51598552019-04-17 14:09:22 -07001903 layerInfo->set_is_protected(isProtected());
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001904
1905 // XXX (b/79210409) mCurrentDataSpace is not protected
1906 layerInfo->set_dataspace(
1907 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1908
1909 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1910 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1911 LayerProtoHelper::writeToProto(state.color,
1912 [&]() { return layerInfo->mutable_requested_color(); });
1913 layerInfo->set_flags(state.flags);
1914
1915 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1916 LayerProtoHelper::writeToProto(requestedTransform,
1917 layerInfo->mutable_requested_transform());
1918
1919 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1920 if (parent != nullptr) {
1921 layerInfo->set_parent(parent->sequence);
1922 } else {
1923 layerInfo->set_parent(-1);
1924 }
1925
1926 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1927 if (zOrderRelativeOf != nullptr) {
1928 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1929 } else {
1930 layerInfo->set_z_order_relative_of(-1);
1931 }
1932
1933 auto buffer = mActiveBuffer;
1934 if (buffer != nullptr) {
1935 LayerProtoHelper::writeToProto(buffer,
1936 [&]() { return layerInfo->mutable_active_buffer(); });
1937 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1938 layerInfo->mutable_buffer_transform());
1939 }
1940
1941 layerInfo->set_queued_frames(getQueuedFrameCount());
1942 layerInfo->set_refresh_pending(isBufferLatched());
1943 layerInfo->set_curr_frame(mCurrentFrameNumber);
1944 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1945
1946 for (const auto& pendingState : mPendingStates) {
1947 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1948 if (barrierLayer != nullptr) {
1949 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1950 barrierLayerProto->set_id(barrierLayer->sequence);
1951 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1952 }
1953 }
1954 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001955 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001956
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001957 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1958 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1959 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001960 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001961
1962 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1963 auto protoMap = layerInfo->mutable_metadata();
1964 for (const auto& entry : state.metadata.mMap) {
1965 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1966 }
1967 LayerProtoHelper::writeToProto(mEffectiveTransform,
1968 layerInfo->mutable_effective_transform());
1969 LayerProtoHelper::writeToProto(mSourceBounds,
1970 [&]() { return layerInfo->mutable_source_bounds(); });
1971 LayerProtoHelper::writeToProto(mScreenBounds,
1972 [&]() { return layerInfo->mutable_screen_bounds(); });
1973 }
chaviw1d044282017-09-27 12:19:28 -07001974}
1975
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001976void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1977 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001978 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1979 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001980 return;
1981 }
1982
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001983 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001984
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001985 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001986
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001987 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001988 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001989
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001990 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001991 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001992
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001993 const int32_t transform =
1994 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001995 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001996
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001997 const int32_t compositionType =
1998 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1999 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002000 layerInfo->set_hwc_composition_type(compositionType);
2001
2002 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2003 static_cast<BufferLayer*>(this)->isProtected()) {
2004 layerInfo->set_is_protected(true);
2005 } else {
2006 layerInfo->set_is_protected(false);
2007 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002008}
2009
Robert Carr2e102c92018-10-23 12:11:15 -07002010bool Layer::isRemovedFromCurrentState() const {
2011 return mRemovedFromCurrentState;
2012}
2013
Arthur Hungd20b2702019-01-14 18:16:16 +08002014InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002015 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002016
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002017 if (info.displayId == ADISPLAY_ID_NONE) {
2018 info.displayId = mDrawingState.layerStack;
2019 }
2020
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002021 ui::Transform t = getTransform();
2022 const float xScale = t.sx();
2023 const float yScale = t.sy();
Arthur Hung118b1142019-05-08 21:25:59 +08002024 float xSurfaceInset = info.surfaceInset;
2025 float ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002026 if (xScale != 1.0f || yScale != 1.0f) {
2027 info.windowXScale *= 1.0f / xScale;
2028 info.windowYScale *= 1.0f / yScale;
2029 info.touchableRegion.scaleSelf(xScale, yScale);
Arthur Hung118b1142019-05-08 21:25:59 +08002030 xSurfaceInset *= xScale;
2031 ySurfaceInset *= yScale;
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002032 }
Robert Carre07e1032018-11-26 12:55:53 -08002033
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002034 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002035 // If this is a portal window, set the touchableRegion to the layerBounds.
2036 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2037 ? getBufferSize(getDrawingState())
2038 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002039 if (!layerBounds.isValid()) {
2040 layerBounds = getCroppedBufferSize(getDrawingState());
2041 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002042 layerBounds = t.transform(layerBounds);
Arthur Hung118b1142019-05-08 21:25:59 +08002043 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002044
Arthur Hungd20b2702019-01-14 18:16:16 +08002045 // Input coordinate should match the layer bounds.
2046 info.frameLeft = layerBounds.left;
2047 info.frameTop = layerBounds.top;
2048 info.frameRight = layerBounds.right;
2049 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002050
2051 // Position the touchable region relative to frame screen location and restrict it to frame
2052 // bounds.
2053 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002054 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002055
2056 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2057 if (info.replaceTouchableRegionWithCrop) {
2058 if (cropLayer == nullptr) {
2059 info.touchableRegion = Region(Rect{mScreenBounds});
2060 } else {
2061 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2062 }
2063 } else if (cropLayer != nullptr) {
2064 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2065 }
2066
Robert Carr720e5062018-07-30 17:45:14 -07002067 return info;
2068}
2069
2070bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002071 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002072}
2073
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002074std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2075 return nullptr;
2076}
2077
chaviw3e727cd2019-01-31 13:41:05 -08002078bool Layer::canReceiveInput() const {
2079 return isVisible();
2080}
2081
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002082compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2083 const sp<const DisplayDevice>& display) const {
2084 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2085}
2086
Mathias Agopian13127d82013-03-05 17:47:11 -08002087// ---------------------------------------------------------------------------
2088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002089}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002090
2091#if defined(__gl_h_)
2092#error "don't include gl/gl.h in this file"
2093#endif
2094
2095#if defined(__gl2_h_)
2096#error "don't include gl2/gl2.h in this file"
2097#endif