blob: bda7e4308f2460d895baf1abba593dd1e7f6140d [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);
433 if (parentType >= 0 || parentAppId >= 0) {
434 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
Peiyong Lin833074a2018-08-28 11:53:54 -0700622void Layer::computeGeometry(const RenderArea& renderArea,
623 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700624 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700625 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800626 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700627
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000628 vec2 lt = vec2(win.left, win.top);
629 vec2 lb = vec2(win.left, win.bottom);
630 vec2 rb = vec2(win.right, win.bottom);
631 vec2 rt = vec2(win.right, win.top);
632
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800633 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000634 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700635 lt = layerTransform.transform(lt);
636 lb = layerTransform.transform(lb);
637 rb = layerTransform.transform(rb);
638 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000639 }
640
Peiyong Lin833074a2018-08-28 11:53:54 -0700641 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700642 position[0] = renderAreaTransform.transform(lt);
643 position[1] = renderAreaTransform.transform(lb);
644 position[2] = renderAreaTransform.transform(rb);
645 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800646}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800647
David Sodman41fdfc92017-11-06 16:09:56 -0800648bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800649 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700650 return (s.flags & layer_state_t::eLayerSecure);
651}
652
Mathias Agopian13127d82013-03-05 17:47:11 -0800653void Layer::setVisibleRegion(const Region& visibleRegion) {
654 // always called from main thread
655 this->visibleRegion = visibleRegion;
656}
657
658void Layer::setCoveredRegion(const Region& coveredRegion) {
659 // always called from main thread
660 this->coveredRegion = coveredRegion;
661}
662
David Sodman41fdfc92017-11-06 16:09:56 -0800663void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800664 // always called from main thread
665 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
666}
667
Robert Carre5f4f692018-01-12 13:12:28 -0800668void Layer::clearVisibilityRegions() {
669 visibleRegion.clear();
670 visibleNonTransparentRegion.clear();
671 coveredRegion.clear();
672}
673
Mathias Agopian13127d82013-03-05 17:47:11 -0800674// ----------------------------------------------------------------------------
675// transaction
676// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800677
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800678void Layer::pushPendingState() {
679 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700680 return;
681 }
Alec Mourie60041e2019-06-14 18:59:51 -0700682 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700683
Dan Stoza7dde5992015-05-22 09:51:44 -0700684 // If this transaction is waiting on the receipt of a frame, generate a sync
685 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700686 // We don't allow installing sync points after we are removed from the current state
687 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800688 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
689 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800690 if (barrierLayer == nullptr) {
691 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700692 // If we can't promote the layer we are intended to wait on,
693 // then it is expired or otherwise invalid. Allow this transaction
694 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800695 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800696 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700697 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800698 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700699 std::stringstream ss;
700 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
701 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800702 mRemoteSyncPoints.push_back(std::move(syncPoint));
703 } else {
704 // We already missed the frame we're supposed to synchronize
705 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800706 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800707 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700708 }
709
Dan Stoza7dde5992015-05-22 09:51:44 -0700710 // Wake us up to check if the frame has been received
711 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700712 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700713 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800714 mPendingStates.push_back(mCurrentState);
715 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700716}
717
Pablo Ceballos05289c22016-04-14 15:49:55 -0700718void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700719 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800720 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700721
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800722 mPendingStates.removeAt(0);
723 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700724}
725
Pablo Ceballos05289c22016-04-14 15:49:55 -0700726bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700727 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800728 while (!mPendingStates.empty()) {
729 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700730 if (mRemoteSyncPoints.empty()) {
731 // If we don't have a sync point for this, apply it anyway. It
732 // will be visually wrong, but it should keep us from getting
733 // into too much trouble.
734 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700735 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700736 stateUpdateAvailable = true;
737 continue;
738 }
739
Marissa Wallf58c14b2018-07-24 10:50:43 -0700740 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800741 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800742 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800743
744 // Signal our end of the sync point and then dispose of it
745 mRemoteSyncPoints.front()->setTransactionApplied();
746 mRemoteSyncPoints.pop_front();
747 continue;
748 }
749
Dan Stoza7dde5992015-05-22 09:51:44 -0700750 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700751 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700752 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700753 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700754 stateUpdateAvailable = true;
755
756 // Signal our end of the sync point and then dispose of it
757 mRemoteSyncPoints.front()->setTransactionApplied();
758 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800759 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700760 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800761 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700762 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700763 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700764 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700765 stateUpdateAvailable = true;
766 }
767 }
768
769 // If we still have pending updates, wake SurfaceFlinger back up and point
770 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800771 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700772 setTransactionFlags(eTransactionNeeded);
773 mFlinger->setTransactionFlags(eTraversalNeeded);
774 }
775
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800776 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700777 return stateUpdateAvailable;
778}
779
Marissa Wall61c58622018-07-18 10:12:20 -0700780uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000781 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782
Marissa Wall61c58622018-07-18 10:12:20 -0700783 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
784 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700785
David Sodmaneb085e02017-10-05 18:49:04 -0700786 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700787 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000788 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800789 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
790 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
791 " requested={ wh={%4u,%4u} }}\n"
792 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
793 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700794 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700795 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
796 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
797 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
798 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
799 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700800 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
801 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
802 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700804
Robert Carre392b552017-09-19 12:16:05 -0700805 // Don't let Layer::doTransaction update the drawing state
806 // if we have a pending resize, unless we are in fixed-size mode.
807 // the drawing state will be updated only once we receive a buffer
808 // with the correct size.
809 //
810 // In particular, we want to make sure the clip (which is part
811 // of the geometry state) is latched together with the size but is
812 // latched immediately when no resizing is involved.
813 //
814 // If a sideband stream is attached, however, we want to skip this
815 // optimization so that transactions aren't missed when a buffer
816 // never arrives
817 //
818 // In the case that we don't have a buffer we ignore other factors
819 // and avoid entering the resizePending state. At a high level the
820 // resizePending state is to avoid applying the state of the new buffer
821 // to the old buffer. However in the state where we don't have an old buffer
822 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700823 const bool resizePending =
824 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
825 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800826 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700827 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800828 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700829 flags |= eDontUpdateGeometryState;
830 }
831 }
832
Robert Carr7bf247e2017-05-18 14:02:49 -0700833 // Here we apply various requested geometry states, depending on our
834 // latching configuration. See Layer.h for a detailed discussion of
835 // how geometry latching is controlled.
836 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000837 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700838
839 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
840 // mode, which causes attributes which normally latch regardless of scaling mode,
841 // to be delayed. We copy the requested state to the active state making sure
842 // to respect these rules (again see Layer.h for a detailed discussion).
843 //
844 // There is an awkward asymmetry in the handling of the crop states in the position
845 // states, as can be seen below. Largely this arises from position and transform
846 // being stored in the same data structure while having different latching rules.
847 // b/38182305
848 //
Marissa Wall61c58622018-07-18 10:12:20 -0700849 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700850 // applyPendingStates in the presence of deferred transactions.
851 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700852 float tx = stateToCommit->active_legacy.transform.tx();
853 float ty = stateToCommit->active_legacy.transform.ty();
854 stateToCommit->active_legacy = stateToCommit->requested_legacy;
855 stateToCommit->active_legacy.transform.set(tx, ty);
856 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700857 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700858 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700859 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700860 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800861 }
862
Marissa Wall61c58622018-07-18 10:12:20 -0700863 return flags;
864}
865
866uint32_t Layer::doTransaction(uint32_t flags) {
867 ATRACE_CALL();
868
chaviw5aedec92018-10-22 10:40:38 -0700869 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800870 return flags;
871 }
872
873 if (mChildrenChanged) {
874 flags |= eVisibleRegion;
875 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700876 }
877
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800878 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000879 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700880 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800881 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700882 }
883
884 flags = doTransactionResize(flags, &c);
885
Alec Mourib416efd2018-09-06 21:01:59 +0000886 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700887
888 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800889 // invalidate and recompute the visible regions if needed
890 flags |= Layer::eVisibleRegion;
891 }
892
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700893 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800894 // invalidate and recompute the visible regions if needed
895 flags |= eVisibleRegion;
896 this->contentDirty = true;
897
898 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700899 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700900 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800901 }
902
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800903 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700904 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800905 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700906 }
907
Mathias Agopian13127d82013-03-05 17:47:11 -0800908 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700909 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800910 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800911 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912}
913
Pablo Ceballos05289c22016-04-14 15:49:55 -0700914void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800915 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700916}
917
Mathias Agopian13127d82013-03-05 17:47:11 -0800918uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800919 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800920}
921
922uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800923 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800924}
925
Robert Carr82364e32016-05-15 11:27:47 -0700926bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800927 if (mCurrentState.requested_legacy.transform.tx() == x &&
928 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800929 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800930 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700931
932 // We update the requested and active position simultaneously because
933 // we want to apply the position portion of the transform matrix immediately,
934 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800935 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700936 if (immediate && !mFreezeGeometryUpdates) {
937 // Here we directly update the active state
938 // unlike other setters, because we store it within
939 // the transform, but use different latching rules.
940 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800941 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700942 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700943 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700944
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800945 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800946 setTransactionFlags(eTransactionNeeded);
947 return true;
948}
Robert Carr82364e32016-05-15 11:27:47 -0700949
Robert Carr1f0a16a2016-10-24 16:27:39 -0700950bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
951 ssize_t idx = mCurrentChildren.indexOf(childLayer);
952 if (idx < 0) {
953 return false;
954 }
955 if (childLayer->setLayer(z)) {
956 mCurrentChildren.removeAt(idx);
957 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800958 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700959 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800960 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700961}
962
Robert Carr503c7042017-09-27 15:06:08 -0700963bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
964 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
965 ssize_t idx = mCurrentChildren.indexOf(childLayer);
966 if (idx < 0) {
967 return false;
968 }
969 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
970 mCurrentChildren.removeAt(idx);
971 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800972 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700973 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800974 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700975}
976
Robert Carrae060832016-11-28 10:51:00 -0800977bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800978 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
979 mCurrentState.sequence++;
980 mCurrentState.z = z;
981 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700982
983 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800984 if (mCurrentState.zOrderRelativeOf != nullptr) {
985 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700986 if (strongRelative != nullptr) {
987 strongRelative->removeZOrderRelative(this);
988 }
chaviw606e5cf2019-03-01 10:12:10 -0800989 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700990 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800991 setTransactionFlags(eTransactionNeeded);
992 return true;
993}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700994
Robert Carrdb66e622017-04-10 16:55:57 -0700995void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800996 mCurrentState.zOrderRelatives.remove(relative);
997 mCurrentState.sequence++;
998 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700999 setTransactionFlags(eTransactionNeeded);
1000}
1001
1002void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001003 mCurrentState.zOrderRelatives.add(relative);
1004 mCurrentState.modified = true;
1005 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001006 setTransactionFlags(eTransactionNeeded);
1007}
1008
chaviw606e5cf2019-03-01 10:12:10 -08001009void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1010 mCurrentState.zOrderRelativeOf = relativeOf;
1011 mCurrentState.sequence++;
1012 mCurrentState.modified = true;
1013 setTransactionFlags(eTransactionNeeded);
1014}
1015
Robert Carr503d2bd2017-12-04 15:49:47 -08001016bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001017 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1018 if (handle == nullptr) {
1019 return false;
1020 }
1021 sp<Layer> relative = handle->owner.promote();
1022 if (relative == nullptr) {
1023 return false;
1024 }
1025
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001026 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1027 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001028 return false;
1029 }
1030
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001031 mCurrentState.sequence++;
1032 mCurrentState.modified = true;
1033 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001034
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001035 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001036 if (oldZOrderRelativeOf != nullptr) {
1037 oldZOrderRelativeOf->removeZOrderRelative(this);
1038 }
chaviw606e5cf2019-03-01 10:12:10 -08001039 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001040 relative->addZOrderRelative(this);
1041
1042 setTransactionFlags(eTransactionNeeded);
1043
1044 return true;
1045}
1046
Mathias Agopian13127d82013-03-05 17:47:11 -08001047bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001048 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001049 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001050 mCurrentState.requested_legacy.w = w;
1051 mCurrentState.requested_legacy.h = h;
1052 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001054
1055 // record the new size, from this point on, when the client request
1056 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001057 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001058 return true;
1059}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001060bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001061 if (mCurrentState.color.a == alpha) return false;
1062 mCurrentState.sequence++;
1063 mCurrentState.color.a = alpha;
1064 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 setTransactionFlags(eTransactionNeeded);
1066 return true;
1067}
chaviw13fdc492017-06-27 12:40:18 -07001068
Valerie Haudd0b7572019-01-29 14:59:27 -08001069bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1070 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001071 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001072 }
1073 mCurrentState.sequence++;
1074 mCurrentState.modified = true;
1075 setTransactionFlags(eTransactionNeeded);
1076
1077 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001078 // create background color layer if one does not yet exist
1079 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1080 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001081 mCurrentState.bgColorLayer = new ColorLayer(
1082 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001083
Valerie Haudd0b7572019-01-29 14:59:27 -08001084 // add to child list
1085 addChild(mCurrentState.bgColorLayer);
1086 mFlinger->mLayersAdded = true;
1087 // set up SF to handle added color layer
1088 if (isRemovedFromCurrentState()) {
1089 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1090 }
1091 mFlinger->setTransactionFlags(eTransactionNeeded);
1092 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1093 mCurrentState.bgColorLayer->reparent(nullptr);
1094 mCurrentState.bgColorLayer = nullptr;
1095 return true;
1096 }
1097
1098 mCurrentState.bgColorLayer->setColor(color);
1099 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1100 mCurrentState.bgColorLayer->setAlpha(alpha);
1101 mCurrentState.bgColorLayer->setDataspace(dataspace);
1102
chaviw13fdc492017-06-27 12:40:18 -07001103 return true;
1104}
1105
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001106bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001107 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001108
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001109 mCurrentState.sequence++;
1110 mCurrentState.cornerRadius = cornerRadius;
1111 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001112 setTransactionFlags(eTransactionNeeded);
1113 return true;
1114}
1115
Robert Carrd4ae7f32018-06-07 16:10:57 -07001116bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1117 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001118 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001119 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1120
1121 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1122 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1123 return false;
1124 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001125 mCurrentState.sequence++;
1126 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1127 matrix.dsdy);
1128 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001129 setTransactionFlags(eTransactionNeeded);
1130 return true;
1131}
Marissa Wall61c58622018-07-18 10:12:20 -07001132
Mathias Agopian13127d82013-03-05 17:47:11 -08001133bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001134 mCurrentState.requestedTransparentRegion_legacy = transparent;
1135 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001136 setTransactionFlags(eTransactionNeeded);
1137 return true;
1138}
1139bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001140 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1141 if (mCurrentState.flags == newFlags) return false;
1142 mCurrentState.sequence++;
1143 mCurrentState.flags = newFlags;
1144 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001145 setTransactionFlags(eTransactionNeeded);
1146 return true;
1147}
Robert Carr99e27f02016-06-16 15:18:02 -07001148
Marissa Wallf58c14b2018-07-24 10:50:43 -07001149bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001150 if (mCurrentState.requestedCrop_legacy == crop) return false;
1151 mCurrentState.sequence++;
1152 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001153 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001154 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001155 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001156 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1157
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001158 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001159 setTransactionFlags(eTransactionNeeded);
1160 return true;
1161}
Robert Carr8d5227b2017-03-16 15:41:03 -07001162
Robert Carrc3574f72016-03-24 12:19:32 -07001163bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001164 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001165 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001166 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001167 return true;
1168}
1169
Evan Roskyef876c92019-01-25 17:46:06 -08001170bool Layer::setMetadata(const LayerMetadata& data) {
1171 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001172 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001173 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001174 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001175 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001176}
1177
Mathias Agopian13127d82013-03-05 17:47:11 -08001178bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001179 if (mCurrentState.layerStack == layerStack) return false;
1180 mCurrentState.sequence++;
1181 mCurrentState.layerStack = layerStack;
1182 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001183 setTransactionFlags(eTransactionNeeded);
1184 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001185}
1186
Peiyong Linc502cb72019-03-01 15:00:23 -08001187bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1188 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1189 return false;
1190 }
1191 mCurrentState.sequence++;
1192 mCurrentState.colorSpaceAgnostic = agnostic;
1193 mCurrentState.modified = true;
1194 setTransactionFlags(eTransactionNeeded);
1195 return true;
1196}
1197
Robert Carr1f0a16a2016-10-24 16:27:39 -07001198uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001199 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001200 if (p == nullptr) {
1201 return getDrawingState().layerStack;
1202 }
1203 return p->getLayerStack();
1204}
1205
Marissa Wallf58c14b2018-07-24 10:50:43 -07001206void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001207 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001208 mCurrentState.barrierLayer_legacy = barrierLayer;
1209 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001210 // We don't set eTransactionNeeded, because just receiving a deferral
1211 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001212 mCurrentState.modified = true;
1213 pushPendingState();
1214 mCurrentState.barrierLayer_legacy = nullptr;
1215 mCurrentState.frameNumber_legacy = 0;
1216 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001217}
1218
Marissa Wallf58c14b2018-07-24 10:50:43 -07001219void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001220 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001221 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001222}
1223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224// ----------------------------------------------------------------------------
1225// pageflip handling...
1226// ----------------------------------------------------------------------------
1227
Robert Carr1f0a16a2016-10-24 16:27:39 -07001228bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001229 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001230 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001231 if (parent != nullptr && parent->isHiddenByPolicy()) {
1232 return true;
1233 }
1234 return s.flags & layer_state_t::eLayerHidden;
1235}
1236
David Sodman41fdfc92017-11-06 16:09:56 -08001237uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001238 // TODO: should we do something special if mSecure is set?
1239 if (mProtectedByApp) {
1240 // need a hardware-protected path to external video sink
1241 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001242 }
Riley Andrews03414a12014-07-01 14:22:59 -07001243 if (mPotentialCursor) {
1244 usage |= GraphicBuffer::USAGE_CURSOR;
1245 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001246 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001247 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001248}
1249
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001250void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001251 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001252 // Disable setting transform hint if the debug flag is set.
1253 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001254 // The transform hint is used to improve performance, but we can
1255 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001256 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001257 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001258 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001259 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001260 orientation = 0;
1261 }
1262 }
David Sodmaneb085e02017-10-05 18:49:04 -07001263 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001264}
1265
Mathias Agopian13127d82013-03-05 17:47:11 -08001266// ----------------------------------------------------------------------------
1267// debugging
1268// ----------------------------------------------------------------------------
1269
Marissa Wall61c58622018-07-18 10:12:20 -07001270// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001271LayerDebugInfo Layer::getLayerDebugInfo() const {
1272 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001273 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001274 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001275 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001276 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001277 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001278 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001279 info.mVisibleRegion = visibleRegion;
1280 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001281 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001282 info.mX = ds.active_legacy.transform.tx();
1283 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001284 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001285 info.mWidth = ds.active_legacy.w;
1286 info.mHeight = ds.active_legacy.h;
1287 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001288 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001289 info.mFlags = ds.flags;
1290 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001291 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001292 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1293 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1294 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1295 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001296 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001297 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001298 if (buffer != 0) {
1299 info.mActiveBufferWidth = buffer->getWidth();
1300 info.mActiveBufferHeight = buffer->getHeight();
1301 info.mActiveBufferStride = buffer->getStride();
1302 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001303 } else {
1304 info.mActiveBufferWidth = 0;
1305 info.mActiveBufferHeight = 0;
1306 info.mActiveBufferStride = 0;
1307 info.mActiveBufferFormat = 0;
1308 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001309 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001310 info.mNumQueuedFrames = getQueuedFrameCount();
1311 info.mRefreshPending = isBufferLatched();
1312 info.mIsOpaque = isOpaque(ds);
1313 info.mContentDirty = contentDirty;
1314 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001315}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001316
Yiwei Zhang5434a782018-12-05 18:06:32 -08001317void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001318 result.append("-------------------------------");
1319 result.append("-------------------------------");
1320 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001321 result.append(" Layer name\n");
1322 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001323 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001324 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001325 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001326 result.append(" Disp Frame (LTRB) | ");
1327 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001328 result.append("-------------------------------");
1329 result.append("-------------------------------");
1330 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001331}
1332
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001333void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1334 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1335 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001336 return;
1337 }
1338
Yiwei Zhang5434a782018-12-05 18:06:32 -08001339 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001340 if (mName.length() > 77) {
1341 std::string shortened;
1342 shortened.append(mName.string(), 36);
1343 shortened.append("[...]");
1344 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001345 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001346 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001347 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001348 }
1349
Yiwei Zhang5434a782018-12-05 18:06:32 -08001350 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001351
Alec Mourib416efd2018-09-06 21:01:59 +00001352 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001353 const auto& compositionState = outputLayer->getState();
1354
Chia-I Wu1e043612018-03-01 09:45:09 -08001355 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001356 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001357 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001358 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001359 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001360 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001361 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1362 StringAppendF(&result, "%10s | ",
1363 toString(getCompositionLayer() ? compositionState.bufferTransform
1364 : static_cast<Hwc2::Transform>(0))
1365 .c_str());
1366 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001367 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001368 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001369 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1370 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001371
Yichi Chen6ca35192018-05-29 12:20:43 +08001372 result.append("- - - - - - - - - - - - - - - -");
1373 result.append("- - - - - - - - - - - - - - - -");
1374 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001375}
Dan Stozae22aec72016-08-01 13:20:59 -07001376
Yiwei Zhang5434a782018-12-05 18:06:32 -08001377void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001378 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001379}
1380
Svetoslavd85084b2014-03-20 10:28:31 -07001381void Layer::clearFrameStats() {
1382 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001383}
1384
Jamie Gennis6547ff42013-07-16 20:12:42 -07001385void Layer::logFrameStats() {
1386 mFrameTracker.logAndResetStats(mName);
1387}
1388
Svetoslavd85084b2014-03-20 10:28:31 -07001389void Layer::getFrameStats(FrameStats* outStats) const {
1390 mFrameTracker.getStats(outStats);
1391}
1392
Yiwei Zhang5434a782018-12-05 18:06:32 -08001393void Layer::dumpFrameEvents(std::string& result) {
1394 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001395 Mutex::Autolock lock(mFrameEventHistoryMutex);
1396 mFrameEventHistory.checkFencesForCompletion();
1397 mFrameEventHistory.dump(result);
1398}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001399
Brian Anderson5ea5e592016-12-01 16:54:33 -08001400void Layer::onDisconnect() {
1401 Mutex::Autolock lock(mFrameEventHistoryMutex);
1402 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001403 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001404}
1405
Brian Anderson3890c392016-07-25 12:48:08 -07001406void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001407 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001408 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001409 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1410 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001411 }
1412
Brian Andersond6927fb2016-07-23 23:37:30 -07001413 Mutex::Autolock lock(mFrameEventHistoryMutex);
1414 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001415 // If there are any unsignaled fences in the aquire timeline at this
1416 // point, the previously queued frame hasn't been latched yet. Go ahead
1417 // and try to get the signal time here so the syscall is taken out of
1418 // the main thread's critical path.
1419 mAcquireTimeline.updateSignalTimes();
1420 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001421 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001422 mFrameEventHistory.addQueue(*newTimestamps);
1423 }
1424
Brian Anderson3890c392016-07-25 12:48:08 -07001425 if (outDelta) {
1426 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001427 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001428}
Dan Stozae77c7662016-05-13 11:37:28 -07001429
Chia-I Wu98f1c102017-05-30 14:54:08 -07001430size_t Layer::getChildrenCount() const {
1431 size_t count = 0;
1432 for (const sp<Layer>& child : mCurrentChildren) {
1433 count += 1 + child->getChildrenCount();
1434 }
1435 return count;
1436}
1437
Robert Carr1f0a16a2016-10-24 16:27:39 -07001438void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001439 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001440 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001441
Robert Carr1f0a16a2016-10-24 16:27:39 -07001442 mCurrentChildren.add(layer);
1443 layer->setParent(this);
1444}
1445
1446ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001447 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001448 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001449
Robert Carr1323c952019-01-28 18:13:27 -08001450 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001451 return mCurrentChildren.remove(layer);
1452}
1453
Robert Carr1db73f62016-12-21 12:58:51 -08001454bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1455 sp<Handle> handle = nullptr;
1456 sp<Layer> newParent = nullptr;
1457 if (newParentHandle == nullptr) {
1458 return false;
1459 }
1460 handle = static_cast<Handle*>(newParentHandle.get());
1461 newParent = handle->owner.promote();
1462 if (newParent == nullptr) {
1463 ALOGE("Unable to promote Layer handle");
1464 return false;
1465 }
1466
chaviw5aedec92018-10-22 10:40:38 -07001467 if (attachChildren()) {
1468 setTransactionFlags(eTransactionNeeded);
1469 }
Robert Carr1db73f62016-12-21 12:58:51 -08001470 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001471 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001472 }
1473 mCurrentChildren.clear();
1474
1475 return true;
1476}
1477
Robert Carr15eae092018-03-23 13:43:53 -07001478void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001479 for (const sp<Layer>& child : mDrawingChildren) {
1480 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001481 child->computeBounds(newParent->mBounds,
1482 newParent->getTransformWithScale(
1483 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001484 }
1485}
1486
chaviwf1961f72017-09-18 16:41:07 -07001487bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001488 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001489
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001490 // While layers are detached, we allow most operations
1491 // and simply halt performing the actual transaction. However
1492 // for reparent != null we would enter the mRemovedFromCurrentState
1493 // state, regardless of whether doTransaction was called, and
1494 // so we need to prevent the update here.
1495 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001496 return false;
1497 }
1498
Robert Carr54cf5b12019-01-25 14:02:28 -08001499 sp<Layer> newParent;
1500 if (newParentHandle != nullptr) {
1501 auto handle = static_cast<Handle*>(newParentHandle.get());
1502 newParent = handle->owner.promote();
1503 if (newParent == nullptr) {
1504 ALOGE("Unable to promote Layer handle");
1505 return false;
1506 }
1507 if (newParent == this) {
1508 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1509 return false;
1510 }
1511 }
1512
chaviwf1961f72017-09-18 16:41:07 -07001513 sp<Layer> parent = getParent();
1514 if (parent != nullptr) {
1515 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001516 }
1517
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001518 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001519 newParent->addChild(this);
1520 if (!newParent->isRemovedFromCurrentState()) {
1521 addToCurrentState();
1522 } else {
1523 onRemovedFromCurrentState();
1524 }
1525
1526 if (mLayerDetached) {
1527 mLayerDetached = false;
1528 callSetTransactionFlags = true;
1529 }
1530 } else {
1531 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001532 }
1533
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001534 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001535 setTransactionFlags(eTransactionNeeded);
1536 }
chaviw06178942017-07-27 10:25:59 -07001537 return true;
1538}
1539
Robert Carr9524cb32017-02-13 11:32:32 -08001540bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001541 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001542 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001543 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001544 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001545 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001546 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001547 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001548 }
Robert Carr7f619b22017-11-06 12:56:35 -08001549 }
Robert Carr9524cb32017-02-13 11:32:32 -08001550
1551 return true;
1552}
1553
chaviw5aedec92018-10-22 10:40:38 -07001554bool Layer::attachChildren() {
1555 bool changed = false;
1556 for (const sp<Layer>& child : mCurrentChildren) {
1557 sp<Client> parentClient = mClientRef.promote();
1558 sp<Client> client(child->mClientRef.promote());
1559 if (client != nullptr && parentClient != client) {
1560 if (child->mLayerDetached) {
1561 child->mLayerDetached = false;
1562 changed = true;
1563 }
1564 changed |= child->attachChildren();
1565 }
1566 }
1567
1568 return changed;
1569}
1570
Peiyong Lind3788632018-09-18 16:01:31 -07001571bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001572 static const mat4 identityMatrix = mat4();
1573
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001574 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001575 return false;
1576 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001577 ++mCurrentState.sequence;
1578 mCurrentState.colorTransform = matrix;
1579 mCurrentState.hasColorTransform = matrix != identityMatrix;
1580 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001581 setTransactionFlags(eTransactionNeeded);
1582 return true;
1583}
1584
chaviwf66724d2018-11-28 16:35:21 -08001585mat4 Layer::getColorTransform() const {
1586 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1587 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1588 colorTransform = parent->getColorTransform() * colorTransform;
1589 }
1590 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001591}
1592
1593bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001594 bool hasColorTransform = getDrawingState().hasColorTransform;
1595 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1596 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1597 }
1598 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001599}
1600
Chia-I Wu11481472018-05-04 10:43:19 -07001601bool Layer::isLegacyDataSpace() const {
1602 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001603 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001604 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001605}
1606
Robert Carr1f0a16a2016-10-24 16:27:39 -07001607void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001608 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609}
1610
Robert Carr1f0a16a2016-10-24 16:27:39 -07001611int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001612 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001613}
1614
Robert Carr29abff82017-12-04 13:51:20 -08001615bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1616 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001617 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001618 return state.zOrderRelativeOf != nullptr;
1619}
1620
David Sodman41fdfc92017-11-06 16:09:56 -08001621__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001622 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001623 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1624 "makeTraversalList received invalid stateSet");
1625 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1626 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001627 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001628
Robert Carr29abff82017-12-04 13:51:20 -08001629 if (state.zOrderRelatives.size() == 0) {
1630 *outSkipRelativeZUsers = true;
1631 return children;
1632 }
1633
chaviwfd462612018-05-31 16:11:27 -07001634 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001635 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001636 sp<Layer> strongRelative = weakRelative.promote();
1637 if (strongRelative != nullptr) {
1638 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001639 }
1640 }
1641
Dan Stoza412903f2017-04-27 13:42:17 -07001642 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001643 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001644 if (childState.zOrderRelativeOf != nullptr) {
1645 continue;
1646 }
Robert Carrdb66e622017-04-10 16:55:57 -07001647 traverse.add(child);
1648 }
1649
1650 return traverse;
1651}
1652
Robert Carr1f0a16a2016-10-24 16:27:39 -07001653/**
Robert Carrdb66e622017-04-10 16:55:57 -07001654 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001655 */
Dan Stoza412903f2017-04-27 13:42:17 -07001656void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001657 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1658 // produce a new list for traversing, including our relatives, and not including our children
1659 // who are relatives of another surface. In the case that there are no relative Z,
1660 // makeTraversalList returns our children directly to avoid significant overhead.
1661 // However in this case we need to take the responsibility for filtering children which
1662 // are relatives of another surface here.
1663 bool skipRelativeZUsers = false;
1664 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001665
Robert Carr1f0a16a2016-10-24 16:27:39 -07001666 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001667 for (; i < list.size(); i++) {
1668 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001669 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1670 continue;
1671 }
1672
Robert Carrdb66e622017-04-10 16:55:57 -07001673 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001674 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001675 }
Dan Stoza412903f2017-04-27 13:42:17 -07001676 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677 }
Robert Carr29abff82017-12-04 13:51:20 -08001678
Dan Stoza412903f2017-04-27 13:42:17 -07001679 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001680 for (; i < list.size(); i++) {
1681 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001682
Robert Carr29abff82017-12-04 13:51:20 -08001683 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1684 continue;
1685 }
Dan Stoza412903f2017-04-27 13:42:17 -07001686 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001687 }
1688}
1689
1690/**
Robert Carrdb66e622017-04-10 16:55:57 -07001691 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001692 */
Dan Stoza412903f2017-04-27 13:42:17 -07001693void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1694 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001695 // See traverseInZOrder for documentation.
1696 bool skipRelativeZUsers = false;
1697 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001698
Robert Carr1f0a16a2016-10-24 16:27:39 -07001699 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001700 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001701 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001702
1703 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1704 continue;
1705 }
1706
Robert Carrdb66e622017-04-10 16:55:57 -07001707 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001708 break;
1709 }
Dan Stoza412903f2017-04-27 13:42:17 -07001710 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711 }
Dan Stoza412903f2017-04-27 13:42:17 -07001712 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001713 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001714 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001715
1716 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1717 continue;
1718 }
1719
Dan Stoza412903f2017-04-27 13:42:17 -07001720 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001721 }
1722}
1723
chaviw4b129c22018-04-09 16:19:43 -07001724LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1725 const std::vector<Layer*>& layersInTree) {
1726 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1727 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001728 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1729 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001730 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001731
chaviwfd462612018-05-31 16:11:27 -07001732 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001733 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1734 sp<Layer> strongRelative = weakRelative.promote();
1735 // Only add relative layers that are also descendents of the top most parent of the tree.
1736 // If a relative layer is not a descendent, then it should be ignored.
1737 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1738 traverse.add(strongRelative);
1739 }
1740 }
1741
1742 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001743 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001744 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1745 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1746 // the child here since it won't be added later as a relative.
1747 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1748 childState.zOrderRelativeOf.promote().get())) {
1749 continue;
1750 }
1751 traverse.add(child);
1752 }
1753
1754 return traverse;
1755}
1756
1757void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1758 LayerVector::StateSet stateSet,
1759 const LayerVector::Visitor& visitor) {
1760 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001761
1762 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001763 for (; i < list.size(); i++) {
1764 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001765 if (relative->getZ() >= 0) {
1766 break;
1767 }
chaviw4b129c22018-04-09 16:19:43 -07001768 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001769 }
chaviw4b129c22018-04-09 16:19:43 -07001770
chaviwa76b2712017-09-20 12:02:26 -07001771 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001772 for (; i < list.size(); i++) {
1773 const auto& relative = list[i];
1774 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001775 }
1776}
1777
chaviw4b129c22018-04-09 16:19:43 -07001778std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1779 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1780 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1781
1782 std::vector<Layer*> layersInTree = {this};
1783 for (size_t i = 0; i < children.size(); i++) {
1784 const auto& child = children[i];
1785 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1786 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1787 }
1788
1789 return layersInTree;
1790}
1791
1792void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1793 const LayerVector::Visitor& visitor) {
1794 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1795 std::sort(layersInTree.begin(), layersInTree.end());
1796 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1797}
1798
Peiyong Linefefaac2018-08-17 12:27:51 -07001799ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001800 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001801}
1802
chaviw13fdc492017-06-27 12:40:18 -07001803half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001804 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001805
chaviw13fdc492017-06-27 12:40:18 -07001806 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1807 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001808}
Robert Carr6452f122017-03-21 10:41:29 -07001809
chaviw13fdc492017-06-27 12:40:18 -07001810half4 Layer::getColor() const {
1811 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001812 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001813}
Robert Carr6452f122017-03-21 10:41:29 -07001814
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001815Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1816 const auto& p = mDrawingParent.promote();
1817 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001818 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001819 if (parentState.radius > 0) {
1820 ui::Transform t = getActiveTransform(getDrawingState());
1821 t = t.inverse();
1822 parentState.cropRect = t.transform(parentState.cropRect);
1823 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1824 // but a transform matrix can define horizontal and vertical scales.
1825 // Let's take the average between both of them and pass into the shader, practically we
1826 // never do this type of transformation on windows anyway.
1827 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1828 return parentState;
1829 }
1830 }
1831 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001832 return radius > 0 && getCrop(getDrawingState()).isValid()
1833 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1834 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001835}
1836
Robert Carr1f0a16a2016-10-24 16:27:39 -07001837void Layer::commitChildList() {
1838 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1839 const auto& child = mCurrentChildren[i];
1840 child->commitChildList();
1841 }
1842 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001843 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844}
1845
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001846static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1847 if (weakBinderHandle == nullptr) {
1848 return nullptr;
1849 }
1850 sp<IBinder> binderHandle = weakBinderHandle.promote();
1851 if (binderHandle == nullptr) {
1852 return nullptr;
1853 }
1854 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1855 if (handle == nullptr) {
1856 return nullptr;
1857 }
1858 return handle->owner;
1859}
1860
Robert Carr720e5062018-07-30 17:45:14 -07001861void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001862 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001863 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001864 mCurrentState.modified = true;
1865 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001866 setTransactionFlags(eTransactionNeeded);
1867}
1868
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001869void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1870 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001871 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1872 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001873 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001874
Peiyong Linefefaac2018-08-17 12:27:51 -07001875 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001876 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001877
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001878 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1879 layerInfo->set_id(sequence);
1880 layerInfo->set_name(getName().c_str());
1881 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001882
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001883 for (const auto& child : children) {
1884 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001885 }
chaviw1d044282017-09-27 12:19:28 -07001886
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001887 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1888 sp<Layer> strongRelative = weakRelative.promote();
1889 if (strongRelative != nullptr) {
1890 layerInfo->add_relatives(strongRelative->sequence);
1891 }
chaviwadc40c22018-07-10 16:57:27 -07001892 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001893
1894 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1895 [&]() { return layerInfo->mutable_transparent_region(); });
1896 LayerProtoHelper::writeToProto(visibleRegion,
1897 [&]() { return layerInfo->mutable_visible_region(); });
1898 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1899 [&]() { return layerInfo->mutable_damage_region(); });
1900
1901 layerInfo->set_layer_stack(getLayerStack());
1902 layerInfo->set_z(state.z);
1903
1904 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1905 [&]() { return layerInfo->mutable_position(); });
1906
1907 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1908 [&]() {
1909 return layerInfo->mutable_requested_position();
1910 });
1911
1912 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1913 [&]() { return layerInfo->mutable_size(); });
1914
1915 LayerProtoHelper::writeToProto(state.crop_legacy,
1916 [&]() { return layerInfo->mutable_crop(); });
1917 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1918
1919 layerInfo->set_is_opaque(isOpaque(state));
1920 layerInfo->set_invalidate(contentDirty);
Peiyong Lin51598552019-04-17 14:09:22 -07001921 layerInfo->set_is_protected(isProtected());
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001922
1923 // XXX (b/79210409) mCurrentDataSpace is not protected
1924 layerInfo->set_dataspace(
1925 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1926
1927 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1928 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1929 LayerProtoHelper::writeToProto(state.color,
1930 [&]() { return layerInfo->mutable_requested_color(); });
1931 layerInfo->set_flags(state.flags);
1932
1933 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1934 LayerProtoHelper::writeToProto(requestedTransform,
1935 layerInfo->mutable_requested_transform());
1936
1937 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1938 if (parent != nullptr) {
1939 layerInfo->set_parent(parent->sequence);
1940 } else {
1941 layerInfo->set_parent(-1);
1942 }
1943
1944 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1945 if (zOrderRelativeOf != nullptr) {
1946 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1947 } else {
1948 layerInfo->set_z_order_relative_of(-1);
1949 }
1950
1951 auto buffer = mActiveBuffer;
1952 if (buffer != nullptr) {
1953 LayerProtoHelper::writeToProto(buffer,
1954 [&]() { return layerInfo->mutable_active_buffer(); });
1955 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1956 layerInfo->mutable_buffer_transform());
1957 }
1958
1959 layerInfo->set_queued_frames(getQueuedFrameCount());
1960 layerInfo->set_refresh_pending(isBufferLatched());
1961 layerInfo->set_curr_frame(mCurrentFrameNumber);
1962 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1963
1964 for (const auto& pendingState : mPendingStates) {
1965 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1966 if (barrierLayer != nullptr) {
1967 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1968 barrierLayerProto->set_id(barrierLayer->sequence);
1969 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1970 }
1971 }
1972 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001973 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001974
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001975 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1976 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1977 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001978 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001979
1980 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1981 auto protoMap = layerInfo->mutable_metadata();
1982 for (const auto& entry : state.metadata.mMap) {
1983 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1984 }
1985 LayerProtoHelper::writeToProto(mEffectiveTransform,
1986 layerInfo->mutable_effective_transform());
1987 LayerProtoHelper::writeToProto(mSourceBounds,
1988 [&]() { return layerInfo->mutable_source_bounds(); });
1989 LayerProtoHelper::writeToProto(mScreenBounds,
1990 [&]() { return layerInfo->mutable_screen_bounds(); });
1991 }
chaviw1d044282017-09-27 12:19:28 -07001992}
1993
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001994void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1995 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001996 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1997 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001998 return;
1999 }
2000
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002001 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002002
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002003 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002004
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002005 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002006 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002007
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002008 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002009 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002010
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002011 const int32_t transform =
2012 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002013 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002014
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002015 const int32_t compositionType =
2016 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2017 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002018 layerInfo->set_hwc_composition_type(compositionType);
2019
2020 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2021 static_cast<BufferLayer*>(this)->isProtected()) {
2022 layerInfo->set_is_protected(true);
2023 } else {
2024 layerInfo->set_is_protected(false);
2025 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002026}
2027
Robert Carr2e102c92018-10-23 12:11:15 -07002028bool Layer::isRemovedFromCurrentState() const {
2029 return mRemovedFromCurrentState;
2030}
2031
Arthur Hungd20b2702019-01-14 18:16:16 +08002032InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002033 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002034
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002035 if (info.displayId == ADISPLAY_ID_NONE) {
2036 info.displayId = mDrawingState.layerStack;
2037 }
2038
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002039 ui::Transform t = getTransform();
2040 const float xScale = t.sx();
2041 const float yScale = t.sy();
Arthur Hung118b1142019-05-08 21:25:59 +08002042 float xSurfaceInset = info.surfaceInset;
2043 float ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002044 if (xScale != 1.0f || yScale != 1.0f) {
2045 info.windowXScale *= 1.0f / xScale;
2046 info.windowYScale *= 1.0f / yScale;
2047 info.touchableRegion.scaleSelf(xScale, yScale);
Arthur Hung118b1142019-05-08 21:25:59 +08002048 xSurfaceInset *= xScale;
2049 ySurfaceInset *= yScale;
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002050 }
Robert Carre07e1032018-11-26 12:55:53 -08002051
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002052 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002053 // If this is a portal window, set the touchableRegion to the layerBounds.
2054 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2055 ? getBufferSize(getDrawingState())
2056 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002057 if (!layerBounds.isValid()) {
2058 layerBounds = getCroppedBufferSize(getDrawingState());
2059 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002060 layerBounds = t.transform(layerBounds);
Arthur Hung118b1142019-05-08 21:25:59 +08002061 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002062
Arthur Hungd20b2702019-01-14 18:16:16 +08002063 // Input coordinate should match the layer bounds.
2064 info.frameLeft = layerBounds.left;
2065 info.frameTop = layerBounds.top;
2066 info.frameRight = layerBounds.right;
2067 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002068
2069 // Position the touchable region relative to frame screen location and restrict it to frame
2070 // bounds.
2071 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002072 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002073
2074 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2075 if (info.replaceTouchableRegionWithCrop) {
2076 if (cropLayer == nullptr) {
2077 info.touchableRegion = Region(Rect{mScreenBounds});
2078 } else {
2079 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2080 }
2081 } else if (cropLayer != nullptr) {
2082 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2083 }
2084
Robert Carr720e5062018-07-30 17:45:14 -07002085 return info;
2086}
2087
2088bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002089 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002090}
2091
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002092std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2093 return nullptr;
2094}
2095
chaviw3e727cd2019-01-31 13:41:05 -08002096bool Layer::canReceiveInput() const {
2097 return isVisible();
2098}
2099
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002100compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2101 const sp<const DisplayDevice>& display) const {
2102 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2103}
2104
Mathias Agopian13127d82013-03-05 17:47:11 -08002105// ---------------------------------------------------------------------------
2106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002108
2109#if defined(__gl_h_)
2110#error "don't include gl/gl.h in this file"
2111#endif
2112
2113#if defined(__gl2_h_)
2114#error "don't include gl2/gl2.h in this file"
2115#endif