blob: 1716c17e51c62df7e8c0a81c52abaa672d5d9b5f [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080032#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080034#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
42#include <renderengine/RenderEngine.h>
43#include <ui/DebugUtils.h>
44#include <ui/GraphicBuffer.h>
45#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/Errors.h>
47#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080048#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Yiwei Zhang60d1a192018-03-07 14:52:28 -080052#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080053#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020054#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080058#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070059#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070060#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080062#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070063
David Sodman41fdfc92017-11-06 16:09:56 -080064#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066namespace android {
67
Yiwei Zhang5434a782018-12-05 18:06:32 -080068using base::StringAppendF;
69
Lloyd Piquef1c675b2018-09-12 20:45:39 -070070std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072Layer::Layer(const LayerCreationArgs& args)
Lloyd Piquefa8fd6b2019-01-29 18:42:24 -080073 : mFlinger(args.flinger), mName(args.name), mClientRef(args.client) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070075
76 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070077 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
78 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
79 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Dan Stozaf7ba41a2017-05-10 15:11:11 -070081 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082
Lloyd Pique0449b0f2018-12-20 16:23:45 -080083 mCurrentState.active_legacy.w = args.w;
84 mCurrentState.active_legacy.h = args.h;
85 mCurrentState.flags = layerFlags;
86 mCurrentState.active_legacy.transform.set(0, 0);
87 mCurrentState.crop_legacy.makeInvalid();
88 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
89 mCurrentState.z = 0;
90 mCurrentState.color.a = 1.0f;
91 mCurrentState.layerStack = 0;
92 mCurrentState.sequence = 0;
93 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080094 mCurrentState.active.w = UINT32_MAX;
95 mCurrentState.active.h = UINT32_MAX;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.cornerRadius = 0.0f;
105 mCurrentState.api = -1;
106 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800107 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800108 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109
110 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700112
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800113 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700114 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800115 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200116 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700117
118 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700119}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700120
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800122 sp<Client> c(mClientRef.promote());
123 if (c != 0) {
124 c->detachLayer(this);
125 }
126
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000127 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700128 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian13127d82013-03-05 17:47:11 -0800131// ---------------------------------------------------------------------------
132// callbacks
133// ---------------------------------------------------------------------------
134
David Sodmaneb085e02017-10-05 18:49:04 -0700135/*
136 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
137 * Layer. So, the implementation is done in BufferLayer. When called on a
138 * ColorLayer object, it's essentially a NOP.
139 */
David Sodmaneb085e02017-10-05 18:49:04 -0700140void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800141
Chia-I Wuc6657022017-08-15 11:18:17 -0700142void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700143 mRemovedFromCurrentState = true;
144
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
146 if (mCurrentState.zOrderRelativeOf != nullptr) {
147 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
148 if (strongRelative != nullptr) {
149 strongRelative->removeZOrderRelative(this);
150 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
chaviw606e5cf2019-03-01 10:12:10 -0800152 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700153 }
Rob Carr4bba3702018-10-08 21:53:30 +0000154
Robert Carr2e102c92018-10-23 12:11:15 -0700155 // Since we are no longer reachable from CurrentState SurfaceFlinger
156 // will no longer invoke doTransaction for us, and so we will
157 // never finish applying transactions. We signal the sync point
158 // now so that another layer will not become indefinitely
159 // blocked.
160 for (auto& point: mRemoteSyncPoints) {
161 point->setTransactionApplied();
162 }
163 mRemoteSyncPoints.clear();
164
165 {
166 Mutex::Autolock syncLock(mLocalSyncPointMutex);
167 for (auto& point : mLocalSyncPoints) {
168 point->setFrameAvailable();
169 }
170 mLocalSyncPoints.clear();
171 }
172
Chia-I Wuc6657022017-08-15 11:18:17 -0700173 for (const auto& child : mCurrentChildren) {
174 child->onRemovedFromCurrentState();
175 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800176
177 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700178}
Chia-I Wu38512252017-05-17 14:36:16 -0700179
chaviw61626f22018-11-15 16:26:27 -0800180void Layer::addToCurrentState() {
181 mRemovedFromCurrentState = false;
182
183 for (const auto& child : mCurrentChildren) {
184 child->addToCurrentState();
185 }
186}
187
Mathias Agopian13127d82013-03-05 17:47:11 -0800188// ---------------------------------------------------------------------------
189// set-up
190// ---------------------------------------------------------------------------
191
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700192const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800193 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194}
195
chaviw13fdc492017-06-27 12:40:18 -0700196bool Layer::getPremultipledAlpha() const {
197 return mPremultipliedAlpha;
198}
199
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700200sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700202 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800203}
204
205// ---------------------------------------------------------------------------
206// h/w composer set-up
207// ---------------------------------------------------------------------------
208
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800209bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
210 auto outputLayer = findOutputLayerForDisplay(displayDevice);
211 LOG_FATAL_IF(!outputLayer);
212 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
213}
214
215HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
216 auto outputLayer = findOutputLayerForDisplay(displayDevice);
217 if (!outputLayer || !outputLayer->getState().hwc) {
218 return nullptr;
219 }
220 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700221}
Steven Thomasb02664d2017-07-26 18:48:28 -0700222
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800223Rect Layer::getContentCrop() const {
224 // this is the crop rectangle that applies to the buffer
225 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700226 Rect crop;
227 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800228 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700229 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800230 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800231 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800232 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700233 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800234 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700235 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700237 return crop;
238}
239
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700240static Rect reduce(const Rect& win, const Region& exclude) {
241 if (CC_LIKELY(exclude.isEmpty())) {
242 return win;
243 }
244 if (exclude.isRect()) {
245 return win.reduce(exclude.getBounds());
246 }
247 return Region(win).subtract(exclude).getBounds();
248}
249
Dan Stoza80d61162017-12-20 15:57:52 -0800250static FloatRect reduce(const FloatRect& win, const Region& exclude) {
251 if (CC_LIKELY(exclude.isEmpty())) {
252 return win;
253 }
254 // Convert through Rect (by rounding) for lack of FloatRegion
255 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
256}
257
Vishnu Nair4351ad52019-02-11 14:13:02 -0800258Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800259 if (!reduceTransparentRegion) {
260 return Rect{mScreenBounds};
261 }
262
263 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800264 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800265 // Transform to screen space.
266 bounds = t.transform(bounds);
267 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700268}
269
Vishnu Nair4351ad52019-02-11 14:13:02 -0800270FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000271 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800272 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700273}
274
Vishnu Nairf0c28512019-02-08 12:40:28 -0800275FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
276 // Subtract the transparent region and snap to the bounds.
277 return reduce(mBounds, activeTransparentRegion);
278}
279
Vishnu Nairc652ff82019-03-15 12:48:54 -0700280ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800281 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
282 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700283 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800284 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700285 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800286 }
287
Marissa Wall290ad082019-03-06 13:23:47 -0800288 // If the layer is a buffer state layer, the active width and height
289 // could be infinite. In that case, return the effective transform.
290 const uint32_t activeWidth = getActiveWidth(getDrawingState());
291 const uint32_t activeHeight = getActiveHeight(getDrawingState());
292 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700293 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800294 }
295
Vishnu Nairc652ff82019-03-15 12:48:54 -0700296 int bufferWidth = mActiveBuffer->getWidth();
297 int bufferHeight = mActiveBuffer->getHeight();
298
299 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
300 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800301 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700302
Marissa Wall290ad082019-03-06 13:23:47 -0800303 float sx = activeWidth / static_cast<float>(bufferWidth);
304 float sy = activeHeight / static_cast<float>(bufferHeight);
305
Vishnu Nair4351ad52019-02-11 14:13:02 -0800306 ui::Transform extraParentScaling;
307 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700308 return extraParentScaling;
309}
310
311ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
312 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
313 // treat child surfaces as pixels in the parent surface.
314 if (!isFixedSize() || !mActiveBuffer) {
315 return mEffectiveTransform;
316 }
317 return mEffectiveTransform * bufferScaleTransform;
318}
319
320FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
321 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
322 // cropped to its parent layer after any buffer transform scaling is applied.
323 if (!isFixedSize() || !mActiveBuffer) {
324 return mBounds;
325 }
326 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800327}
328
329void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
330 const State& s(getDrawingState());
331
332 // Calculate effective layer transform
333 mEffectiveTransform = parentTransform * getActiveTransform(s);
334
335 // Transform parent bounds to layer space
336 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
337
Vishnu Nairc652ff82019-03-15 12:48:54 -0700338 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800339 mSourceBounds = computeSourceBounds(parentBounds);
340
341 // Calculate bounds by croping diplay frame with layer crop and parent bounds
342 FloatRect bounds = mSourceBounds;
343 const Rect layerCrop = getCrop(s);
344 if (!layerCrop.isEmpty()) {
345 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
346 }
347 bounds = bounds.intersect(parentBounds);
348
349 mBounds = bounds;
350 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700351
352 // Add any buffer scaling to the layer's children.
353 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800354 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700355 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
356 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800357 }
358}
359
Vishnu Nair60356342018-11-13 13:00:45 -0800360Rect Layer::getCroppedBufferSize(const State& s) const {
361 Rect size = getBufferSize(s);
362 Rect crop = getCrop(s);
363 if (!crop.isEmpty() && size.isValid()) {
364 size.intersect(crop, &size);
365 } else if (!crop.isEmpty()) {
366 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700367 }
Vishnu Nair60356342018-11-13 13:00:45 -0800368 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800369}
370
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700371void Layer::setupRoundedCornersCropCoordinates(Rect win,
372 const FloatRect& roundedCornersCrop) const {
373 // Translate win by the rounded corners rect coordinates, to have all values in
374 // layer coordinate space.
375 win.left -= roundedCornersCrop.left;
376 win.right -= roundedCornersCrop.left;
377 win.top -= roundedCornersCrop.top;
378 win.bottom -= roundedCornersCrop.top;
379
Lloyd Pique0b785d82018-12-04 17:25:27 -0800380 renderengine::Mesh::VertexArray<vec2> cropCoords(
381 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700382 cropCoords[0] = vec2(win.left, win.top);
383 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
384 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
385 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700386}
387
Lloyd Piquea83776c2019-01-29 18:42:32 -0800388void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
389 const auto& drawingState{getDrawingState()};
390 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500391 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800392 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800393 blendMode =
394 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800395 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800396
Lloyd Piquea83776c2019-01-29 18:42:32 -0800397 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
398 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700399 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400400 if (parent.get()) {
401 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800402 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
403 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
404 if (parentType >= 0 || parentAppId >= 0) {
405 type = parentType;
406 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700407 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400408 }
409
Lloyd Piquea83776c2019-01-29 18:42:32 -0800410 compositionState.geomLayerTransform = getTransform();
411 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
412 compositionState.geomBufferSize = getBufferSize(drawingState);
413 compositionState.geomContentCrop = getContentCrop();
414 compositionState.geomCrop = getCrop(drawingState);
415 compositionState.geomBufferTransform = mCurrentTransform;
416 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
417 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
418 compositionState.geomLayerBounds = mBounds;
419 compositionState.geomUsesSourceCrop = usesSourceCrop();
420 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700421
Lloyd Piquea83776c2019-01-29 18:42:32 -0800422 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
423 compositionState.alpha = alpha;
424 compositionState.type = type;
425 compositionState.appId = appId;
426}
David Sodmanba340492018-08-05 21:51:33 -0700427
Lloyd Piquea83776c2019-01-29 18:42:32 -0800428void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
429 bool includeGeometry) const {
430 if (includeGeometry) {
431 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700432 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800433}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700434
Lloyd Piquea83776c2019-01-29 18:42:32 -0800435const char* Layer::getDebugName() const {
436 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800437}
438
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800439void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
440 const auto outputLayer = findOutputLayerForDisplay(display);
441 LOG_FATAL_IF(!outputLayer);
442 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800443}
Dan Stozaee44edd2015-03-23 15:50:23 -0700444
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800445bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
446 const auto outputLayer = findOutputLayerForDisplay(display);
447 LOG_FATAL_IF(!outputLayer);
448 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800449}
450
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700451void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800452 const auto outputLayer = findOutputLayerForDisplay(display);
453 LOG_FATAL_IF(!outputLayer);
454
455 if (!outputLayer->getState().hwc ||
456 (*outputLayer->getState().hwc).hwcCompositionType !=
457 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800458 return;
459 }
460
461 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800462 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800463
464 // Apply the layer's transform, followed by the display's global transform
465 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800466 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800467 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700468 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800469 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700470 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700471 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800472 auto position = displayTransform.transform(frame);
473
Dominik Laskowski7e045462018-05-30 13:02:02 -0700474 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800475 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800476 ALOGE_IF(error != HWC2::Error::None,
477 "[%s] Failed to set cursor position "
478 "to (%d, %d): %s (%d)",
479 mName.string(), position.left, position.top, to_string(error).c_str(),
480 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800481}
Riley Andrews03414a12014-07-01 14:22:59 -0700482
Mathias Agopian13127d82013-03-05 17:47:11 -0800483// ---------------------------------------------------------------------------
484// drawing...
485// ---------------------------------------------------------------------------
486
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000487bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800488 Region& clearRegion, const bool supportProtectedContent,
489 renderengine::LayerSettings& layer) {
490 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800491}
492
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000493bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800494 Region& clearRegion, const bool supportProtectedContent,
495 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000496 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800497 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000498}
499
500bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
501 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800502 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000503 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800504 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000505 half alpha = getAlpha();
506 layer.geometry.boundaries = bounds;
507 if (useIdentityTransform) {
508 layer.geometry.positionTransform = mat4();
509 } else {
510 const ui::Transform transform = getTransform();
511 mat4 m;
512 m[0][0] = transform[0][0];
513 m[0][1] = transform[0][1];
514 m[0][3] = transform[0][2];
515 m[1][0] = transform[1][0];
516 m[1][1] = transform[1][1];
517 m[1][3] = transform[1][2];
518 m[3][0] = transform[2][0];
519 m[3][1] = transform[2][1];
520 m[3][3] = transform[2][2];
521 layer.geometry.positionTransform = m;
522 }
523
524 if (hasColorTransform()) {
525 layer.colorTransform = getColorTransform();
526 }
527
528 const auto roundedCornerState = getRoundedCornerState();
529 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
530 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
531
532 layer.alpha = alpha;
533 layer.sourceDataspace = mCurrentDataSpace;
534 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800535}
536
David Sodman41fdfc92017-11-06 16:09:56 -0800537void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
538 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800539 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800540 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700541 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800542 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800543}
544
David Sodmanc1498e62018-09-12 14:36:26 -0700545void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
546 clearWithOpenGL(renderArea, 0, 0, 0, 0);
547}
548
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800549void Layer::setCompositionType(const sp<const DisplayDevice>& display,
550 Hwc2::IComposerClient::Composition type) {
551 const auto outputLayer = findOutputLayerForDisplay(display);
552 LOG_FATAL_IF(!outputLayer);
553 LOG_FATAL_IF(!outputLayer->getState().hwc);
554 auto& compositionState = outputLayer->editState();
555
556 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
557 toString(type).c_str(), 1);
558 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700559 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800560 (*compositionState.hwc).hwcCompositionType = type;
561
562 auto error = (*compositionState.hwc)
563 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
564 ALOGE_IF(error != HWC2::Error::None,
565 "[%s] Failed to set "
566 "composition type %s: %s (%d)",
567 mName.string(), toString(type).c_str(), to_string(error).c_str(),
568 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569 }
570}
571
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800572Hwc2::IComposerClient::Composition Layer::getCompositionType(
573 const sp<const DisplayDevice>& display) const {
574 const auto outputLayer = findOutputLayerForDisplay(display);
575 LOG_FATAL_IF(!outputLayer);
576 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
577 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800578}
579
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800580bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
581 const auto outputLayer = findOutputLayerForDisplay(display);
582 LOG_FATAL_IF(!outputLayer);
583 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585
Dan Stozacac35382016-01-27 12:21:06 -0800586bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
587 if (point->getFrameNumber() <= mCurrentFrameNumber) {
588 // Don't bother with a SyncPoint, since we've already latched the
589 // relevant frame
590 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700591 }
Robert Carr2e102c92018-10-23 12:11:15 -0700592 if (isRemovedFromCurrentState()) {
593 return false;
594 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700595
Dan Stozacac35382016-01-27 12:21:06 -0800596 Mutex::Autolock lock(mLocalSyncPointMutex);
597 mLocalSyncPoints.push_back(point);
598 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700599}
600
Mathias Agopian13127d82013-03-05 17:47:11 -0800601// ----------------------------------------------------------------------------
602// local state
603// ----------------------------------------------------------------------------
604
Peiyong Lin833074a2018-08-28 11:53:54 -0700605void Layer::computeGeometry(const RenderArea& renderArea,
606 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700607 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700608 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800609 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700610
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000611 vec2 lt = vec2(win.left, win.top);
612 vec2 lb = vec2(win.left, win.bottom);
613 vec2 rb = vec2(win.right, win.bottom);
614 vec2 rt = vec2(win.right, win.top);
615
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800616 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000617 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700618 lt = layerTransform.transform(lt);
619 lb = layerTransform.transform(lb);
620 rb = layerTransform.transform(rb);
621 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000622 }
623
Peiyong Lin833074a2018-08-28 11:53:54 -0700624 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700625 position[0] = renderAreaTransform.transform(lt);
626 position[1] = renderAreaTransform.transform(lb);
627 position[2] = renderAreaTransform.transform(rb);
628 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800629}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800630
David Sodman41fdfc92017-11-06 16:09:56 -0800631bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800632 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700633 return (s.flags & layer_state_t::eLayerSecure);
634}
635
Mathias Agopian13127d82013-03-05 17:47:11 -0800636void Layer::setVisibleRegion(const Region& visibleRegion) {
637 // always called from main thread
638 this->visibleRegion = visibleRegion;
639}
640
641void Layer::setCoveredRegion(const Region& coveredRegion) {
642 // always called from main thread
643 this->coveredRegion = coveredRegion;
644}
645
David Sodman41fdfc92017-11-06 16:09:56 -0800646void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800647 // always called from main thread
648 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
649}
650
Robert Carre5f4f692018-01-12 13:12:28 -0800651void Layer::clearVisibilityRegions() {
652 visibleRegion.clear();
653 visibleNonTransparentRegion.clear();
654 coveredRegion.clear();
655}
656
Mathias Agopian13127d82013-03-05 17:47:11 -0800657// ----------------------------------------------------------------------------
658// transaction
659// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800660
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800661void Layer::pushPendingState() {
662 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700663 return;
664 }
665
Dan Stoza7dde5992015-05-22 09:51:44 -0700666 // If this transaction is waiting on the receipt of a frame, generate a sync
667 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700668 // We don't allow installing sync points after we are removed from the current state
669 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800670 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
671 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800672 if (barrierLayer == nullptr) {
673 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700674 // If we can't promote the layer we are intended to wait on,
675 // then it is expired or otherwise invalid. Allow this transaction
676 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800677 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800678 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800679 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800680 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800681 mRemoteSyncPoints.push_back(std::move(syncPoint));
682 } else {
683 // We already missed the frame we're supposed to synchronize
684 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800685 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800686 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 }
688
Dan Stoza7dde5992015-05-22 09:51:44 -0700689 // Wake us up to check if the frame has been received
690 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700691 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700692 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800693 mPendingStates.push_back(mCurrentState);
694 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700695}
696
Pablo Ceballos05289c22016-04-14 15:49:55 -0700697void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800698 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700699
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800700 mPendingStates.removeAt(0);
701 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700702}
703
Pablo Ceballos05289c22016-04-14 15:49:55 -0700704bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700705 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800706 while (!mPendingStates.empty()) {
707 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700708 if (mRemoteSyncPoints.empty()) {
709 // If we don't have a sync point for this, apply it anyway. It
710 // will be visually wrong, but it should keep us from getting
711 // into too much trouble.
712 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700713 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700714 stateUpdateAvailable = true;
715 continue;
716 }
717
Marissa Wallf58c14b2018-07-24 10:50:43 -0700718 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800719 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800720 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800721
722 // Signal our end of the sync point and then dispose of it
723 mRemoteSyncPoints.front()->setTransactionApplied();
724 mRemoteSyncPoints.pop_front();
725 continue;
726 }
727
Dan Stoza7dde5992015-05-22 09:51:44 -0700728 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
729 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700730 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700731 stateUpdateAvailable = true;
732
733 // Signal our end of the sync point and then dispose of it
734 mRemoteSyncPoints.front()->setTransactionApplied();
735 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800736 } else {
737 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700738 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700739 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700740 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700741 stateUpdateAvailable = true;
742 }
743 }
744
745 // If we still have pending updates, wake SurfaceFlinger back up and point
746 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800747 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700748 setTransactionFlags(eTransactionNeeded);
749 mFlinger->setTransactionFlags(eTraversalNeeded);
750 }
751
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800752 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700753 return stateUpdateAvailable;
754}
755
Marissa Wall61c58622018-07-18 10:12:20 -0700756uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000757 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758
Marissa Wall61c58622018-07-18 10:12:20 -0700759 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
760 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700761
David Sodmaneb085e02017-10-05 18:49:04 -0700762 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700763 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000764 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800765 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
766 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
767 " requested={ wh={%4u,%4u} }}\n"
768 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
769 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700770 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700771 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
772 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
773 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
774 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
775 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700776 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
777 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
778 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700780
Robert Carre392b552017-09-19 12:16:05 -0700781 // Don't let Layer::doTransaction update the drawing state
782 // if we have a pending resize, unless we are in fixed-size mode.
783 // the drawing state will be updated only once we receive a buffer
784 // with the correct size.
785 //
786 // In particular, we want to make sure the clip (which is part
787 // of the geometry state) is latched together with the size but is
788 // latched immediately when no resizing is involved.
789 //
790 // If a sideband stream is attached, however, we want to skip this
791 // optimization so that transactions aren't missed when a buffer
792 // never arrives
793 //
794 // In the case that we don't have a buffer we ignore other factors
795 // and avoid entering the resizePending state. At a high level the
796 // resizePending state is to avoid applying the state of the new buffer
797 // to the old buffer. However in the state where we don't have an old buffer
798 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700799 const bool resizePending =
800 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
801 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800802 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700803 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800804 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700805 flags |= eDontUpdateGeometryState;
806 }
807 }
808
Robert Carr7bf247e2017-05-18 14:02:49 -0700809 // Here we apply various requested geometry states, depending on our
810 // latching configuration. See Layer.h for a detailed discussion of
811 // how geometry latching is controlled.
812 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000813 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700814
815 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
816 // mode, which causes attributes which normally latch regardless of scaling mode,
817 // to be delayed. We copy the requested state to the active state making sure
818 // to respect these rules (again see Layer.h for a detailed discussion).
819 //
820 // There is an awkward asymmetry in the handling of the crop states in the position
821 // states, as can be seen below. Largely this arises from position and transform
822 // being stored in the same data structure while having different latching rules.
823 // b/38182305
824 //
Marissa Wall61c58622018-07-18 10:12:20 -0700825 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700826 // applyPendingStates in the presence of deferred transactions.
827 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700828 float tx = stateToCommit->active_legacy.transform.tx();
829 float ty = stateToCommit->active_legacy.transform.ty();
830 stateToCommit->active_legacy = stateToCommit->requested_legacy;
831 stateToCommit->active_legacy.transform.set(tx, ty);
832 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700833 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700834 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700835 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700836 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800837 }
838
Marissa Wall61c58622018-07-18 10:12:20 -0700839 return flags;
840}
841
842uint32_t Layer::doTransaction(uint32_t flags) {
843 ATRACE_CALL();
844
chaviw5aedec92018-10-22 10:40:38 -0700845 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800846 return flags;
847 }
848
849 if (mChildrenChanged) {
850 flags |= eVisibleRegion;
851 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700852 }
853
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800854 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000855 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700856 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800857 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700858 }
859
860 flags = doTransactionResize(flags, &c);
861
Alec Mourib416efd2018-09-06 21:01:59 +0000862 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700863
864 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800865 // invalidate and recompute the visible regions if needed
866 flags |= Layer::eVisibleRegion;
867 }
868
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700869 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800870 // invalidate and recompute the visible regions if needed
871 flags |= eVisibleRegion;
872 this->contentDirty = true;
873
874 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700875 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700876 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800877 }
878
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800879 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700880 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700882 }
883
Mathias Agopian13127d82013-03-05 17:47:11 -0800884 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700885 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800886 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800887 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888}
889
Pablo Ceballos05289c22016-04-14 15:49:55 -0700890void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700892}
893
Mathias Agopian13127d82013-03-05 17:47:11 -0800894uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800895 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800896}
897
898uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800899 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800900}
901
Robert Carr82364e32016-05-15 11:27:47 -0700902bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800903 if (mCurrentState.requested_legacy.transform.tx() == x &&
904 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800905 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800906 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700907
908 // We update the requested and active position simultaneously because
909 // we want to apply the position portion of the transform matrix immediately,
910 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800911 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700912 if (immediate && !mFreezeGeometryUpdates) {
913 // Here we directly update the active state
914 // unlike other setters, because we store it within
915 // the transform, but use different latching rules.
916 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800917 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700918 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700919 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700920
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800921 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800922 setTransactionFlags(eTransactionNeeded);
923 return true;
924}
Robert Carr82364e32016-05-15 11:27:47 -0700925
Robert Carr1f0a16a2016-10-24 16:27:39 -0700926bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
927 ssize_t idx = mCurrentChildren.indexOf(childLayer);
928 if (idx < 0) {
929 return false;
930 }
931 if (childLayer->setLayer(z)) {
932 mCurrentChildren.removeAt(idx);
933 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800934 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700935 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800936 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700937}
938
Robert Carr503c7042017-09-27 15:06:08 -0700939bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
940 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
941 ssize_t idx = mCurrentChildren.indexOf(childLayer);
942 if (idx < 0) {
943 return false;
944 }
945 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
946 mCurrentChildren.removeAt(idx);
947 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800948 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700949 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800950 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700951}
952
Robert Carrae060832016-11-28 10:51:00 -0800953bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800954 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
955 mCurrentState.sequence++;
956 mCurrentState.z = z;
957 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700958
959 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800960 if (mCurrentState.zOrderRelativeOf != nullptr) {
961 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700962 if (strongRelative != nullptr) {
963 strongRelative->removeZOrderRelative(this);
964 }
chaviw606e5cf2019-03-01 10:12:10 -0800965 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700966 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800967 setTransactionFlags(eTransactionNeeded);
968 return true;
969}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700970
Robert Carrdb66e622017-04-10 16:55:57 -0700971void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800972 mCurrentState.zOrderRelatives.remove(relative);
973 mCurrentState.sequence++;
974 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700975 setTransactionFlags(eTransactionNeeded);
976}
977
978void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800979 mCurrentState.zOrderRelatives.add(relative);
980 mCurrentState.modified = true;
981 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700982 setTransactionFlags(eTransactionNeeded);
983}
984
chaviw606e5cf2019-03-01 10:12:10 -0800985void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
986 mCurrentState.zOrderRelativeOf = relativeOf;
987 mCurrentState.sequence++;
988 mCurrentState.modified = true;
989 setTransactionFlags(eTransactionNeeded);
990}
991
Robert Carr503d2bd2017-12-04 15:49:47 -0800992bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700993 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
994 if (handle == nullptr) {
995 return false;
996 }
997 sp<Layer> relative = handle->owner.promote();
998 if (relative == nullptr) {
999 return false;
1000 }
1001
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001002 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1003 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001004 return false;
1005 }
1006
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001007 mCurrentState.sequence++;
1008 mCurrentState.modified = true;
1009 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001010
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001011 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001012 if (oldZOrderRelativeOf != nullptr) {
1013 oldZOrderRelativeOf->removeZOrderRelative(this);
1014 }
chaviw606e5cf2019-03-01 10:12:10 -08001015 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001016 relative->addZOrderRelative(this);
1017
1018 setTransactionFlags(eTransactionNeeded);
1019
1020 return true;
1021}
1022
Mathias Agopian13127d82013-03-05 17:47:11 -08001023bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001024 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001025 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001026 mCurrentState.requested_legacy.w = w;
1027 mCurrentState.requested_legacy.h = h;
1028 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001029 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001030
1031 // record the new size, from this point on, when the client request
1032 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001033 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001034 return true;
1035}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001036bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001037 if (mCurrentState.color.a == alpha) return false;
1038 mCurrentState.sequence++;
1039 mCurrentState.color.a = alpha;
1040 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001041 setTransactionFlags(eTransactionNeeded);
1042 return true;
1043}
chaviw13fdc492017-06-27 12:40:18 -07001044
Valerie Haudd0b7572019-01-29 14:59:27 -08001045bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1046 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001047 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001048 }
1049 mCurrentState.sequence++;
1050 mCurrentState.modified = true;
1051 setTransactionFlags(eTransactionNeeded);
1052
1053 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001054 // create background color layer if one does not yet exist
1055 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1056 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001057 mCurrentState.bgColorLayer = new ColorLayer(
1058 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001059
Valerie Haudd0b7572019-01-29 14:59:27 -08001060 // add to child list
1061 addChild(mCurrentState.bgColorLayer);
1062 mFlinger->mLayersAdded = true;
1063 // set up SF to handle added color layer
1064 if (isRemovedFromCurrentState()) {
1065 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1066 }
1067 mFlinger->setTransactionFlags(eTransactionNeeded);
1068 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1069 mCurrentState.bgColorLayer->reparent(nullptr);
1070 mCurrentState.bgColorLayer = nullptr;
1071 return true;
1072 }
1073
1074 mCurrentState.bgColorLayer->setColor(color);
1075 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1076 mCurrentState.bgColorLayer->setAlpha(alpha);
1077 mCurrentState.bgColorLayer->setDataspace(dataspace);
1078
chaviw13fdc492017-06-27 12:40:18 -07001079 return true;
1080}
1081
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001082bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001084
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001085 mCurrentState.sequence++;
1086 mCurrentState.cornerRadius = cornerRadius;
1087 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001088 setTransactionFlags(eTransactionNeeded);
1089 return true;
1090}
1091
Robert Carrd4ae7f32018-06-07 16:10:57 -07001092bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1093 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001094 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001095 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1096
1097 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1098 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1099 return false;
1100 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001101 mCurrentState.sequence++;
1102 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1103 matrix.dsdy);
1104 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001105 setTransactionFlags(eTransactionNeeded);
1106 return true;
1107}
Marissa Wall61c58622018-07-18 10:12:20 -07001108
Mathias Agopian13127d82013-03-05 17:47:11 -08001109bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001110 mCurrentState.requestedTransparentRegion_legacy = transparent;
1111 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001112 setTransactionFlags(eTransactionNeeded);
1113 return true;
1114}
1115bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1117 if (mCurrentState.flags == newFlags) return false;
1118 mCurrentState.sequence++;
1119 mCurrentState.flags = newFlags;
1120 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001121 setTransactionFlags(eTransactionNeeded);
1122 return true;
1123}
Robert Carr99e27f02016-06-16 15:18:02 -07001124
Marissa Wallf58c14b2018-07-24 10:50:43 -07001125bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001126 if (mCurrentState.requestedCrop_legacy == crop) return false;
1127 mCurrentState.sequence++;
1128 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001129 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001130 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001131 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001132 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1133
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001134 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001135 setTransactionFlags(eTransactionNeeded);
1136 return true;
1137}
Robert Carr8d5227b2017-03-16 15:41:03 -07001138
Robert Carrc3574f72016-03-24 12:19:32 -07001139bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001140 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001141 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001142 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001143 return true;
1144}
1145
Evan Roskyef876c92019-01-25 17:46:06 -08001146bool Layer::setMetadata(const LayerMetadata& data) {
1147 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001148 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001149 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001150 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001151 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001152}
1153
Mathias Agopian13127d82013-03-05 17:47:11 -08001154bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001155 if (mCurrentState.layerStack == layerStack) return false;
1156 mCurrentState.sequence++;
1157 mCurrentState.layerStack = layerStack;
1158 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001159 setTransactionFlags(eTransactionNeeded);
1160 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001161}
1162
Peiyong Linc502cb72019-03-01 15:00:23 -08001163bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1164 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1165 return false;
1166 }
1167 mCurrentState.sequence++;
1168 mCurrentState.colorSpaceAgnostic = agnostic;
1169 mCurrentState.modified = true;
1170 setTransactionFlags(eTransactionNeeded);
1171 return true;
1172}
1173
Robert Carr1f0a16a2016-10-24 16:27:39 -07001174uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001175 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001176 if (p == nullptr) {
1177 return getDrawingState().layerStack;
1178 }
1179 return p->getLayerStack();
1180}
1181
Marissa Wallf58c14b2018-07-24 10:50:43 -07001182void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001183 mCurrentState.barrierLayer_legacy = barrierLayer;
1184 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001185 // We don't set eTransactionNeeded, because just receiving a deferral
1186 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001187 mCurrentState.modified = true;
1188 pushPendingState();
1189 mCurrentState.barrierLayer_legacy = nullptr;
1190 mCurrentState.frameNumber_legacy = 0;
1191 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001192}
1193
Marissa Wallf58c14b2018-07-24 10:50:43 -07001194void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001195 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001196 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001197}
1198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199// ----------------------------------------------------------------------------
1200// pageflip handling...
1201// ----------------------------------------------------------------------------
1202
Robert Carr1f0a16a2016-10-24 16:27:39 -07001203bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001204 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001205 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001206 if (parent != nullptr && parent->isHiddenByPolicy()) {
1207 return true;
1208 }
1209 return s.flags & layer_state_t::eLayerHidden;
1210}
1211
David Sodman41fdfc92017-11-06 16:09:56 -08001212uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001213 // TODO: should we do something special if mSecure is set?
1214 if (mProtectedByApp) {
1215 // need a hardware-protected path to external video sink
1216 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001217 }
Riley Andrews03414a12014-07-01 14:22:59 -07001218 if (mPotentialCursor) {
1219 usage |= GraphicBuffer::USAGE_CURSOR;
1220 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001221 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001222 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001223}
1224
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001225void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001226 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001227 // Disable setting transform hint if the debug flag is set or if the
1228 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1229 // in one orientation.
1230 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001231 // The transform hint is used to improve performance, but we can
1232 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001233 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001234 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001235 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001236 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001237 orientation = 0;
1238 }
1239 }
David Sodmaneb085e02017-10-05 18:49:04 -07001240 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001241}
1242
Mathias Agopian13127d82013-03-05 17:47:11 -08001243// ----------------------------------------------------------------------------
1244// debugging
1245// ----------------------------------------------------------------------------
1246
Marissa Wall61c58622018-07-18 10:12:20 -07001247// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001248LayerDebugInfo Layer::getLayerDebugInfo() const {
1249 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001250 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001251 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001252 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001253 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001254 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001255 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001256 info.mVisibleRegion = visibleRegion;
1257 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001258 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001259 info.mX = ds.active_legacy.transform.tx();
1260 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001261 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001262 info.mWidth = ds.active_legacy.w;
1263 info.mHeight = ds.active_legacy.h;
1264 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001265 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001266 info.mFlags = ds.flags;
1267 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001268 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001269 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1270 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1271 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1272 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001273 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001274 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001275 if (buffer != 0) {
1276 info.mActiveBufferWidth = buffer->getWidth();
1277 info.mActiveBufferHeight = buffer->getHeight();
1278 info.mActiveBufferStride = buffer->getStride();
1279 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001280 } else {
1281 info.mActiveBufferWidth = 0;
1282 info.mActiveBufferHeight = 0;
1283 info.mActiveBufferStride = 0;
1284 info.mActiveBufferFormat = 0;
1285 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001287 info.mNumQueuedFrames = getQueuedFrameCount();
1288 info.mRefreshPending = isBufferLatched();
1289 info.mIsOpaque = isOpaque(ds);
1290 info.mContentDirty = contentDirty;
1291 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001292}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001293
Yiwei Zhang5434a782018-12-05 18:06:32 -08001294void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001295 result.append("-------------------------------");
1296 result.append("-------------------------------");
1297 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001298 result.append(" Layer name\n");
1299 result.append(" Z | ");
1300 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001301 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001302 result.append(" Disp Frame (LTRB) | ");
1303 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001304 result.append("-------------------------------");
1305 result.append("-------------------------------");
1306 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001307}
1308
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001309void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1310 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1311 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001312 return;
1313 }
1314
Yiwei Zhang5434a782018-12-05 18:06:32 -08001315 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001316 if (mName.length() > 77) {
1317 std::string shortened;
1318 shortened.append(mName.string(), 36);
1319 shortened.append("[...]");
1320 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001321 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001322 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001323 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001324 }
1325
Yiwei Zhang5434a782018-12-05 18:06:32 -08001326 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001327
Alec Mourib416efd2018-09-06 21:01:59 +00001328 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001329 const auto& compositionState = outputLayer->getState();
1330
Chia-I Wu1e043612018-03-01 09:45:09 -08001331 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001332 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001333 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001334 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001335 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001336 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1337 StringAppendF(&result, "%10s | ",
1338 toString(getCompositionLayer() ? compositionState.bufferTransform
1339 : static_cast<Hwc2::Transform>(0))
1340 .c_str());
1341 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001342 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001343 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001344 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1345 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001346
Yichi Chen6ca35192018-05-29 12:20:43 +08001347 result.append("- - - - - - - - - - - - - - - -");
1348 result.append("- - - - - - - - - - - - - - - -");
1349 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001350}
Dan Stozae22aec72016-08-01 13:20:59 -07001351
Yiwei Zhang5434a782018-12-05 18:06:32 -08001352void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001353 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001354}
1355
Svetoslavd85084b2014-03-20 10:28:31 -07001356void Layer::clearFrameStats() {
1357 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001358}
1359
Jamie Gennis6547ff42013-07-16 20:12:42 -07001360void Layer::logFrameStats() {
1361 mFrameTracker.logAndResetStats(mName);
1362}
1363
Svetoslavd85084b2014-03-20 10:28:31 -07001364void Layer::getFrameStats(FrameStats* outStats) const {
1365 mFrameTracker.getStats(outStats);
1366}
1367
Yiwei Zhang5434a782018-12-05 18:06:32 -08001368void Layer::dumpFrameEvents(std::string& result) {
1369 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001370 Mutex::Autolock lock(mFrameEventHistoryMutex);
1371 mFrameEventHistory.checkFencesForCompletion();
1372 mFrameEventHistory.dump(result);
1373}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001374
Brian Anderson5ea5e592016-12-01 16:54:33 -08001375void Layer::onDisconnect() {
1376 Mutex::Autolock lock(mFrameEventHistoryMutex);
1377 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001378 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001379}
1380
Brian Anderson3890c392016-07-25 12:48:08 -07001381void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001382 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001383 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001384 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1385 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001386 }
1387
Brian Andersond6927fb2016-07-23 23:37:30 -07001388 Mutex::Autolock lock(mFrameEventHistoryMutex);
1389 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001390 // If there are any unsignaled fences in the aquire timeline at this
1391 // point, the previously queued frame hasn't been latched yet. Go ahead
1392 // and try to get the signal time here so the syscall is taken out of
1393 // the main thread's critical path.
1394 mAcquireTimeline.updateSignalTimes();
1395 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001396 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001397 mFrameEventHistory.addQueue(*newTimestamps);
1398 }
1399
Brian Anderson3890c392016-07-25 12:48:08 -07001400 if (outDelta) {
1401 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001402 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001403}
Dan Stozae77c7662016-05-13 11:37:28 -07001404
Chia-I Wu98f1c102017-05-30 14:54:08 -07001405size_t Layer::getChildrenCount() const {
1406 size_t count = 0;
1407 for (const sp<Layer>& child : mCurrentChildren) {
1408 count += 1 + child->getChildrenCount();
1409 }
1410 return count;
1411}
1412
Robert Carr1f0a16a2016-10-24 16:27:39 -07001413void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001414 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001415 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001416
Robert Carr1f0a16a2016-10-24 16:27:39 -07001417 mCurrentChildren.add(layer);
1418 layer->setParent(this);
1419}
1420
1421ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001422 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001423 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001424
Robert Carr1323c952019-01-28 18:13:27 -08001425 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001426 return mCurrentChildren.remove(layer);
1427}
1428
Robert Carr1db73f62016-12-21 12:58:51 -08001429bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1430 sp<Handle> handle = nullptr;
1431 sp<Layer> newParent = nullptr;
1432 if (newParentHandle == nullptr) {
1433 return false;
1434 }
1435 handle = static_cast<Handle*>(newParentHandle.get());
1436 newParent = handle->owner.promote();
1437 if (newParent == nullptr) {
1438 ALOGE("Unable to promote Layer handle");
1439 return false;
1440 }
1441
chaviw5aedec92018-10-22 10:40:38 -07001442 if (attachChildren()) {
1443 setTransactionFlags(eTransactionNeeded);
1444 }
Robert Carr1db73f62016-12-21 12:58:51 -08001445 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001446 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001447 }
1448 mCurrentChildren.clear();
1449
1450 return true;
1451}
1452
Robert Carr15eae092018-03-23 13:43:53 -07001453void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001454 for (const sp<Layer>& child : mDrawingChildren) {
1455 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001456 child->computeBounds(newParent->mBounds,
1457 newParent->getTransformWithScale(
1458 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001459 }
1460}
1461
chaviwf1961f72017-09-18 16:41:07 -07001462bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001463 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001464
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001465 // While layers are detached, we allow most operations
1466 // and simply halt performing the actual transaction. However
1467 // for reparent != null we would enter the mRemovedFromCurrentState
1468 // state, regardless of whether doTransaction was called, and
1469 // so we need to prevent the update here.
1470 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001471 return false;
1472 }
1473
Robert Carr54cf5b12019-01-25 14:02:28 -08001474 sp<Layer> newParent;
1475 if (newParentHandle != nullptr) {
1476 auto handle = static_cast<Handle*>(newParentHandle.get());
1477 newParent = handle->owner.promote();
1478 if (newParent == nullptr) {
1479 ALOGE("Unable to promote Layer handle");
1480 return false;
1481 }
1482 if (newParent == this) {
1483 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1484 return false;
1485 }
1486 }
1487
chaviwf1961f72017-09-18 16:41:07 -07001488 sp<Layer> parent = getParent();
1489 if (parent != nullptr) {
1490 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001491 }
1492
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001493 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001494 newParent->addChild(this);
1495 if (!newParent->isRemovedFromCurrentState()) {
1496 addToCurrentState();
1497 } else {
1498 onRemovedFromCurrentState();
1499 }
1500
1501 if (mLayerDetached) {
1502 mLayerDetached = false;
1503 callSetTransactionFlags = true;
1504 }
1505 } else {
1506 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001507 }
1508
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001509 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001510 setTransactionFlags(eTransactionNeeded);
1511 }
chaviw06178942017-07-27 10:25:59 -07001512 return true;
1513}
1514
Robert Carr9524cb32017-02-13 11:32:32 -08001515bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001516 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001517 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001518 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001519 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001520 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001521 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001522 }
Robert Carr7f619b22017-11-06 12:56:35 -08001523 }
Robert Carr9524cb32017-02-13 11:32:32 -08001524
1525 return true;
1526}
1527
chaviw5aedec92018-10-22 10:40:38 -07001528bool Layer::attachChildren() {
1529 bool changed = false;
1530 for (const sp<Layer>& child : mCurrentChildren) {
1531 sp<Client> parentClient = mClientRef.promote();
1532 sp<Client> client(child->mClientRef.promote());
1533 if (client != nullptr && parentClient != client) {
1534 if (child->mLayerDetached) {
1535 child->mLayerDetached = false;
1536 changed = true;
1537 }
1538 changed |= child->attachChildren();
1539 }
1540 }
1541
1542 return changed;
1543}
1544
Peiyong Lind3788632018-09-18 16:01:31 -07001545bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001546 static const mat4 identityMatrix = mat4();
1547
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001548 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001549 return false;
1550 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001551 ++mCurrentState.sequence;
1552 mCurrentState.colorTransform = matrix;
1553 mCurrentState.hasColorTransform = matrix != identityMatrix;
1554 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001555 setTransactionFlags(eTransactionNeeded);
1556 return true;
1557}
1558
chaviwf66724d2018-11-28 16:35:21 -08001559mat4 Layer::getColorTransform() const {
1560 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1561 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1562 colorTransform = parent->getColorTransform() * colorTransform;
1563 }
1564 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001565}
1566
1567bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001568 bool hasColorTransform = getDrawingState().hasColorTransform;
1569 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1570 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1571 }
1572 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001573}
1574
Chia-I Wu11481472018-05-04 10:43:19 -07001575bool Layer::isLegacyDataSpace() const {
1576 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001577 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001578 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001579}
1580
Robert Carr1f0a16a2016-10-24 16:27:39 -07001581void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001582 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001583}
1584
Robert Carr1f0a16a2016-10-24 16:27:39 -07001585int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001586 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001587}
1588
Robert Carr29abff82017-12-04 13:51:20 -08001589bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1590 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001591 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001592 return state.zOrderRelativeOf != nullptr;
1593}
1594
David Sodman41fdfc92017-11-06 16:09:56 -08001595__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001596 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001597 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1598 "makeTraversalList received invalid stateSet");
1599 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1600 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001601 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001602
Robert Carr29abff82017-12-04 13:51:20 -08001603 if (state.zOrderRelatives.size() == 0) {
1604 *outSkipRelativeZUsers = true;
1605 return children;
1606 }
1607
chaviwfd462612018-05-31 16:11:27 -07001608 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001609 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001610 sp<Layer> strongRelative = weakRelative.promote();
1611 if (strongRelative != nullptr) {
1612 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001613 }
1614 }
1615
Dan Stoza412903f2017-04-27 13:42:17 -07001616 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001617 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001618 if (childState.zOrderRelativeOf != nullptr) {
1619 continue;
1620 }
Robert Carrdb66e622017-04-10 16:55:57 -07001621 traverse.add(child);
1622 }
1623
1624 return traverse;
1625}
1626
Robert Carr1f0a16a2016-10-24 16:27:39 -07001627/**
Robert Carrdb66e622017-04-10 16:55:57 -07001628 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001629 */
Dan Stoza412903f2017-04-27 13:42:17 -07001630void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001631 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1632 // produce a new list for traversing, including our relatives, and not including our children
1633 // who are relatives of another surface. In the case that there are no relative Z,
1634 // makeTraversalList returns our children directly to avoid significant overhead.
1635 // However in this case we need to take the responsibility for filtering children which
1636 // are relatives of another surface here.
1637 bool skipRelativeZUsers = false;
1638 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001639
Robert Carr1f0a16a2016-10-24 16:27:39 -07001640 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001641 for (; i < list.size(); i++) {
1642 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001643 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1644 continue;
1645 }
1646
Robert Carrdb66e622017-04-10 16:55:57 -07001647 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001648 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001649 }
Dan Stoza412903f2017-04-27 13:42:17 -07001650 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001651 }
Robert Carr29abff82017-12-04 13:51:20 -08001652
Dan Stoza412903f2017-04-27 13:42:17 -07001653 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001654 for (; i < list.size(); i++) {
1655 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001656
Robert Carr29abff82017-12-04 13:51:20 -08001657 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1658 continue;
1659 }
Dan Stoza412903f2017-04-27 13:42:17 -07001660 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001661 }
1662}
1663
1664/**
Robert Carrdb66e622017-04-10 16:55:57 -07001665 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001666 */
Dan Stoza412903f2017-04-27 13:42:17 -07001667void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1668 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001669 // See traverseInZOrder for documentation.
1670 bool skipRelativeZUsers = false;
1671 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001672
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001674 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001675 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001676
1677 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1678 continue;
1679 }
1680
Robert Carrdb66e622017-04-10 16:55:57 -07001681 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001682 break;
1683 }
Dan Stoza412903f2017-04-27 13:42:17 -07001684 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001685 }
Dan Stoza412903f2017-04-27 13:42:17 -07001686 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001687 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001688 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001689
1690 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1691 continue;
1692 }
1693
Dan Stoza412903f2017-04-27 13:42:17 -07001694 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695 }
1696}
1697
chaviw4b129c22018-04-09 16:19:43 -07001698LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1699 const std::vector<Layer*>& layersInTree) {
1700 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1701 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001702 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1703 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001704 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001705
chaviwfd462612018-05-31 16:11:27 -07001706 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001707 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1708 sp<Layer> strongRelative = weakRelative.promote();
1709 // Only add relative layers that are also descendents of the top most parent of the tree.
1710 // If a relative layer is not a descendent, then it should be ignored.
1711 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1712 traverse.add(strongRelative);
1713 }
1714 }
1715
1716 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001717 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001718 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1719 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1720 // the child here since it won't be added later as a relative.
1721 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1722 childState.zOrderRelativeOf.promote().get())) {
1723 continue;
1724 }
1725 traverse.add(child);
1726 }
1727
1728 return traverse;
1729}
1730
1731void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1732 LayerVector::StateSet stateSet,
1733 const LayerVector::Visitor& visitor) {
1734 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001735
1736 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001737 for (; i < list.size(); i++) {
1738 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001739 if (relative->getZ() >= 0) {
1740 break;
1741 }
chaviw4b129c22018-04-09 16:19:43 -07001742 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001743 }
chaviw4b129c22018-04-09 16:19:43 -07001744
chaviwa76b2712017-09-20 12:02:26 -07001745 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001746 for (; i < list.size(); i++) {
1747 const auto& relative = list[i];
1748 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001749 }
1750}
1751
chaviw4b129c22018-04-09 16:19:43 -07001752std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1753 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1754 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1755
1756 std::vector<Layer*> layersInTree = {this};
1757 for (size_t i = 0; i < children.size(); i++) {
1758 const auto& child = children[i];
1759 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1760 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1761 }
1762
1763 return layersInTree;
1764}
1765
1766void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1767 const LayerVector::Visitor& visitor) {
1768 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1769 std::sort(layersInTree.begin(), layersInTree.end());
1770 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1771}
1772
Peiyong Linefefaac2018-08-17 12:27:51 -07001773ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001774 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001775}
1776
chaviw13fdc492017-06-27 12:40:18 -07001777half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001778 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001779
chaviw13fdc492017-06-27 12:40:18 -07001780 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1781 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001782}
Robert Carr6452f122017-03-21 10:41:29 -07001783
chaviw13fdc492017-06-27 12:40:18 -07001784half4 Layer::getColor() const {
1785 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001786 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001787}
Robert Carr6452f122017-03-21 10:41:29 -07001788
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001789Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1790 const auto& p = mDrawingParent.promote();
1791 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001792 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001793 if (parentState.radius > 0) {
1794 ui::Transform t = getActiveTransform(getDrawingState());
1795 t = t.inverse();
1796 parentState.cropRect = t.transform(parentState.cropRect);
1797 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1798 // but a transform matrix can define horizontal and vertical scales.
1799 // Let's take the average between both of them and pass into the shader, practically we
1800 // never do this type of transformation on windows anyway.
1801 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1802 return parentState;
1803 }
1804 }
1805 const float radius = getDrawingState().cornerRadius;
Peiyong Lin27016a92019-03-29 17:36:08 +00001806 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001807}
1808
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809void Layer::commitChildList() {
1810 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1811 const auto& child = mCurrentChildren[i];
1812 child->commitChildList();
1813 }
1814 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001815 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816}
1817
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001818static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1819 if (weakBinderHandle == nullptr) {
1820 return nullptr;
1821 }
1822 sp<IBinder> binderHandle = weakBinderHandle.promote();
1823 if (binderHandle == nullptr) {
1824 return nullptr;
1825 }
1826 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1827 if (handle == nullptr) {
1828 return nullptr;
1829 }
1830 return handle->owner;
1831}
1832
Robert Carr720e5062018-07-30 17:45:14 -07001833void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001834 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001835 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001836 mCurrentState.modified = true;
1837 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001838 setTransactionFlags(eTransactionNeeded);
1839}
1840
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001841void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1842 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001843 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1844 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001845 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001846
Peiyong Linefefaac2018-08-17 12:27:51 -07001847 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001848 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001849
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001850 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1851 layerInfo->set_id(sequence);
1852 layerInfo->set_name(getName().c_str());
1853 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001854
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001855 for (const auto& child : children) {
1856 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001857 }
chaviw1d044282017-09-27 12:19:28 -07001858
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001859 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1860 sp<Layer> strongRelative = weakRelative.promote();
1861 if (strongRelative != nullptr) {
1862 layerInfo->add_relatives(strongRelative->sequence);
1863 }
chaviwadc40c22018-07-10 16:57:27 -07001864 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001865
1866 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1867 [&]() { return layerInfo->mutable_transparent_region(); });
1868 LayerProtoHelper::writeToProto(visibleRegion,
1869 [&]() { return layerInfo->mutable_visible_region(); });
1870 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1871 [&]() { return layerInfo->mutable_damage_region(); });
1872
1873 layerInfo->set_layer_stack(getLayerStack());
1874 layerInfo->set_z(state.z);
1875
1876 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1877 [&]() { return layerInfo->mutable_position(); });
1878
1879 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1880 [&]() {
1881 return layerInfo->mutable_requested_position();
1882 });
1883
1884 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1885 [&]() { return layerInfo->mutable_size(); });
1886
1887 LayerProtoHelper::writeToProto(state.crop_legacy,
1888 [&]() { return layerInfo->mutable_crop(); });
1889 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1890
1891 layerInfo->set_is_opaque(isOpaque(state));
1892 layerInfo->set_invalidate(contentDirty);
1893
1894 // XXX (b/79210409) mCurrentDataSpace is not protected
1895 layerInfo->set_dataspace(
1896 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1897
1898 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1899 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1900 LayerProtoHelper::writeToProto(state.color,
1901 [&]() { return layerInfo->mutable_requested_color(); });
1902 layerInfo->set_flags(state.flags);
1903
1904 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1905 LayerProtoHelper::writeToProto(requestedTransform,
1906 layerInfo->mutable_requested_transform());
1907
1908 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1909 if (parent != nullptr) {
1910 layerInfo->set_parent(parent->sequence);
1911 } else {
1912 layerInfo->set_parent(-1);
1913 }
1914
1915 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1916 if (zOrderRelativeOf != nullptr) {
1917 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1918 } else {
1919 layerInfo->set_z_order_relative_of(-1);
1920 }
1921
1922 auto buffer = mActiveBuffer;
1923 if (buffer != nullptr) {
1924 LayerProtoHelper::writeToProto(buffer,
1925 [&]() { return layerInfo->mutable_active_buffer(); });
1926 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1927 layerInfo->mutable_buffer_transform());
1928 }
1929
1930 layerInfo->set_queued_frames(getQueuedFrameCount());
1931 layerInfo->set_refresh_pending(isBufferLatched());
1932 layerInfo->set_curr_frame(mCurrentFrameNumber);
1933 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1934
1935 for (const auto& pendingState : mPendingStates) {
1936 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1937 if (barrierLayer != nullptr) {
1938 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1939 barrierLayerProto->set_id(barrierLayer->sequence);
1940 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1941 }
1942 }
1943 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001944 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001945
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001946 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1947 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1948 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001949 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001950
1951 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1952 auto protoMap = layerInfo->mutable_metadata();
1953 for (const auto& entry : state.metadata.mMap) {
1954 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1955 }
1956 LayerProtoHelper::writeToProto(mEffectiveTransform,
1957 layerInfo->mutable_effective_transform());
1958 LayerProtoHelper::writeToProto(mSourceBounds,
1959 [&]() { return layerInfo->mutable_source_bounds(); });
1960 LayerProtoHelper::writeToProto(mScreenBounds,
1961 [&]() { return layerInfo->mutable_screen_bounds(); });
1962 }
chaviw1d044282017-09-27 12:19:28 -07001963}
1964
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001965void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1966 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001967 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1968 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001969 return;
1970 }
1971
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001972 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001973
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001974 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001975
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001976 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001977 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001978
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001979 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001980 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001981
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001982 const int32_t transform =
1983 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001984 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001985
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001986 const int32_t compositionType =
1987 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1988 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001989 layerInfo->set_hwc_composition_type(compositionType);
1990
1991 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1992 static_cast<BufferLayer*>(this)->isProtected()) {
1993 layerInfo->set_is_protected(true);
1994 } else {
1995 layerInfo->set_is_protected(false);
1996 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001997}
1998
Robert Carr2e102c92018-10-23 12:11:15 -07001999bool Layer::isRemovedFromCurrentState() const {
2000 return mRemovedFromCurrentState;
2001}
2002
Arthur Hungd20b2702019-01-14 18:16:16 +08002003InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002004 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002005
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002006 if (info.displayId == ADISPLAY_ID_NONE) {
2007 info.displayId = mDrawingState.layerStack;
2008 }
2009
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002010 ui::Transform t = getTransform();
2011 const float xScale = t.sx();
2012 const float yScale = t.sy();
2013 if (xScale != 1.0f || yScale != 1.0f) {
2014 info.windowXScale *= 1.0f / xScale;
2015 info.windowYScale *= 1.0f / yScale;
2016 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002017 }
Robert Carre07e1032018-11-26 12:55:53 -08002018
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002019 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002020 // If this is a portal window, set the touchableRegion to the layerBounds.
2021 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2022 ? getBufferSize(getDrawingState())
2023 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002024 if (!layerBounds.isValid()) {
2025 layerBounds = getCroppedBufferSize(getDrawingState());
2026 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002027 layerBounds = t.transform(layerBounds);
2028 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2029
Arthur Hungd20b2702019-01-14 18:16:16 +08002030 // Input coordinate should match the layer bounds.
2031 info.frameLeft = layerBounds.left;
2032 info.frameTop = layerBounds.top;
2033 info.frameRight = layerBounds.right;
2034 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002035
2036 // Position the touchable region relative to frame screen location and restrict it to frame
2037 // bounds.
2038 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002039 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002040
2041 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2042 if (info.replaceTouchableRegionWithCrop) {
2043 if (cropLayer == nullptr) {
2044 info.touchableRegion = Region(Rect{mScreenBounds});
2045 } else {
2046 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2047 }
2048 } else if (cropLayer != nullptr) {
2049 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2050 }
2051
Robert Carr720e5062018-07-30 17:45:14 -07002052 return info;
2053}
2054
2055bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002056 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002057}
2058
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002059std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2060 return nullptr;
2061}
2062
chaviw3e727cd2019-01-31 13:41:05 -08002063bool Layer::canReceiveInput() const {
2064 return isVisible();
2065}
2066
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002067compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2068 const sp<const DisplayDevice>& display) const {
2069 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2070}
2071
Mathias Agopian13127d82013-03-05 17:47:11 -08002072// ---------------------------------------------------------------------------
2073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002075
2076#if defined(__gl_h_)
2077#error "don't include gl/gl.h in this file"
2078#endif
2079
2080#if defined(__gl2_h_)
2081#error "don't include gl2/gl2.h in this file"
2082#endif