blob: 898d37e787c7bf4081521402ae1949aba7e56330 [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
Robert Carr2e102c92018-10-23 12:11:15 -0700129 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700130}
131
Mathias Agopian13127d82013-03-05 17:47:11 -0800132// ---------------------------------------------------------------------------
133// callbacks
134// ---------------------------------------------------------------------------
135
David Sodmaneb085e02017-10-05 18:49:04 -0700136/*
137 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
138 * Layer. So, the implementation is done in BufferLayer. When called on a
139 * ColorLayer object, it's essentially a NOP.
140 */
David Sodmaneb085e02017-10-05 18:49:04 -0700141void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800142
Chia-I Wuc6657022017-08-15 11:18:17 -0700143void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700144 mRemovedFromCurrentState = true;
145
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800146 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
147 if (mCurrentState.zOrderRelativeOf != nullptr) {
148 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
149 if (strongRelative != nullptr) {
150 strongRelative->removeZOrderRelative(this);
151 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700152 }
chaviw606e5cf2019-03-01 10:12:10 -0800153 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700154 }
Rob Carr4bba3702018-10-08 21:53:30 +0000155
Robert Carr2e102c92018-10-23 12:11:15 -0700156 // Since we are no longer reachable from CurrentState SurfaceFlinger
157 // will no longer invoke doTransaction for us, and so we will
158 // never finish applying transactions. We signal the sync point
159 // now so that another layer will not become indefinitely
160 // blocked.
161 for (auto& point: mRemoteSyncPoints) {
162 point->setTransactionApplied();
163 }
164 mRemoteSyncPoints.clear();
165
166 {
167 Mutex::Autolock syncLock(mLocalSyncPointMutex);
168 for (auto& point : mLocalSyncPoints) {
169 point->setFrameAvailable();
170 }
171 mLocalSyncPoints.clear();
172 }
173
Chia-I Wuc6657022017-08-15 11:18:17 -0700174 for (const auto& child : mCurrentChildren) {
175 child->onRemovedFromCurrentState();
176 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800177
178 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700179}
Chia-I Wu38512252017-05-17 14:36:16 -0700180
chaviw61626f22018-11-15 16:26:27 -0800181void Layer::addToCurrentState() {
182 mRemovedFromCurrentState = false;
183
184 for (const auto& child : mCurrentChildren) {
185 child->addToCurrentState();
186 }
187}
188
Mathias Agopian13127d82013-03-05 17:47:11 -0800189// ---------------------------------------------------------------------------
190// set-up
191// ---------------------------------------------------------------------------
192
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700193const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800194 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
chaviw13fdc492017-06-27 12:40:18 -0700197bool Layer::getPremultipledAlpha() const {
198 return mPremultipliedAlpha;
199}
200
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700201sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700203 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800204}
205
206// ---------------------------------------------------------------------------
207// h/w composer set-up
208// ---------------------------------------------------------------------------
209
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800210bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
211 auto outputLayer = findOutputLayerForDisplay(displayDevice);
212 LOG_FATAL_IF(!outputLayer);
213 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
214}
215
216HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
217 auto outputLayer = findOutputLayerForDisplay(displayDevice);
218 if (!outputLayer || !outputLayer->getState().hwc) {
219 return nullptr;
220 }
221 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700222}
Steven Thomasb02664d2017-07-26 18:48:28 -0700223
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800224Rect Layer::getContentCrop() const {
225 // this is the crop rectangle that applies to the buffer
226 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700227 Rect crop;
228 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800229 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700230 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800231 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800232 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800233 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700234 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800235 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700236 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700237 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700238 return crop;
239}
240
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700241static Rect reduce(const Rect& win, const Region& exclude) {
242 if (CC_LIKELY(exclude.isEmpty())) {
243 return win;
244 }
245 if (exclude.isRect()) {
246 return win.reduce(exclude.getBounds());
247 }
248 return Region(win).subtract(exclude).getBounds();
249}
250
Dan Stoza80d61162017-12-20 15:57:52 -0800251static FloatRect reduce(const FloatRect& win, const Region& exclude) {
252 if (CC_LIKELY(exclude.isEmpty())) {
253 return win;
254 }
255 // Convert through Rect (by rounding) for lack of FloatRegion
256 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
257}
258
Vishnu Nair4351ad52019-02-11 14:13:02 -0800259Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800260 if (!reduceTransparentRegion) {
261 return Rect{mScreenBounds};
262 }
263
264 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800265 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800266 // Transform to screen space.
267 bounds = t.transform(bounds);
268 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700269}
270
Vishnu Nair4351ad52019-02-11 14:13:02 -0800271FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000272 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800273 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700274}
275
Vishnu Nairf0c28512019-02-08 12:40:28 -0800276FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
277 // Subtract the transparent region and snap to the bounds.
278 return reduce(mBounds, activeTransparentRegion);
279}
280
Vishnu Nairc652ff82019-03-15 12:48:54 -0700281ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800282 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
283 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700284 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800285 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700286 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800287 }
288
Marissa Wall290ad082019-03-06 13:23:47 -0800289 // If the layer is a buffer state layer, the active width and height
290 // could be infinite. In that case, return the effective transform.
291 const uint32_t activeWidth = getActiveWidth(getDrawingState());
292 const uint32_t activeHeight = getActiveHeight(getDrawingState());
293 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700294 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800295 }
296
Vishnu Nairc652ff82019-03-15 12:48:54 -0700297 int bufferWidth = mActiveBuffer->getWidth();
298 int bufferHeight = mActiveBuffer->getHeight();
299
300 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
301 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800302 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700303
Marissa Wall290ad082019-03-06 13:23:47 -0800304 float sx = activeWidth / static_cast<float>(bufferWidth);
305 float sy = activeHeight / static_cast<float>(bufferHeight);
306
Vishnu Nair4351ad52019-02-11 14:13:02 -0800307 ui::Transform extraParentScaling;
308 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700309 return extraParentScaling;
310}
311
312ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
313 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
314 // treat child surfaces as pixels in the parent surface.
315 if (!isFixedSize() || !mActiveBuffer) {
316 return mEffectiveTransform;
317 }
318 return mEffectiveTransform * bufferScaleTransform;
319}
320
321FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
322 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
323 // cropped to its parent layer after any buffer transform scaling is applied.
324 if (!isFixedSize() || !mActiveBuffer) {
325 return mBounds;
326 }
327 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800328}
329
330void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
331 const State& s(getDrawingState());
332
333 // Calculate effective layer transform
334 mEffectiveTransform = parentTransform * getActiveTransform(s);
335
336 // Transform parent bounds to layer space
337 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
338
Vishnu Nairc652ff82019-03-15 12:48:54 -0700339 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800340 mSourceBounds = computeSourceBounds(parentBounds);
341
342 // Calculate bounds by croping diplay frame with layer crop and parent bounds
343 FloatRect bounds = mSourceBounds;
344 const Rect layerCrop = getCrop(s);
345 if (!layerCrop.isEmpty()) {
346 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
347 }
348 bounds = bounds.intersect(parentBounds);
349
350 mBounds = bounds;
351 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700352
353 // Add any buffer scaling to the layer's children.
354 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800355 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700356 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
357 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800358 }
359}
360
Vishnu Nair60356342018-11-13 13:00:45 -0800361Rect Layer::getCroppedBufferSize(const State& s) const {
362 Rect size = getBufferSize(s);
363 Rect crop = getCrop(s);
364 if (!crop.isEmpty() && size.isValid()) {
365 size.intersect(crop, &size);
366 } else if (!crop.isEmpty()) {
367 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700368 }
Vishnu Nair60356342018-11-13 13:00:45 -0800369 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800370}
371
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700372void Layer::setupRoundedCornersCropCoordinates(Rect win,
373 const FloatRect& roundedCornersCrop) const {
374 // Translate win by the rounded corners rect coordinates, to have all values in
375 // layer coordinate space.
376 win.left -= roundedCornersCrop.left;
377 win.right -= roundedCornersCrop.left;
378 win.top -= roundedCornersCrop.top;
379 win.bottom -= roundedCornersCrop.top;
380
Lloyd Pique0b785d82018-12-04 17:25:27 -0800381 renderengine::Mesh::VertexArray<vec2> cropCoords(
382 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700383 cropCoords[0] = vec2(win.left, win.top);
384 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
385 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
386 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700387}
388
Lloyd Piquea83776c2019-01-29 18:42:32 -0800389void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
390 const auto& drawingState{getDrawingState()};
391 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500392 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800393 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800394 blendMode =
395 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800396 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800397
Lloyd Piquea83776c2019-01-29 18:42:32 -0800398 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
399 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700400 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400401 if (parent.get()) {
402 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800403 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
404 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
405 if (parentType >= 0 || parentAppId >= 0) {
406 type = parentType;
407 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700408 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400409 }
410
Lloyd Piquea83776c2019-01-29 18:42:32 -0800411 compositionState.geomLayerTransform = getTransform();
412 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
413 compositionState.geomBufferSize = getBufferSize(drawingState);
414 compositionState.geomContentCrop = getContentCrop();
415 compositionState.geomCrop = getCrop(drawingState);
416 compositionState.geomBufferTransform = mCurrentTransform;
417 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
418 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
419 compositionState.geomLayerBounds = mBounds;
420 compositionState.geomUsesSourceCrop = usesSourceCrop();
421 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700422
Lloyd Piquea83776c2019-01-29 18:42:32 -0800423 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
424 compositionState.alpha = alpha;
425 compositionState.type = type;
426 compositionState.appId = appId;
427}
David Sodmanba340492018-08-05 21:51:33 -0700428
Lloyd Piquea83776c2019-01-29 18:42:32 -0800429void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
430 bool includeGeometry) const {
431 if (includeGeometry) {
432 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700433 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800434}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700435
Lloyd Piquea83776c2019-01-29 18:42:32 -0800436const char* Layer::getDebugName() const {
437 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800438}
439
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800440void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
441 const auto outputLayer = findOutputLayerForDisplay(display);
442 LOG_FATAL_IF(!outputLayer);
443 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800444}
Dan Stozaee44edd2015-03-23 15:50:23 -0700445
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800446bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
447 const auto outputLayer = findOutputLayerForDisplay(display);
448 LOG_FATAL_IF(!outputLayer);
449 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800450}
451
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700452void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800453 const auto outputLayer = findOutputLayerForDisplay(display);
454 LOG_FATAL_IF(!outputLayer);
455
456 if (!outputLayer->getState().hwc ||
457 (*outputLayer->getState().hwc).hwcCompositionType !=
458 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800459 return;
460 }
461
462 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800463 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800464
465 // Apply the layer's transform, followed by the display's global transform
466 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800467 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800468 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700469 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800470 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700471 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700472 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 auto position = displayTransform.transform(frame);
474
Dominik Laskowski7e045462018-05-30 13:02:02 -0700475 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800476 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800477 ALOGE_IF(error != HWC2::Error::None,
478 "[%s] Failed to set cursor position "
479 "to (%d, %d): %s (%d)",
480 mName.string(), position.left, position.top, to_string(error).c_str(),
481 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482}
Riley Andrews03414a12014-07-01 14:22:59 -0700483
Mathias Agopian13127d82013-03-05 17:47:11 -0800484// ---------------------------------------------------------------------------
485// drawing...
486// ---------------------------------------------------------------------------
487
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000488bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800489 Region& clearRegion, const bool supportProtectedContent,
490 renderengine::LayerSettings& layer) {
491 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800492}
493
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000494bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800495 Region& clearRegion, const bool supportProtectedContent,
496 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000497 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800498 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000499}
500
501bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
502 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800503 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000504 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800505 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000506 half alpha = getAlpha();
507 layer.geometry.boundaries = bounds;
508 if (useIdentityTransform) {
509 layer.geometry.positionTransform = mat4();
510 } else {
511 const ui::Transform transform = getTransform();
512 mat4 m;
513 m[0][0] = transform[0][0];
514 m[0][1] = transform[0][1];
515 m[0][3] = transform[0][2];
516 m[1][0] = transform[1][0];
517 m[1][1] = transform[1][1];
518 m[1][3] = transform[1][2];
519 m[3][0] = transform[2][0];
520 m[3][1] = transform[2][1];
521 m[3][3] = transform[2][2];
522 layer.geometry.positionTransform = m;
523 }
524
525 if (hasColorTransform()) {
526 layer.colorTransform = getColorTransform();
527 }
528
529 const auto roundedCornerState = getRoundedCornerState();
530 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
531 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
532
533 layer.alpha = alpha;
534 layer.sourceDataspace = mCurrentDataSpace;
535 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800536}
537
David Sodman41fdfc92017-11-06 16:09:56 -0800538void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
539 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800540 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800541 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700542 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800543 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800544}
545
David Sodmanc1498e62018-09-12 14:36:26 -0700546void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
547 clearWithOpenGL(renderArea, 0, 0, 0, 0);
548}
549
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800550void Layer::setCompositionType(const sp<const DisplayDevice>& display,
551 Hwc2::IComposerClient::Composition type) {
552 const auto outputLayer = findOutputLayerForDisplay(display);
553 LOG_FATAL_IF(!outputLayer);
554 LOG_FATAL_IF(!outputLayer->getState().hwc);
555 auto& compositionState = outputLayer->editState();
556
557 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
558 toString(type).c_str(), 1);
559 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700560 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800561 (*compositionState.hwc).hwcCompositionType = type;
562
563 auto error = (*compositionState.hwc)
564 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
565 ALOGE_IF(error != HWC2::Error::None,
566 "[%s] Failed to set "
567 "composition type %s: %s (%d)",
568 mName.string(), toString(type).c_str(), to_string(error).c_str(),
569 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570 }
571}
572
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800573Hwc2::IComposerClient::Composition Layer::getCompositionType(
574 const sp<const DisplayDevice>& display) const {
575 const auto outputLayer = findOutputLayerForDisplay(display);
576 LOG_FATAL_IF(!outputLayer);
577 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
578 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579}
580
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800581bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
582 const auto outputLayer = findOutputLayerForDisplay(display);
583 LOG_FATAL_IF(!outputLayer);
584 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586
Dan Stozacac35382016-01-27 12:21:06 -0800587bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
588 if (point->getFrameNumber() <= mCurrentFrameNumber) {
589 // Don't bother with a SyncPoint, since we've already latched the
590 // relevant frame
591 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700592 }
Robert Carr2e102c92018-10-23 12:11:15 -0700593 if (isRemovedFromCurrentState()) {
594 return false;
595 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700596
Dan Stozacac35382016-01-27 12:21:06 -0800597 Mutex::Autolock lock(mLocalSyncPointMutex);
598 mLocalSyncPoints.push_back(point);
599 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700600}
601
Mathias Agopian13127d82013-03-05 17:47:11 -0800602// ----------------------------------------------------------------------------
603// local state
604// ----------------------------------------------------------------------------
605
Peiyong Lin833074a2018-08-28 11:53:54 -0700606void Layer::computeGeometry(const RenderArea& renderArea,
607 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700608 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700609 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800610 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700611
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000612 vec2 lt = vec2(win.left, win.top);
613 vec2 lb = vec2(win.left, win.bottom);
614 vec2 rb = vec2(win.right, win.bottom);
615 vec2 rt = vec2(win.right, win.top);
616
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800617 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000618 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700619 lt = layerTransform.transform(lt);
620 lb = layerTransform.transform(lb);
621 rb = layerTransform.transform(rb);
622 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000623 }
624
Peiyong Lin833074a2018-08-28 11:53:54 -0700625 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700626 position[0] = renderAreaTransform.transform(lt);
627 position[1] = renderAreaTransform.transform(lb);
628 position[2] = renderAreaTransform.transform(rb);
629 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800630}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800631
David Sodman41fdfc92017-11-06 16:09:56 -0800632bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800633 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700634 return (s.flags & layer_state_t::eLayerSecure);
635}
636
Mathias Agopian13127d82013-03-05 17:47:11 -0800637void Layer::setVisibleRegion(const Region& visibleRegion) {
638 // always called from main thread
639 this->visibleRegion = visibleRegion;
640}
641
642void Layer::setCoveredRegion(const Region& coveredRegion) {
643 // always called from main thread
644 this->coveredRegion = coveredRegion;
645}
646
David Sodman41fdfc92017-11-06 16:09:56 -0800647void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800648 // always called from main thread
649 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
650}
651
Robert Carre5f4f692018-01-12 13:12:28 -0800652void Layer::clearVisibilityRegions() {
653 visibleRegion.clear();
654 visibleNonTransparentRegion.clear();
655 coveredRegion.clear();
656}
657
Mathias Agopian13127d82013-03-05 17:47:11 -0800658// ----------------------------------------------------------------------------
659// transaction
660// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800661
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800662void Layer::pushPendingState() {
663 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700664 return;
665 }
666
Dan Stoza7dde5992015-05-22 09:51:44 -0700667 // If this transaction is waiting on the receipt of a frame, generate a sync
668 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700669 // We don't allow installing sync points after we are removed from the current state
670 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800671 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
672 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800673 if (barrierLayer == nullptr) {
674 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700675 // If we can't promote the layer we are intended to wait on,
676 // then it is expired or otherwise invalid. Allow this transaction
677 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800678 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800679 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800680 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800681 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800682 mRemoteSyncPoints.push_back(std::move(syncPoint));
683 } else {
684 // We already missed the frame we're supposed to synchronize
685 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800686 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800687 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700688 }
689
Dan Stoza7dde5992015-05-22 09:51:44 -0700690 // Wake us up to check if the frame has been received
691 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700692 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700693 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800694 mPendingStates.push_back(mCurrentState);
695 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700696}
697
Pablo Ceballos05289c22016-04-14 15:49:55 -0700698void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800699 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700700
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800701 mPendingStates.removeAt(0);
702 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700703}
704
Pablo Ceballos05289c22016-04-14 15:49:55 -0700705bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700706 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800707 while (!mPendingStates.empty()) {
708 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700709 if (mRemoteSyncPoints.empty()) {
710 // If we don't have a sync point for this, apply it anyway. It
711 // will be visually wrong, but it should keep us from getting
712 // into too much trouble.
713 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700714 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700715 stateUpdateAvailable = true;
716 continue;
717 }
718
Marissa Wallf58c14b2018-07-24 10:50:43 -0700719 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800720 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800721 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800722
723 // Signal our end of the sync point and then dispose of it
724 mRemoteSyncPoints.front()->setTransactionApplied();
725 mRemoteSyncPoints.pop_front();
726 continue;
727 }
728
Dan Stoza7dde5992015-05-22 09:51:44 -0700729 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
730 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700731 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700732 stateUpdateAvailable = true;
733
734 // Signal our end of the sync point and then dispose of it
735 mRemoteSyncPoints.front()->setTransactionApplied();
736 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800737 } else {
738 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700739 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700740 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700741 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700742 stateUpdateAvailable = true;
743 }
744 }
745
746 // If we still have pending updates, wake SurfaceFlinger back up and point
747 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800748 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700749 setTransactionFlags(eTransactionNeeded);
750 mFlinger->setTransactionFlags(eTraversalNeeded);
751 }
752
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800753 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700754 return stateUpdateAvailable;
755}
756
Marissa Wall61c58622018-07-18 10:12:20 -0700757uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000758 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759
Marissa Wall61c58622018-07-18 10:12:20 -0700760 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
761 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700762
David Sodmaneb085e02017-10-05 18:49:04 -0700763 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700764 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000765 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800766 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
767 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
768 " requested={ wh={%4u,%4u} }}\n"
769 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
770 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700771 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700772 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
773 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
774 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
775 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
776 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700777 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
778 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
779 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700781
Robert Carre392b552017-09-19 12:16:05 -0700782 // Don't let Layer::doTransaction update the drawing state
783 // if we have a pending resize, unless we are in fixed-size mode.
784 // the drawing state will be updated only once we receive a buffer
785 // with the correct size.
786 //
787 // In particular, we want to make sure the clip (which is part
788 // of the geometry state) is latched together with the size but is
789 // latched immediately when no resizing is involved.
790 //
791 // If a sideband stream is attached, however, we want to skip this
792 // optimization so that transactions aren't missed when a buffer
793 // never arrives
794 //
795 // In the case that we don't have a buffer we ignore other factors
796 // and avoid entering the resizePending state. At a high level the
797 // resizePending state is to avoid applying the state of the new buffer
798 // to the old buffer. However in the state where we don't have an old buffer
799 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700800 const bool resizePending =
801 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
802 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800803 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700804 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800805 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700806 flags |= eDontUpdateGeometryState;
807 }
808 }
809
Robert Carr7bf247e2017-05-18 14:02:49 -0700810 // Here we apply various requested geometry states, depending on our
811 // latching configuration. See Layer.h for a detailed discussion of
812 // how geometry latching is controlled.
813 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000814 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700815
816 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
817 // mode, which causes attributes which normally latch regardless of scaling mode,
818 // to be delayed. We copy the requested state to the active state making sure
819 // to respect these rules (again see Layer.h for a detailed discussion).
820 //
821 // There is an awkward asymmetry in the handling of the crop states in the position
822 // states, as can be seen below. Largely this arises from position and transform
823 // being stored in the same data structure while having different latching rules.
824 // b/38182305
825 //
Marissa Wall61c58622018-07-18 10:12:20 -0700826 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700827 // applyPendingStates in the presence of deferred transactions.
828 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700829 float tx = stateToCommit->active_legacy.transform.tx();
830 float ty = stateToCommit->active_legacy.transform.ty();
831 stateToCommit->active_legacy = stateToCommit->requested_legacy;
832 stateToCommit->active_legacy.transform.set(tx, ty);
833 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700834 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700835 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700836 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700837 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800838 }
839
Marissa Wall61c58622018-07-18 10:12:20 -0700840 return flags;
841}
842
843uint32_t Layer::doTransaction(uint32_t flags) {
844 ATRACE_CALL();
845
chaviw5aedec92018-10-22 10:40:38 -0700846 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800847 return flags;
848 }
849
850 if (mChildrenChanged) {
851 flags |= eVisibleRegion;
852 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700853 }
854
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800855 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000856 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700857 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800858 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700859 }
860
861 flags = doTransactionResize(flags, &c);
862
Alec Mourib416efd2018-09-06 21:01:59 +0000863 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700864
865 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800866 // invalidate and recompute the visible regions if needed
867 flags |= Layer::eVisibleRegion;
868 }
869
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700870 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800871 // invalidate and recompute the visible regions if needed
872 flags |= eVisibleRegion;
873 this->contentDirty = true;
874
875 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700876 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700877 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800878 }
879
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800880 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700881 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800882 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700883 }
884
Mathias Agopian13127d82013-03-05 17:47:11 -0800885 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700886 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800887 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800888 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889}
890
Pablo Ceballos05289c22016-04-14 15:49:55 -0700891void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800892 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700893}
894
Mathias Agopian13127d82013-03-05 17:47:11 -0800895uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800897}
898
899uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800900 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800901}
902
Robert Carr82364e32016-05-15 11:27:47 -0700903bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800904 if (mCurrentState.requested_legacy.transform.tx() == x &&
905 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800906 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800907 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700908
909 // We update the requested and active position simultaneously because
910 // we want to apply the position portion of the transform matrix immediately,
911 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800912 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700913 if (immediate && !mFreezeGeometryUpdates) {
914 // Here we directly update the active state
915 // unlike other setters, because we store it within
916 // the transform, but use different latching rules.
917 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800918 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700919 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700920 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700921
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800922 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800923 setTransactionFlags(eTransactionNeeded);
924 return true;
925}
Robert Carr82364e32016-05-15 11:27:47 -0700926
Robert Carr1f0a16a2016-10-24 16:27:39 -0700927bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
928 ssize_t idx = mCurrentChildren.indexOf(childLayer);
929 if (idx < 0) {
930 return false;
931 }
932 if (childLayer->setLayer(z)) {
933 mCurrentChildren.removeAt(idx);
934 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800935 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700936 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800937 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700938}
939
Robert Carr503c7042017-09-27 15:06:08 -0700940bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
941 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
942 ssize_t idx = mCurrentChildren.indexOf(childLayer);
943 if (idx < 0) {
944 return false;
945 }
946 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
947 mCurrentChildren.removeAt(idx);
948 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800949 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700950 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800951 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700952}
953
Robert Carrae060832016-11-28 10:51:00 -0800954bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800955 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
956 mCurrentState.sequence++;
957 mCurrentState.z = z;
958 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700959
960 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800961 if (mCurrentState.zOrderRelativeOf != nullptr) {
962 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700963 if (strongRelative != nullptr) {
964 strongRelative->removeZOrderRelative(this);
965 }
chaviw606e5cf2019-03-01 10:12:10 -0800966 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700967 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800968 setTransactionFlags(eTransactionNeeded);
969 return true;
970}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700971
Robert Carrdb66e622017-04-10 16:55:57 -0700972void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800973 mCurrentState.zOrderRelatives.remove(relative);
974 mCurrentState.sequence++;
975 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700976 setTransactionFlags(eTransactionNeeded);
977}
978
979void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800980 mCurrentState.zOrderRelatives.add(relative);
981 mCurrentState.modified = true;
982 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700983 setTransactionFlags(eTransactionNeeded);
984}
985
chaviw606e5cf2019-03-01 10:12:10 -0800986void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
987 mCurrentState.zOrderRelativeOf = relativeOf;
988 mCurrentState.sequence++;
989 mCurrentState.modified = true;
990 setTransactionFlags(eTransactionNeeded);
991}
992
Robert Carr503d2bd2017-12-04 15:49:47 -0800993bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700994 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
995 if (handle == nullptr) {
996 return false;
997 }
998 sp<Layer> relative = handle->owner.promote();
999 if (relative == nullptr) {
1000 return false;
1001 }
1002
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001003 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1004 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001005 return false;
1006 }
1007
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001008 mCurrentState.sequence++;
1009 mCurrentState.modified = true;
1010 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001011
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001012 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001013 if (oldZOrderRelativeOf != nullptr) {
1014 oldZOrderRelativeOf->removeZOrderRelative(this);
1015 }
chaviw606e5cf2019-03-01 10:12:10 -08001016 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001017 relative->addZOrderRelative(this);
1018
1019 setTransactionFlags(eTransactionNeeded);
1020
1021 return true;
1022}
1023
Mathias Agopian13127d82013-03-05 17:47:11 -08001024bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001025 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001026 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001027 mCurrentState.requested_legacy.w = w;
1028 mCurrentState.requested_legacy.h = h;
1029 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001030 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001031
1032 // record the new size, from this point on, when the client request
1033 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001034 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001035 return true;
1036}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001037bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001038 if (mCurrentState.color.a == alpha) return false;
1039 mCurrentState.sequence++;
1040 mCurrentState.color.a = alpha;
1041 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001042 setTransactionFlags(eTransactionNeeded);
1043 return true;
1044}
chaviw13fdc492017-06-27 12:40:18 -07001045
Valerie Haudd0b7572019-01-29 14:59:27 -08001046bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1047 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001048 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001049 }
1050 mCurrentState.sequence++;
1051 mCurrentState.modified = true;
1052 setTransactionFlags(eTransactionNeeded);
1053
1054 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001055 // create background color layer if one does not yet exist
1056 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1057 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001058 mCurrentState.bgColorLayer = new ColorLayer(
1059 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001060
Valerie Haudd0b7572019-01-29 14:59:27 -08001061 // add to child list
1062 addChild(mCurrentState.bgColorLayer);
1063 mFlinger->mLayersAdded = true;
1064 // set up SF to handle added color layer
1065 if (isRemovedFromCurrentState()) {
1066 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1067 }
1068 mFlinger->setTransactionFlags(eTransactionNeeded);
1069 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1070 mCurrentState.bgColorLayer->reparent(nullptr);
1071 mCurrentState.bgColorLayer = nullptr;
1072 return true;
1073 }
1074
1075 mCurrentState.bgColorLayer->setColor(color);
1076 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1077 mCurrentState.bgColorLayer->setAlpha(alpha);
1078 mCurrentState.bgColorLayer->setDataspace(dataspace);
1079
chaviw13fdc492017-06-27 12:40:18 -07001080 return true;
1081}
1082
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001083bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001084 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001085
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 mCurrentState.sequence++;
1087 mCurrentState.cornerRadius = cornerRadius;
1088 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001089 setTransactionFlags(eTransactionNeeded);
1090 return true;
1091}
1092
Robert Carrd4ae7f32018-06-07 16:10:57 -07001093bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1094 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001095 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001096 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1097
1098 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1099 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1100 return false;
1101 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001102 mCurrentState.sequence++;
1103 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1104 matrix.dsdy);
1105 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001106 setTransactionFlags(eTransactionNeeded);
1107 return true;
1108}
Marissa Wall61c58622018-07-18 10:12:20 -07001109
Mathias Agopian13127d82013-03-05 17:47:11 -08001110bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001111 mCurrentState.requestedTransparentRegion_legacy = transparent;
1112 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001113 setTransactionFlags(eTransactionNeeded);
1114 return true;
1115}
1116bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001117 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1118 if (mCurrentState.flags == newFlags) return false;
1119 mCurrentState.sequence++;
1120 mCurrentState.flags = newFlags;
1121 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001122 setTransactionFlags(eTransactionNeeded);
1123 return true;
1124}
Robert Carr99e27f02016-06-16 15:18:02 -07001125
Marissa Wallf58c14b2018-07-24 10:50:43 -07001126bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001127 if (mCurrentState.requestedCrop_legacy == crop) return false;
1128 mCurrentState.sequence++;
1129 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001130 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001131 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001132 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001133 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1134
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001135 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001136 setTransactionFlags(eTransactionNeeded);
1137 return true;
1138}
Robert Carr8d5227b2017-03-16 15:41:03 -07001139
Robert Carrc3574f72016-03-24 12:19:32 -07001140bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001141 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001142 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001143 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001144 return true;
1145}
1146
Evan Roskyef876c92019-01-25 17:46:06 -08001147bool Layer::setMetadata(const LayerMetadata& data) {
1148 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001149 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001150 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001151 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001152 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001153}
1154
Mathias Agopian13127d82013-03-05 17:47:11 -08001155bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001156 if (mCurrentState.layerStack == layerStack) return false;
1157 mCurrentState.sequence++;
1158 mCurrentState.layerStack = layerStack;
1159 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001160 setTransactionFlags(eTransactionNeeded);
1161 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001162}
1163
Peiyong Linc502cb72019-03-01 15:00:23 -08001164bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1165 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1166 return false;
1167 }
1168 mCurrentState.sequence++;
1169 mCurrentState.colorSpaceAgnostic = agnostic;
1170 mCurrentState.modified = true;
1171 setTransactionFlags(eTransactionNeeded);
1172 return true;
1173}
1174
Robert Carr1f0a16a2016-10-24 16:27:39 -07001175uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001176 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001177 if (p == nullptr) {
1178 return getDrawingState().layerStack;
1179 }
1180 return p->getLayerStack();
1181}
1182
Marissa Wallf58c14b2018-07-24 10:50:43 -07001183void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001184 mCurrentState.barrierLayer_legacy = barrierLayer;
1185 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001186 // We don't set eTransactionNeeded, because just receiving a deferral
1187 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001188 mCurrentState.modified = true;
1189 pushPendingState();
1190 mCurrentState.barrierLayer_legacy = nullptr;
1191 mCurrentState.frameNumber_legacy = 0;
1192 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001193}
1194
Marissa Wallf58c14b2018-07-24 10:50:43 -07001195void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001196 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001197 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001198}
1199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200// ----------------------------------------------------------------------------
1201// pageflip handling...
1202// ----------------------------------------------------------------------------
1203
Robert Carr1f0a16a2016-10-24 16:27:39 -07001204bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001205 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001206 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001207 if (parent != nullptr && parent->isHiddenByPolicy()) {
1208 return true;
1209 }
1210 return s.flags & layer_state_t::eLayerHidden;
1211}
1212
David Sodman41fdfc92017-11-06 16:09:56 -08001213uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001214 // TODO: should we do something special if mSecure is set?
1215 if (mProtectedByApp) {
1216 // need a hardware-protected path to external video sink
1217 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001218 }
Riley Andrews03414a12014-07-01 14:22:59 -07001219 if (mPotentialCursor) {
1220 usage |= GraphicBuffer::USAGE_CURSOR;
1221 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001222 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001223 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001224}
1225
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001226void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001227 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001228 // Disable setting transform hint if the debug flag is set or if the
1229 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1230 // in one orientation.
1231 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001232 // The transform hint is used to improve performance, but we can
1233 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001234 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001235 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001236 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001237 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001238 orientation = 0;
1239 }
1240 }
David Sodmaneb085e02017-10-05 18:49:04 -07001241 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001242}
1243
Mathias Agopian13127d82013-03-05 17:47:11 -08001244// ----------------------------------------------------------------------------
1245// debugging
1246// ----------------------------------------------------------------------------
1247
Marissa Wall61c58622018-07-18 10:12:20 -07001248// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001249LayerDebugInfo Layer::getLayerDebugInfo() const {
1250 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001251 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001252 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001253 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001254 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001255 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001256 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001257 info.mVisibleRegion = visibleRegion;
1258 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001259 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001260 info.mX = ds.active_legacy.transform.tx();
1261 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001262 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001263 info.mWidth = ds.active_legacy.w;
1264 info.mHeight = ds.active_legacy.h;
1265 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001266 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001267 info.mFlags = ds.flags;
1268 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001269 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001270 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1271 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1272 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1273 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001274 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001275 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001276 if (buffer != 0) {
1277 info.mActiveBufferWidth = buffer->getWidth();
1278 info.mActiveBufferHeight = buffer->getHeight();
1279 info.mActiveBufferStride = buffer->getStride();
1280 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001281 } else {
1282 info.mActiveBufferWidth = 0;
1283 info.mActiveBufferHeight = 0;
1284 info.mActiveBufferStride = 0;
1285 info.mActiveBufferFormat = 0;
1286 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001287 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001288 info.mNumQueuedFrames = getQueuedFrameCount();
1289 info.mRefreshPending = isBufferLatched();
1290 info.mIsOpaque = isOpaque(ds);
1291 info.mContentDirty = contentDirty;
1292 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001294
Yiwei Zhang5434a782018-12-05 18:06:32 -08001295void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001296 result.append("-------------------------------");
1297 result.append("-------------------------------");
1298 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001299 result.append(" Layer name\n");
1300 result.append(" Z | ");
1301 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001302 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001303 result.append(" Disp Frame (LTRB) | ");
1304 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001305 result.append("-------------------------------");
1306 result.append("-------------------------------");
1307 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001308}
1309
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001310void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1311 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1312 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001313 return;
1314 }
1315
Yiwei Zhang5434a782018-12-05 18:06:32 -08001316 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001317 if (mName.length() > 77) {
1318 std::string shortened;
1319 shortened.append(mName.string(), 36);
1320 shortened.append("[...]");
1321 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001322 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001323 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001324 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001325 }
1326
Yiwei Zhang5434a782018-12-05 18:06:32 -08001327 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001328
Alec Mourib416efd2018-09-06 21:01:59 +00001329 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001330 const auto& compositionState = outputLayer->getState();
1331
Chia-I Wu1e043612018-03-01 09:45:09 -08001332 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001333 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001334 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001335 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001336 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001337 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1338 StringAppendF(&result, "%10s | ",
1339 toString(getCompositionLayer() ? compositionState.bufferTransform
1340 : static_cast<Hwc2::Transform>(0))
1341 .c_str());
1342 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001343 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001344 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001345 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1346 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001347
Yichi Chen6ca35192018-05-29 12:20:43 +08001348 result.append("- - - - - - - - - - - - - - - -");
1349 result.append("- - - - - - - - - - - - - - - -");
1350 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001351}
Dan Stozae22aec72016-08-01 13:20:59 -07001352
Yiwei Zhang5434a782018-12-05 18:06:32 -08001353void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001354 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001355}
1356
Svetoslavd85084b2014-03-20 10:28:31 -07001357void Layer::clearFrameStats() {
1358 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001359}
1360
Jamie Gennis6547ff42013-07-16 20:12:42 -07001361void Layer::logFrameStats() {
1362 mFrameTracker.logAndResetStats(mName);
1363}
1364
Svetoslavd85084b2014-03-20 10:28:31 -07001365void Layer::getFrameStats(FrameStats* outStats) const {
1366 mFrameTracker.getStats(outStats);
1367}
1368
Yiwei Zhang5434a782018-12-05 18:06:32 -08001369void Layer::dumpFrameEvents(std::string& result) {
1370 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001371 Mutex::Autolock lock(mFrameEventHistoryMutex);
1372 mFrameEventHistory.checkFencesForCompletion();
1373 mFrameEventHistory.dump(result);
1374}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001375
Brian Anderson5ea5e592016-12-01 16:54:33 -08001376void Layer::onDisconnect() {
1377 Mutex::Autolock lock(mFrameEventHistoryMutex);
1378 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001379 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001380}
1381
Brian Anderson3890c392016-07-25 12:48:08 -07001382void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001383 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001384 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001385 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1386 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001387 }
1388
Brian Andersond6927fb2016-07-23 23:37:30 -07001389 Mutex::Autolock lock(mFrameEventHistoryMutex);
1390 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001391 // If there are any unsignaled fences in the aquire timeline at this
1392 // point, the previously queued frame hasn't been latched yet. Go ahead
1393 // and try to get the signal time here so the syscall is taken out of
1394 // the main thread's critical path.
1395 mAcquireTimeline.updateSignalTimes();
1396 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001397 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001398 mFrameEventHistory.addQueue(*newTimestamps);
1399 }
1400
Brian Anderson3890c392016-07-25 12:48:08 -07001401 if (outDelta) {
1402 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001403 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001404}
Dan Stozae77c7662016-05-13 11:37:28 -07001405
Chia-I Wu98f1c102017-05-30 14:54:08 -07001406size_t Layer::getChildrenCount() const {
1407 size_t count = 0;
1408 for (const sp<Layer>& child : mCurrentChildren) {
1409 count += 1 + child->getChildrenCount();
1410 }
1411 return count;
1412}
1413
Robert Carr1f0a16a2016-10-24 16:27:39 -07001414void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001415 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001416 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001417
Robert Carr1f0a16a2016-10-24 16:27:39 -07001418 mCurrentChildren.add(layer);
1419 layer->setParent(this);
1420}
1421
1422ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001423 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001424 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001425
Robert Carr1323c952019-01-28 18:13:27 -08001426 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001427 return mCurrentChildren.remove(layer);
1428}
1429
Robert Carr1db73f62016-12-21 12:58:51 -08001430bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1431 sp<Handle> handle = nullptr;
1432 sp<Layer> newParent = nullptr;
1433 if (newParentHandle == nullptr) {
1434 return false;
1435 }
1436 handle = static_cast<Handle*>(newParentHandle.get());
1437 newParent = handle->owner.promote();
1438 if (newParent == nullptr) {
1439 ALOGE("Unable to promote Layer handle");
1440 return false;
1441 }
1442
chaviw5aedec92018-10-22 10:40:38 -07001443 if (attachChildren()) {
1444 setTransactionFlags(eTransactionNeeded);
1445 }
Robert Carr1db73f62016-12-21 12:58:51 -08001446 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001447 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001448 }
1449 mCurrentChildren.clear();
1450
1451 return true;
1452}
1453
Robert Carr15eae092018-03-23 13:43:53 -07001454void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001455 for (const sp<Layer>& child : mDrawingChildren) {
1456 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001457 child->computeBounds(newParent->mBounds,
1458 newParent->getTransformWithScale(
1459 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001460 }
1461}
1462
chaviwf1961f72017-09-18 16:41:07 -07001463bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001464 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001465
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001466 // While layers are detached, we allow most operations
1467 // and simply halt performing the actual transaction. However
1468 // for reparent != null we would enter the mRemovedFromCurrentState
1469 // state, regardless of whether doTransaction was called, and
1470 // so we need to prevent the update here.
1471 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001472 return false;
1473 }
1474
Robert Carr54cf5b12019-01-25 14:02:28 -08001475 sp<Layer> newParent;
1476 if (newParentHandle != nullptr) {
1477 auto handle = static_cast<Handle*>(newParentHandle.get());
1478 newParent = handle->owner.promote();
1479 if (newParent == nullptr) {
1480 ALOGE("Unable to promote Layer handle");
1481 return false;
1482 }
1483 if (newParent == this) {
1484 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1485 return false;
1486 }
1487 }
1488
chaviwf1961f72017-09-18 16:41:07 -07001489 sp<Layer> parent = getParent();
1490 if (parent != nullptr) {
1491 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001492 }
1493
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001494 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001495 newParent->addChild(this);
1496 if (!newParent->isRemovedFromCurrentState()) {
1497 addToCurrentState();
1498 } else {
1499 onRemovedFromCurrentState();
1500 }
1501
1502 if (mLayerDetached) {
1503 mLayerDetached = false;
1504 callSetTransactionFlags = true;
1505 }
1506 } else {
1507 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001508 }
1509
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001510 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001511 setTransactionFlags(eTransactionNeeded);
1512 }
chaviw06178942017-07-27 10:25:59 -07001513 return true;
1514}
1515
Robert Carr9524cb32017-02-13 11:32:32 -08001516bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001517 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001518 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001519 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001520 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001521 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001522 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001523 }
Robert Carr7f619b22017-11-06 12:56:35 -08001524 }
Robert Carr9524cb32017-02-13 11:32:32 -08001525
1526 return true;
1527}
1528
chaviw5aedec92018-10-22 10:40:38 -07001529bool Layer::attachChildren() {
1530 bool changed = false;
1531 for (const sp<Layer>& child : mCurrentChildren) {
1532 sp<Client> parentClient = mClientRef.promote();
1533 sp<Client> client(child->mClientRef.promote());
1534 if (client != nullptr && parentClient != client) {
1535 if (child->mLayerDetached) {
1536 child->mLayerDetached = false;
1537 changed = true;
1538 }
1539 changed |= child->attachChildren();
1540 }
1541 }
1542
1543 return changed;
1544}
1545
Peiyong Lind3788632018-09-18 16:01:31 -07001546bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001547 static const mat4 identityMatrix = mat4();
1548
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001549 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001550 return false;
1551 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001552 ++mCurrentState.sequence;
1553 mCurrentState.colorTransform = matrix;
1554 mCurrentState.hasColorTransform = matrix != identityMatrix;
1555 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001556 setTransactionFlags(eTransactionNeeded);
1557 return true;
1558}
1559
chaviwf66724d2018-11-28 16:35:21 -08001560mat4 Layer::getColorTransform() const {
1561 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1562 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1563 colorTransform = parent->getColorTransform() * colorTransform;
1564 }
1565 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001566}
1567
1568bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001569 bool hasColorTransform = getDrawingState().hasColorTransform;
1570 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1571 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1572 }
1573 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001574}
1575
Chia-I Wu11481472018-05-04 10:43:19 -07001576bool Layer::isLegacyDataSpace() const {
1577 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001578 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001579 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001580}
1581
Robert Carr1f0a16a2016-10-24 16:27:39 -07001582void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001583 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001584}
1585
Robert Carr1f0a16a2016-10-24 16:27:39 -07001586int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001587 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001588}
1589
Robert Carr29abff82017-12-04 13:51:20 -08001590bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1591 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001592 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001593 return state.zOrderRelativeOf != nullptr;
1594}
1595
David Sodman41fdfc92017-11-06 16:09:56 -08001596__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001597 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001598 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1599 "makeTraversalList received invalid stateSet");
1600 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1601 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001602 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001603
Robert Carr29abff82017-12-04 13:51:20 -08001604 if (state.zOrderRelatives.size() == 0) {
1605 *outSkipRelativeZUsers = true;
1606 return children;
1607 }
1608
chaviwfd462612018-05-31 16:11:27 -07001609 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001610 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001611 sp<Layer> strongRelative = weakRelative.promote();
1612 if (strongRelative != nullptr) {
1613 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001614 }
1615 }
1616
Dan Stoza412903f2017-04-27 13:42:17 -07001617 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001618 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001619 if (childState.zOrderRelativeOf != nullptr) {
1620 continue;
1621 }
Robert Carrdb66e622017-04-10 16:55:57 -07001622 traverse.add(child);
1623 }
1624
1625 return traverse;
1626}
1627
Robert Carr1f0a16a2016-10-24 16:27:39 -07001628/**
Robert Carrdb66e622017-04-10 16:55:57 -07001629 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001630 */
Dan Stoza412903f2017-04-27 13:42:17 -07001631void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001632 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1633 // produce a new list for traversing, including our relatives, and not including our children
1634 // who are relatives of another surface. In the case that there are no relative Z,
1635 // makeTraversalList returns our children directly to avoid significant overhead.
1636 // However in this case we need to take the responsibility for filtering children which
1637 // are relatives of another surface here.
1638 bool skipRelativeZUsers = false;
1639 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001640
Robert Carr1f0a16a2016-10-24 16:27:39 -07001641 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001642 for (; i < list.size(); i++) {
1643 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001644 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1645 continue;
1646 }
1647
Robert Carrdb66e622017-04-10 16:55:57 -07001648 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001649 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001650 }
Dan Stoza412903f2017-04-27 13:42:17 -07001651 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001652 }
Robert Carr29abff82017-12-04 13:51:20 -08001653
Dan Stoza412903f2017-04-27 13:42:17 -07001654 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001655 for (; i < list.size(); i++) {
1656 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001657
Robert Carr29abff82017-12-04 13:51:20 -08001658 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1659 continue;
1660 }
Dan Stoza412903f2017-04-27 13:42:17 -07001661 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001662 }
1663}
1664
1665/**
Robert Carrdb66e622017-04-10 16:55:57 -07001666 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667 */
Dan Stoza412903f2017-04-27 13:42:17 -07001668void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1669 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001670 // See traverseInZOrder for documentation.
1671 bool skipRelativeZUsers = false;
1672 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001673
Robert Carr1f0a16a2016-10-24 16:27:39 -07001674 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001675 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001676 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001677
1678 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1679 continue;
1680 }
1681
Robert Carrdb66e622017-04-10 16:55:57 -07001682 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001683 break;
1684 }
Dan Stoza412903f2017-04-27 13:42:17 -07001685 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001686 }
Dan Stoza412903f2017-04-27 13:42:17 -07001687 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001688 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001689 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001690
1691 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1692 continue;
1693 }
1694
Dan Stoza412903f2017-04-27 13:42:17 -07001695 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001696 }
1697}
1698
chaviw4b129c22018-04-09 16:19:43 -07001699LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1700 const std::vector<Layer*>& layersInTree) {
1701 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1702 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001703 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1704 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001705 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001706
chaviwfd462612018-05-31 16:11:27 -07001707 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001708 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1709 sp<Layer> strongRelative = weakRelative.promote();
1710 // Only add relative layers that are also descendents of the top most parent of the tree.
1711 // If a relative layer is not a descendent, then it should be ignored.
1712 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1713 traverse.add(strongRelative);
1714 }
1715 }
1716
1717 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001718 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001719 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1720 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1721 // the child here since it won't be added later as a relative.
1722 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1723 childState.zOrderRelativeOf.promote().get())) {
1724 continue;
1725 }
1726 traverse.add(child);
1727 }
1728
1729 return traverse;
1730}
1731
1732void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1733 LayerVector::StateSet stateSet,
1734 const LayerVector::Visitor& visitor) {
1735 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001736
1737 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001738 for (; i < list.size(); i++) {
1739 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001740 if (relative->getZ() >= 0) {
1741 break;
1742 }
chaviw4b129c22018-04-09 16:19:43 -07001743 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001744 }
chaviw4b129c22018-04-09 16:19:43 -07001745
chaviwa76b2712017-09-20 12:02:26 -07001746 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001747 for (; i < list.size(); i++) {
1748 const auto& relative = list[i];
1749 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001750 }
1751}
1752
chaviw4b129c22018-04-09 16:19:43 -07001753std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1754 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1755 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1756
1757 std::vector<Layer*> layersInTree = {this};
1758 for (size_t i = 0; i < children.size(); i++) {
1759 const auto& child = children[i];
1760 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1761 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1762 }
1763
1764 return layersInTree;
1765}
1766
1767void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1768 const LayerVector::Visitor& visitor) {
1769 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1770 std::sort(layersInTree.begin(), layersInTree.end());
1771 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1772}
1773
Peiyong Linefefaac2018-08-17 12:27:51 -07001774ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001775 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001776}
1777
chaviw13fdc492017-06-27 12:40:18 -07001778half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001779 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001780
chaviw13fdc492017-06-27 12:40:18 -07001781 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1782 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001783}
Robert Carr6452f122017-03-21 10:41:29 -07001784
chaviw13fdc492017-06-27 12:40:18 -07001785half4 Layer::getColor() const {
1786 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001787 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001788}
Robert Carr6452f122017-03-21 10:41:29 -07001789
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001790Layer::RoundedCornerState Layer::getRoundedCornerState() const {
Peiyong Lin79566952019-03-19 16:07:33 -07001791 return getRoundedCornerStateInternal(mSourceBounds);
1792}
1793
1794Layer::RoundedCornerState Layer::getRoundedCornerStateInternal(const FloatRect bounds) const {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001795 const auto& p = mDrawingParent.promote();
1796 if (p != nullptr) {
Peiyong Lin79566952019-03-19 16:07:33 -07001797 RoundedCornerState parentState = p->getRoundedCornerStateInternal(bounds);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001798 if (parentState.radius > 0) {
1799 ui::Transform t = getActiveTransform(getDrawingState());
1800 t = t.inverse();
1801 parentState.cropRect = t.transform(parentState.cropRect);
1802 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1803 // but a transform matrix can define horizontal and vertical scales.
1804 // Let's take the average between both of them and pass into the shader, practically we
1805 // never do this type of transformation on windows anyway.
1806 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1807 return parentState;
1808 }
1809 }
1810 const float radius = getDrawingState().cornerRadius;
Peiyong Lin79566952019-03-19 16:07:33 -07001811 return radius > 0
1812 ? RoundedCornerState(bounds.intersect(getCrop(getDrawingState()).toFloatRect()), radius)
1813 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001814}
1815
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816void Layer::commitChildList() {
1817 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1818 const auto& child = mCurrentChildren[i];
1819 child->commitChildList();
1820 }
1821 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001822 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823}
1824
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001825static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1826 if (weakBinderHandle == nullptr) {
1827 return nullptr;
1828 }
1829 sp<IBinder> binderHandle = weakBinderHandle.promote();
1830 if (binderHandle == nullptr) {
1831 return nullptr;
1832 }
1833 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1834 if (handle == nullptr) {
1835 return nullptr;
1836 }
1837 return handle->owner;
1838}
1839
Robert Carr720e5062018-07-30 17:45:14 -07001840void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001841 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001842 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001843 mCurrentState.modified = true;
1844 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001845 setTransactionFlags(eTransactionNeeded);
1846}
1847
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001848void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1849 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001850 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1851 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001852 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001853
Peiyong Linefefaac2018-08-17 12:27:51 -07001854 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001855 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001856
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001857 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1858 layerInfo->set_id(sequence);
1859 layerInfo->set_name(getName().c_str());
1860 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001861
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001862 for (const auto& child : children) {
1863 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001864 }
chaviw1d044282017-09-27 12:19:28 -07001865
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001866 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1867 sp<Layer> strongRelative = weakRelative.promote();
1868 if (strongRelative != nullptr) {
1869 layerInfo->add_relatives(strongRelative->sequence);
1870 }
chaviwadc40c22018-07-10 16:57:27 -07001871 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001872
1873 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1874 [&]() { return layerInfo->mutable_transparent_region(); });
1875 LayerProtoHelper::writeToProto(visibleRegion,
1876 [&]() { return layerInfo->mutable_visible_region(); });
1877 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1878 [&]() { return layerInfo->mutable_damage_region(); });
1879
1880 layerInfo->set_layer_stack(getLayerStack());
1881 layerInfo->set_z(state.z);
1882
1883 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1884 [&]() { return layerInfo->mutable_position(); });
1885
1886 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1887 [&]() {
1888 return layerInfo->mutable_requested_position();
1889 });
1890
1891 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1892 [&]() { return layerInfo->mutable_size(); });
1893
1894 LayerProtoHelper::writeToProto(state.crop_legacy,
1895 [&]() { return layerInfo->mutable_crop(); });
1896 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1897
1898 layerInfo->set_is_opaque(isOpaque(state));
1899 layerInfo->set_invalidate(contentDirty);
1900
1901 // XXX (b/79210409) mCurrentDataSpace is not protected
1902 layerInfo->set_dataspace(
1903 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1904
1905 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1906 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1907 LayerProtoHelper::writeToProto(state.color,
1908 [&]() { return layerInfo->mutable_requested_color(); });
1909 layerInfo->set_flags(state.flags);
1910
1911 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1912 LayerProtoHelper::writeToProto(requestedTransform,
1913 layerInfo->mutable_requested_transform());
1914
1915 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1916 if (parent != nullptr) {
1917 layerInfo->set_parent(parent->sequence);
1918 } else {
1919 layerInfo->set_parent(-1);
1920 }
1921
1922 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1923 if (zOrderRelativeOf != nullptr) {
1924 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1925 } else {
1926 layerInfo->set_z_order_relative_of(-1);
1927 }
1928
1929 auto buffer = mActiveBuffer;
1930 if (buffer != nullptr) {
1931 LayerProtoHelper::writeToProto(buffer,
1932 [&]() { return layerInfo->mutable_active_buffer(); });
1933 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1934 layerInfo->mutable_buffer_transform());
1935 }
1936
1937 layerInfo->set_queued_frames(getQueuedFrameCount());
1938 layerInfo->set_refresh_pending(isBufferLatched());
1939 layerInfo->set_curr_frame(mCurrentFrameNumber);
1940 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1941
1942 for (const auto& pendingState : mPendingStates) {
1943 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1944 if (barrierLayer != nullptr) {
1945 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1946 barrierLayerProto->set_id(barrierLayer->sequence);
1947 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1948 }
1949 }
1950 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001951 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001952
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001953 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1954 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1955 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001956 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001957
1958 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1959 auto protoMap = layerInfo->mutable_metadata();
1960 for (const auto& entry : state.metadata.mMap) {
1961 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1962 }
1963 LayerProtoHelper::writeToProto(mEffectiveTransform,
1964 layerInfo->mutable_effective_transform());
1965 LayerProtoHelper::writeToProto(mSourceBounds,
1966 [&]() { return layerInfo->mutable_source_bounds(); });
1967 LayerProtoHelper::writeToProto(mScreenBounds,
1968 [&]() { return layerInfo->mutable_screen_bounds(); });
1969 }
chaviw1d044282017-09-27 12:19:28 -07001970}
1971
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001972void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1973 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001974 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1975 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001976 return;
1977 }
1978
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001979 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001980
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001981 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001982
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001983 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001984 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001985
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001986 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001987 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001988
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001989 const int32_t transform =
1990 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001991 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001992
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001993 const int32_t compositionType =
1994 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1995 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001996 layerInfo->set_hwc_composition_type(compositionType);
1997
1998 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1999 static_cast<BufferLayer*>(this)->isProtected()) {
2000 layerInfo->set_is_protected(true);
2001 } else {
2002 layerInfo->set_is_protected(false);
2003 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002004}
2005
Robert Carr2e102c92018-10-23 12:11:15 -07002006bool Layer::isRemovedFromCurrentState() const {
2007 return mRemovedFromCurrentState;
2008}
2009
Arthur Hungd20b2702019-01-14 18:16:16 +08002010InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002011 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002012
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002013 if (info.displayId == ADISPLAY_ID_NONE) {
2014 info.displayId = mDrawingState.layerStack;
2015 }
2016
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002017 ui::Transform t = getTransform();
2018 const float xScale = t.sx();
2019 const float yScale = t.sy();
2020 if (xScale != 1.0f || yScale != 1.0f) {
2021 info.windowXScale *= 1.0f / xScale;
2022 info.windowYScale *= 1.0f / yScale;
2023 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002024 }
Robert Carre07e1032018-11-26 12:55:53 -08002025
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002026 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002027 // If this is a portal window, set the touchableRegion to the layerBounds.
2028 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2029 ? getBufferSize(getDrawingState())
2030 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002031 if (!layerBounds.isValid()) {
2032 layerBounds = getCroppedBufferSize(getDrawingState());
2033 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002034 layerBounds = t.transform(layerBounds);
2035 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2036
Arthur Hungd20b2702019-01-14 18:16:16 +08002037 // Input coordinate should match the layer bounds.
2038 info.frameLeft = layerBounds.left;
2039 info.frameTop = layerBounds.top;
2040 info.frameRight = layerBounds.right;
2041 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002042
2043 // Position the touchable region relative to frame screen location and restrict it to frame
2044 // bounds.
2045 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002046 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002047
2048 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2049 if (info.replaceTouchableRegionWithCrop) {
2050 if (cropLayer == nullptr) {
2051 info.touchableRegion = Region(Rect{mScreenBounds});
2052 } else {
2053 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2054 }
2055 } else if (cropLayer != nullptr) {
2056 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2057 }
2058
Robert Carr720e5062018-07-30 17:45:14 -07002059 return info;
2060}
2061
2062bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002063 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002064}
2065
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002066std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2067 return nullptr;
2068}
2069
chaviw3e727cd2019-01-31 13:41:05 -08002070bool Layer::canReceiveInput() const {
2071 return isVisible();
2072}
2073
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002074compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2075 const sp<const DisplayDevice>& display) const {
2076 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2077}
2078
Mathias Agopian13127d82013-03-05 17:47:11 -08002079// ---------------------------------------------------------------------------
2080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002081}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002082
2083#if defined(__gl_h_)
2084#error "don't include gl/gl.h in this file"
2085#endif
2086
2087#if defined(__gl2_h_)
2088#error "don't include gl2/gl2.h in this file"
2089#endif