blob: 2c73b531af0ef02826083184932e36a2349c102e [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 Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080033#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <gui/BufferItem.h>
39#include <gui/LayerDebugInfo.h>
40#include <gui/Surface.h>
41#include <renderengine/RenderEngine.h>
42#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Yiwei Zhang60d1a192018-03-07 14:52:28 -080051#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080052#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020053#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080055#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080057#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070058#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070059#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080061#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070062
David Sodman41fdfc92017-11-06 16:09:56 -080063#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065namespace android {
66
Yiwei Zhang5434a782018-12-05 18:06:32 -080067using base::StringAppendF;
68
Lloyd Piquef1c675b2018-09-12 20:45:39 -070069std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080070
Lloyd Pique42ab75e2018-09-12 20:46:03 -070071Layer::Layer(const LayerCreationArgs& args)
72 : mFlinger(args.flinger),
73 mName(args.name),
74 mClientRef(args.client),
75 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070077
78 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070079 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
80 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
81 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082
Dan Stozaf7ba41a2017-05-10 15:11:11 -070083 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070084
Lloyd Pique0449b0f2018-12-20 16:23:45 -080085 mCurrentState.active_legacy.w = args.w;
86 mCurrentState.active_legacy.h = args.h;
87 mCurrentState.flags = layerFlags;
88 mCurrentState.active_legacy.transform.set(0, 0);
89 mCurrentState.crop_legacy.makeInvalid();
90 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
91 mCurrentState.z = 0;
92 mCurrentState.color.a = 1.0f;
93 mCurrentState.layerStack = 0;
94 mCurrentState.sequence = 0;
95 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080096 mCurrentState.active.w = UINT32_MAX;
97 mCurrentState.active.h = UINT32_MAX;
98 mCurrentState.active.transform.set(0, 0);
99 mCurrentState.transform = 0;
100 mCurrentState.transformToDisplayInverse = false;
101 mCurrentState.crop.makeInvalid();
102 mCurrentState.acquireFence = new Fence(-1);
103 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
104 mCurrentState.hdrMetadata.validTypes = 0;
105 mCurrentState.surfaceDamageRegion.clear();
106 mCurrentState.cornerRadius = 0.0f;
107 mCurrentState.api = -1;
108 mCurrentState.hasColorTransform = false;
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 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800153 mCurrentState.zOrderRelativeOf = 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 Nair4351ad52019-02-11 14:13:02 -0800281ui::Transform Layer::getTransformWithScale() const {
282 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
283 // it isFixedSize) then there may be additional scaling not accounted
284 // for in the transform. We need to mirror this scaling to child surfaces
285 // or we will break the contract where WM can treat child surfaces as
286 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800287 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800288 return mEffectiveTransform;
289 }
290
291 int bufferWidth;
292 int bufferHeight;
293 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800294 bufferWidth = mActiveBuffer->getWidth();
295 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800296 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800297 bufferHeight = mActiveBuffer->getWidth();
298 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800299 }
300 float sx = getActiveWidth(getDrawingState()) / static_cast<float>(bufferWidth);
301 float sy = getActiveHeight(getDrawingState()) / static_cast<float>(bufferHeight);
302 ui::Transform extraParentScaling;
303 extraParentScaling.set(sx, 0, 0, sy);
304 return mEffectiveTransform * extraParentScaling;
305}
306
307void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
308 const State& s(getDrawingState());
309
310 // Calculate effective layer transform
311 mEffectiveTransform = parentTransform * getActiveTransform(s);
312
313 // Transform parent bounds to layer space
314 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
315
316 // Calculate display frame
317 mSourceBounds = computeSourceBounds(parentBounds);
318
319 // Calculate bounds by croping diplay frame with layer crop and parent bounds
320 FloatRect bounds = mSourceBounds;
321 const Rect layerCrop = getCrop(s);
322 if (!layerCrop.isEmpty()) {
323 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
324 }
325 bounds = bounds.intersect(parentBounds);
326
327 mBounds = bounds;
328 mScreenBounds = mEffectiveTransform.transform(mBounds);
329 for (const sp<Layer>& child : mDrawingChildren) {
330 child->computeBounds(mBounds, getTransformWithScale());
331 }
332}
333
Robert Carrb5d3d262016-03-25 15:08:13 -0700334
Robert Carr1f0a16a2016-10-24 16:27:39 -0700335
Vishnu Nair60356342018-11-13 13:00:45 -0800336Rect Layer::getCroppedBufferSize(const State& s) const {
337 Rect size = getBufferSize(s);
338 Rect crop = getCrop(s);
339 if (!crop.isEmpty() && size.isValid()) {
340 size.intersect(crop, &size);
341 } else if (!crop.isEmpty()) {
342 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700343 }
Vishnu Nair60356342018-11-13 13:00:45 -0800344 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800345}
346
Alec Mouri1f3bd182019-01-24 15:20:18 +0000347Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700348 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800349 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700350 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800351
352 // apply the projection's clipping to the window crop in
353 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700354 // if there are no window scaling involved, this operation will map to full
355 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700356
Vishnu Nair4351ad52019-02-11 14:13:02 -0800357 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000358 ui::Transform t = getTransform();
359 // Transform to screen space.
360 activeCropFloat = t.transform(activeCropFloat);
361 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
362 // Back to layer space to work with the content crop.
363 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800364 // This needs to be here as transform.transform(Rect) computes the
365 // transformed rect and then takes the bounding box of the result before
366 // returning. This means
367 // transform.inverse().transform(transform.transform(Rect)) != Rect
368 // in which case we need to make sure the final rect is clipped to the
369 // display bounds.
370 Rect activeCrop{activeCropFloat};
371 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000372 activeCrop.clear();
373 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700374 return activeCrop;
375}
376
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700377void Layer::setupRoundedCornersCropCoordinates(Rect win,
378 const FloatRect& roundedCornersCrop) const {
379 // Translate win by the rounded corners rect coordinates, to have all values in
380 // layer coordinate space.
381 win.left -= roundedCornersCrop.left;
382 win.right -= roundedCornersCrop.left;
383 win.top -= roundedCornersCrop.top;
384 win.bottom -= roundedCornersCrop.top;
385
Lloyd Pique0b785d82018-12-04 17:25:27 -0800386 renderengine::Mesh::VertexArray<vec2> cropCoords(
387 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700388 cropCoords[0] = vec2(win.left, win.top);
389 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
390 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
391 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700392}
393
Alec Mouri1f3bd182019-01-24 15:20:18 +0000394FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700395 // the content crop is the area of the content that gets scaled to the
396 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800397 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700398
399 // In addition there is a WM-specified crop we pull from our drawing state.
400 const State& s(getDrawingState());
401
Alec Mouri1f3bd182019-01-24 15:20:18 +0000402 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800403 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700404
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000405 // Transform the window crop to match the buffer coordinate system,
406 // which means using the inverse of the current transform set on the
407 // SurfaceFlingerConsumer.
408 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800409 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000410 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700411 * the code below applies the primary display's inverse transform to the
412 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000413 */
David Sodman41fdfc92017-11-06 16:09:56 -0800414 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415 // calculate the inverse transform
416 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800417 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800418 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000419 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700420 invTransform = (ui::Transform(invTransformOrient) *
421 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800422 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000423
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800424 int winWidth = bufferSize.getWidth();
425 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
427 // If the activeCrop has been rotate the ends are rotated but not
428 // the space itself so when transforming ends back we can't rely on
429 // a modification of the axes of rotation. To account for this we
430 // need to reorient the inverse rotation in terms of the current
431 // axes of rotation.
432 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
433 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
434 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800435 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800437 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800439 const Rect winCrop =
440 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441
442 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800443 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 float yScale = crop.getHeight() / float(winHeight);
445
David Sodman41fdfc92017-11-06 16:09:56 -0800446 float insetL = winCrop.left * xScale;
447 float insetT = winCrop.top * yScale;
448 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 float insetB = (winHeight - winCrop.bottom) * yScale;
450
David Sodman41fdfc92017-11-06 16:09:56 -0800451 crop.left += insetL;
452 crop.top += insetT;
453 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 crop.bottom -= insetB;
455
Mathias Agopian13127d82013-03-05 17:47:11 -0800456 return crop;
457}
458
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700459void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800460 const auto outputLayer = findOutputLayerForDisplay(display);
461 LOG_FATAL_IF(!outputLayer);
462 LOG_FATAL_IF(!outputLayer->getState().hwc);
463 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700464
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800465 if (!hasHwcLayer(display)) {
466 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
467 display->getDebugName().c_str());
468 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800469 }
470
Lloyd Pique0b785d82018-12-04 17:25:27 -0800471 LOG_FATAL_IF(!getCompositionLayer());
472 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800473 auto& compositionState = outputLayer->editState();
474
475 // enable this layer
476 compositionState.forceClientComposition = false;
477
478 if (isSecure() && !display->isSecure()) {
479 compositionState.forceClientComposition = true;
480 }
David Sodman15094112018-10-11 09:39:37 -0700481
Mathias Agopian13127d82013-03-05 17:47:11 -0800482 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700483 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800484 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500485 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800486 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800487 blendMode =
488 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800489 }
David Sodman15094112018-10-11 09:39:37 -0700490 auto error = hwcLayer->setBlendMode(blendMode);
491 ALOGE_IF(error != HWC2::Error::None,
492 "[%s] Failed to set blend mode %s:"
493 " %s (%d)",
494 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
495 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800496 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800497
498 // apply the layer's transform, followed by the display's global transform
499 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700500 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800501 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700502 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800503 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700504 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700505 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000506 activeCrop.clear();
507 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700508 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800509 // This needs to be here as transform.transform(Rect) computes the
510 // transformed rect and then takes the bounding box of the result before
511 // returning. This means
512 // transform.inverse().transform(transform.transform(Rect)) != Rect
513 // in which case we need to make sure the final rect is clipped to the
514 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800515 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000516 activeCrop.clear();
517 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700518 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800519 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
520 activeTransparentRegion.orSelf(
521 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800522 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
523 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800524 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700525 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700526
Vishnu Nair4351ad52019-02-11 14:13:02 -0800527 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800528 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800529 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700530 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000531 frame.clear();
532 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700533 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800534 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700535 error = hwcLayer->setDisplayFrame(transformedFrame);
536 if (error != HWC2::Error::None) {
537 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
538 transformedFrame.left, transformedFrame.top, transformedFrame.right,
539 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
540 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800541 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700542 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800543
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700544 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700545 error = hwcLayer->setSourceCrop(sourceCrop);
546 if (error != HWC2::Error::None) {
547 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
548 "%s (%d)",
549 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
550 to_string(error).c_str(), static_cast<int32_t>(error));
551 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800552 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700553 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800555 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700556 error = hwcLayer->setPlaneAlpha(alpha);
557 ALOGE_IF(error != HWC2::Error::None,
558 "[%s] Failed to set plane alpha %.3f: "
559 "%s (%d)",
560 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800561 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562
David Sodman15094112018-10-11 09:39:37 -0700563 error = hwcLayer->setZOrder(z);
564 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
565 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800566 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500567
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800568 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
569 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700570 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400571 if (parent.get()) {
572 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800573 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
574 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
575 if (parentType >= 0 || parentAppId >= 0) {
576 type = parentType;
577 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700578 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400579 }
580
David Sodman15094112018-10-11 09:39:37 -0700581 error = hwcLayer->setInfo(type, appId);
582 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
583 static_cast<int32_t>(error));
584
Lloyd Pique0b785d82018-12-04 17:25:27 -0800585 commonCompositionState.type = type;
586 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700587
Mathias Agopian29a367b2011-07-12 14:51:45 -0700588 /*
589 * Transformations are applied in this order:
590 * 1) buffer orientation/flip/mirror
591 * 2) state transformation (window manager)
592 * 3) layer orientation (screen orientation)
593 * (NOTE: the matrices are multiplied in reverse order)
594 */
595
Peiyong Linefefaac2018-08-17 12:27:51 -0700596 const ui::Transform bufferOrientation(mCurrentTransform);
597 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700598
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800599 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700600 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700601 * the code below applies the primary display's inverse transform to the
602 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700603 */
David Sodman41fdfc92017-11-06 16:09:56 -0800604 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700605 // calculate the inverse transform
606 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800607 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700608 }
Robert Carrcae605c2017-03-29 12:10:31 -0700609
610 /*
611 * Here we cancel out the orientation component of the WM transform.
612 * The scaling and translate components are already included in our bounds
613 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000614 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700615 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700616 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700618
619 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800620 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700621 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800623 compositionState.forceClientComposition = true;
624 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800625 } else {
626 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700627 auto error = hwcLayer->setTransform(transform);
628 ALOGE_IF(error != HWC2::Error::None,
629 "[%s] Failed to set transform %s: "
630 "%s (%d)",
631 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
632 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800633 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800634 }
635}
636
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800637void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
638 const auto outputLayer = findOutputLayerForDisplay(display);
639 LOG_FATAL_IF(!outputLayer);
640 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641}
Dan Stozaee44edd2015-03-23 15:50:23 -0700642
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800643bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
644 const auto outputLayer = findOutputLayerForDisplay(display);
645 LOG_FATAL_IF(!outputLayer);
646 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800647}
648
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700649void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800650 const auto outputLayer = findOutputLayerForDisplay(display);
651 LOG_FATAL_IF(!outputLayer);
652
653 if (!outputLayer->getState().hwc ||
654 (*outputLayer->getState().hwc).hwcCompositionType !=
655 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656 return;
657 }
658
659 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800660 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661
662 // Apply the layer's transform, followed by the display's global transform
663 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800664 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700666 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800667 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700668 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700669 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670 auto position = displayTransform.transform(frame);
671
Dominik Laskowski7e045462018-05-30 13:02:02 -0700672 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800673 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800674 ALOGE_IF(error != HWC2::Error::None,
675 "[%s] Failed to set cursor position "
676 "to (%d, %d): %s (%d)",
677 mName.string(), position.left, position.top, to_string(error).c_str(),
678 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679}
Riley Andrews03414a12014-07-01 14:22:59 -0700680
Mathias Agopian13127d82013-03-05 17:47:11 -0800681// ---------------------------------------------------------------------------
682// drawing...
683// ---------------------------------------------------------------------------
684
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000685bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
686 Region& clearRegion, renderengine::LayerSettings& layer) {
687 return prepareClientLayer(renderArea, clip, false, clearRegion, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800688}
689
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000690bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
691 Region& clearRegion, renderengine::LayerSettings& layer) {
692 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
693 clearRegion, layer);
694}
695
696bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
697 bool useIdentityTransform, Region& /*clearRegion*/,
698 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800699 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000700 half alpha = getAlpha();
701 layer.geometry.boundaries = bounds;
702 if (useIdentityTransform) {
703 layer.geometry.positionTransform = mat4();
704 } else {
705 const ui::Transform transform = getTransform();
706 mat4 m;
707 m[0][0] = transform[0][0];
708 m[0][1] = transform[0][1];
709 m[0][3] = transform[0][2];
710 m[1][0] = transform[1][0];
711 m[1][1] = transform[1][1];
712 m[1][3] = transform[1][2];
713 m[3][0] = transform[2][0];
714 m[3][1] = transform[2][1];
715 m[3][3] = transform[2][2];
716 layer.geometry.positionTransform = m;
717 }
718
719 if (hasColorTransform()) {
720 layer.colorTransform = getColorTransform();
721 }
722
723 const auto roundedCornerState = getRoundedCornerState();
724 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
725 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
726
727 layer.alpha = alpha;
728 layer.sourceDataspace = mCurrentDataSpace;
729 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800730}
731
David Sodman41fdfc92017-11-06 16:09:56 -0800732void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
733 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800734 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800735 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700736 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800737 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800738}
739
David Sodmanc1498e62018-09-12 14:36:26 -0700740void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
741 clearWithOpenGL(renderArea, 0, 0, 0, 0);
742}
743
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800744void Layer::setCompositionType(const sp<const DisplayDevice>& display,
745 Hwc2::IComposerClient::Composition type) {
746 const auto outputLayer = findOutputLayerForDisplay(display);
747 LOG_FATAL_IF(!outputLayer);
748 LOG_FATAL_IF(!outputLayer->getState().hwc);
749 auto& compositionState = outputLayer->editState();
750
751 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
752 toString(type).c_str(), 1);
753 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700754 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800755 (*compositionState.hwc).hwcCompositionType = type;
756
757 auto error = (*compositionState.hwc)
758 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
759 ALOGE_IF(error != HWC2::Error::None,
760 "[%s] Failed to set "
761 "composition type %s: %s (%d)",
762 mName.string(), toString(type).c_str(), to_string(error).c_str(),
763 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764 }
765}
766
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800767Hwc2::IComposerClient::Composition Layer::getCompositionType(
768 const sp<const DisplayDevice>& display) const {
769 const auto outputLayer = findOutputLayerForDisplay(display);
770 LOG_FATAL_IF(!outputLayer);
771 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
772 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773}
774
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800775bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
776 const auto outputLayer = findOutputLayerForDisplay(display);
777 LOG_FATAL_IF(!outputLayer);
778 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800779}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780
Dan Stozacac35382016-01-27 12:21:06 -0800781bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
782 if (point->getFrameNumber() <= mCurrentFrameNumber) {
783 // Don't bother with a SyncPoint, since we've already latched the
784 // relevant frame
785 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700786 }
Robert Carr2e102c92018-10-23 12:11:15 -0700787 if (isRemovedFromCurrentState()) {
788 return false;
789 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700790
Dan Stozacac35382016-01-27 12:21:06 -0800791 Mutex::Autolock lock(mLocalSyncPointMutex);
792 mLocalSyncPoints.push_back(point);
793 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700794}
795
Mathias Agopian13127d82013-03-05 17:47:11 -0800796// ----------------------------------------------------------------------------
797// local state
798// ----------------------------------------------------------------------------
799
Peiyong Lin833074a2018-08-28 11:53:54 -0700800void Layer::computeGeometry(const RenderArea& renderArea,
801 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700802 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700803 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800804 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700805
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000806 vec2 lt = vec2(win.left, win.top);
807 vec2 lb = vec2(win.left, win.bottom);
808 vec2 rb = vec2(win.right, win.bottom);
809 vec2 rt = vec2(win.right, win.top);
810
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800811 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000812 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700813 lt = layerTransform.transform(lt);
814 lb = layerTransform.transform(lb);
815 rb = layerTransform.transform(rb);
816 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000817 }
818
Peiyong Lin833074a2018-08-28 11:53:54 -0700819 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700820 position[0] = renderAreaTransform.transform(lt);
821 position[1] = renderAreaTransform.transform(lb);
822 position[2] = renderAreaTransform.transform(rb);
823 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800824}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800825
David Sodman41fdfc92017-11-06 16:09:56 -0800826bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800827 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700828 return (s.flags & layer_state_t::eLayerSecure);
829}
830
Mathias Agopian13127d82013-03-05 17:47:11 -0800831void Layer::setVisibleRegion(const Region& visibleRegion) {
832 // always called from main thread
833 this->visibleRegion = visibleRegion;
834}
835
836void Layer::setCoveredRegion(const Region& coveredRegion) {
837 // always called from main thread
838 this->coveredRegion = coveredRegion;
839}
840
David Sodman41fdfc92017-11-06 16:09:56 -0800841void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800842 // always called from main thread
843 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
844}
845
Robert Carre5f4f692018-01-12 13:12:28 -0800846void Layer::clearVisibilityRegions() {
847 visibleRegion.clear();
848 visibleNonTransparentRegion.clear();
849 coveredRegion.clear();
850}
851
Mathias Agopian13127d82013-03-05 17:47:11 -0800852// ----------------------------------------------------------------------------
853// transaction
854// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800855
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800856void Layer::pushPendingState() {
857 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700858 return;
859 }
860
Dan Stoza7dde5992015-05-22 09:51:44 -0700861 // If this transaction is waiting on the receipt of a frame, generate a sync
862 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700863 // We don't allow installing sync points after we are removed from the current state
864 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800865 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
866 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800867 if (barrierLayer == nullptr) {
868 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700869 // If we can't promote the layer we are intended to wait on,
870 // then it is expired or otherwise invalid. Allow this transaction
871 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800872 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800873 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800874 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800875 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800876 mRemoteSyncPoints.push_back(std::move(syncPoint));
877 } else {
878 // We already missed the frame we're supposed to synchronize
879 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800880 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800881 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700882 }
883
Dan Stoza7dde5992015-05-22 09:51:44 -0700884 // Wake us up to check if the frame has been received
885 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700886 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800888 mPendingStates.push_back(mCurrentState);
889 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700890}
891
Pablo Ceballos05289c22016-04-14 15:49:55 -0700892void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800893 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700894
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800895 mPendingStates.removeAt(0);
896 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700897}
898
Pablo Ceballos05289c22016-04-14 15:49:55 -0700899bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 while (!mPendingStates.empty()) {
902 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700903 if (mRemoteSyncPoints.empty()) {
904 // If we don't have a sync point for this, apply it anyway. It
905 // will be visually wrong, but it should keep us from getting
906 // into too much trouble.
907 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700908 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700909 stateUpdateAvailable = true;
910 continue;
911 }
912
Marissa Wallf58c14b2018-07-24 10:50:43 -0700913 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800915 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800916
917 // Signal our end of the sync point and then dispose of it
918 mRemoteSyncPoints.front()->setTransactionApplied();
919 mRemoteSyncPoints.pop_front();
920 continue;
921 }
922
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
924 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 stateUpdateAvailable = true;
927
928 // Signal our end of the sync point and then dispose of it
929 mRemoteSyncPoints.front()->setTransactionApplied();
930 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800931 } else {
932 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700933 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700935 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 stateUpdateAvailable = true;
937 }
938 }
939
940 // If we still have pending updates, wake SurfaceFlinger back up and point
941 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700943 setTransactionFlags(eTransactionNeeded);
944 mFlinger->setTransactionFlags(eTraversalNeeded);
945 }
946
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800947 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700948 return stateUpdateAvailable;
949}
950
Marissa Wall61c58622018-07-18 10:12:20 -0700951uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000952 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953
Marissa Wall61c58622018-07-18 10:12:20 -0700954 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
955 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700956
David Sodmaneb085e02017-10-05 18:49:04 -0700957 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700958 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000959 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800960 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
961 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
962 " requested={ wh={%4u,%4u} }}\n"
963 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
964 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700965 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700966 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
967 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
968 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
969 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
970 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700971 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
972 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
973 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700975
Robert Carre392b552017-09-19 12:16:05 -0700976 // Don't let Layer::doTransaction update the drawing state
977 // if we have a pending resize, unless we are in fixed-size mode.
978 // the drawing state will be updated only once we receive a buffer
979 // with the correct size.
980 //
981 // In particular, we want to make sure the clip (which is part
982 // of the geometry state) is latched together with the size but is
983 // latched immediately when no resizing is involved.
984 //
985 // If a sideband stream is attached, however, we want to skip this
986 // optimization so that transactions aren't missed when a buffer
987 // never arrives
988 //
989 // In the case that we don't have a buffer we ignore other factors
990 // and avoid entering the resizePending state. At a high level the
991 // resizePending state is to avoid applying the state of the new buffer
992 // to the old buffer. However in the state where we don't have an old buffer
993 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700994 const bool resizePending =
995 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
996 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800997 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700998 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800999 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001000 flags |= eDontUpdateGeometryState;
1001 }
1002 }
1003
Robert Carr7bf247e2017-05-18 14:02:49 -07001004 // Here we apply various requested geometry states, depending on our
1005 // latching configuration. See Layer.h for a detailed discussion of
1006 // how geometry latching is controlled.
1007 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001008 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001009
1010 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1011 // mode, which causes attributes which normally latch regardless of scaling mode,
1012 // to be delayed. We copy the requested state to the active state making sure
1013 // to respect these rules (again see Layer.h for a detailed discussion).
1014 //
1015 // There is an awkward asymmetry in the handling of the crop states in the position
1016 // states, as can be seen below. Largely this arises from position and transform
1017 // being stored in the same data structure while having different latching rules.
1018 // b/38182305
1019 //
Marissa Wall61c58622018-07-18 10:12:20 -07001020 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001021 // applyPendingStates in the presence of deferred transactions.
1022 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001023 float tx = stateToCommit->active_legacy.transform.tx();
1024 float ty = stateToCommit->active_legacy.transform.ty();
1025 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1026 stateToCommit->active_legacy.transform.set(tx, ty);
1027 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001028 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001029 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001030 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001031 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 }
1033
Marissa Wall61c58622018-07-18 10:12:20 -07001034 return flags;
1035}
1036
1037uint32_t Layer::doTransaction(uint32_t flags) {
1038 ATRACE_CALL();
1039
chaviw5aedec92018-10-22 10:40:38 -07001040 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001041 return flags;
1042 }
1043
1044 if (mChildrenChanged) {
1045 flags |= eVisibleRegion;
1046 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001047 }
1048
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001049 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001050 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001051 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001052 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001053 }
1054
1055 flags = doTransactionResize(flags, &c);
1056
Alec Mourib416efd2018-09-06 21:01:59 +00001057 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001058
1059 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001060 // invalidate and recompute the visible regions if needed
1061 flags |= Layer::eVisibleRegion;
1062 }
1063
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001064 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 // invalidate and recompute the visible regions if needed
1066 flags |= eVisibleRegion;
1067 this->contentDirty = true;
1068
1069 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001070 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001071 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 }
1073
Dan Stozac8145172016-04-28 16:29:06 -07001074 // If the layer is hidden, signal and clear out all local sync points so
1075 // that transactions for layers depending on this layer's frames becoming
1076 // visible are not blocked
1077 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001078 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001079 }
1080
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001081 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001082 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001084 }
1085
Mathias Agopian13127d82013-03-05 17:47:11 -08001086 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001087 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001088 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090}
1091
Pablo Ceballos05289c22016-04-14 15:49:55 -07001092void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001094}
1095
Mathias Agopian13127d82013-03-05 17:47:11 -08001096uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001097 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001098}
1099
1100uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001101 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001102}
1103
Robert Carr82364e32016-05-15 11:27:47 -07001104bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001105 if (mCurrentState.requested_legacy.transform.tx() == x &&
1106 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001107 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001109
1110 // We update the requested and active position simultaneously because
1111 // we want to apply the position portion of the transform matrix immediately,
1112 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001114 if (immediate && !mFreezeGeometryUpdates) {
1115 // Here we directly update the active state
1116 // unlike other setters, because we store it within
1117 // the transform, but use different latching rules.
1118 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001120 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001121 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001122
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001123 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 setTransactionFlags(eTransactionNeeded);
1125 return true;
1126}
Robert Carr82364e32016-05-15 11:27:47 -07001127
Robert Carr1f0a16a2016-10-24 16:27:39 -07001128bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1129 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1130 if (idx < 0) {
1131 return false;
1132 }
1133 if (childLayer->setLayer(z)) {
1134 mCurrentChildren.removeAt(idx);
1135 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001136 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001137 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001138 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001139}
1140
Robert Carr503c7042017-09-27 15:06:08 -07001141bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1142 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1143 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1144 if (idx < 0) {
1145 return false;
1146 }
1147 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1148 mCurrentChildren.removeAt(idx);
1149 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001150 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001151 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001152 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001153}
1154
Robert Carrae060832016-11-28 10:51:00 -08001155bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001156 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1157 mCurrentState.sequence++;
1158 mCurrentState.z = z;
1159 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001160
1161 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001162 if (mCurrentState.zOrderRelativeOf != nullptr) {
1163 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001164 if (strongRelative != nullptr) {
1165 strongRelative->removeZOrderRelative(this);
1166 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001167 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001168 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001169 setTransactionFlags(eTransactionNeeded);
1170 return true;
1171}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001172
Robert Carrdb66e622017-04-10 16:55:57 -07001173void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001174 mCurrentState.zOrderRelatives.remove(relative);
1175 mCurrentState.sequence++;
1176 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001177 setTransactionFlags(eTransactionNeeded);
1178}
1179
1180void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001181 mCurrentState.zOrderRelatives.add(relative);
1182 mCurrentState.modified = true;
1183 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001184 setTransactionFlags(eTransactionNeeded);
1185}
1186
Robert Carr503d2bd2017-12-04 15:49:47 -08001187bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001188 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1189 if (handle == nullptr) {
1190 return false;
1191 }
1192 sp<Layer> relative = handle->owner.promote();
1193 if (relative == nullptr) {
1194 return false;
1195 }
1196
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001197 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1198 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001199 return false;
1200 }
1201
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001202 mCurrentState.sequence++;
1203 mCurrentState.modified = true;
1204 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001205
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001206 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001207 if (oldZOrderRelativeOf != nullptr) {
1208 oldZOrderRelativeOf->removeZOrderRelative(this);
1209 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001210 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001211 relative->addZOrderRelative(this);
1212
1213 setTransactionFlags(eTransactionNeeded);
1214
1215 return true;
1216}
1217
Mathias Agopian13127d82013-03-05 17:47:11 -08001218bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001219 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001220 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001221 mCurrentState.requested_legacy.w = w;
1222 mCurrentState.requested_legacy.h = h;
1223 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001225
1226 // record the new size, from this point on, when the client request
1227 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001228 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 return true;
1230}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001231bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001232 if (mCurrentState.color.a == alpha) return false;
1233 mCurrentState.sequence++;
1234 mCurrentState.color.a = alpha;
1235 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 setTransactionFlags(eTransactionNeeded);
1237 return true;
1238}
chaviw13fdc492017-06-27 12:40:18 -07001239
Valerie Haudd0b7572019-01-29 14:59:27 -08001240bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1241 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001242 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001243 }
1244 mCurrentState.sequence++;
1245 mCurrentState.modified = true;
1246 setTransactionFlags(eTransactionNeeded);
1247
1248 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001249 // create background color layer if one does not yet exist
1250 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1251 const String8& name = mName + "BackgroundColorLayer";
1252 mCurrentState.bgColorLayer =
1253 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001254
Valerie Haudd0b7572019-01-29 14:59:27 -08001255 // add to child list
1256 addChild(mCurrentState.bgColorLayer);
1257 mFlinger->mLayersAdded = true;
1258 // set up SF to handle added color layer
1259 if (isRemovedFromCurrentState()) {
1260 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1261 }
1262 mFlinger->setTransactionFlags(eTransactionNeeded);
1263 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1264 mCurrentState.bgColorLayer->reparent(nullptr);
1265 mCurrentState.bgColorLayer = nullptr;
1266 return true;
1267 }
1268
1269 mCurrentState.bgColorLayer->setColor(color);
1270 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1271 mCurrentState.bgColorLayer->setAlpha(alpha);
1272 mCurrentState.bgColorLayer->setDataspace(dataspace);
1273
chaviw13fdc492017-06-27 12:40:18 -07001274 return true;
1275}
1276
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001277bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001278 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001279
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001280 mCurrentState.sequence++;
1281 mCurrentState.cornerRadius = cornerRadius;
1282 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001283 setTransactionFlags(eTransactionNeeded);
1284 return true;
1285}
1286
Robert Carrd4ae7f32018-06-07 16:10:57 -07001287bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1288 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001289 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001290 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1291
1292 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1293 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1294 return false;
1295 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001296 mCurrentState.sequence++;
1297 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1298 matrix.dsdy);
1299 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001300 setTransactionFlags(eTransactionNeeded);
1301 return true;
1302}
Marissa Wall61c58622018-07-18 10:12:20 -07001303
Mathias Agopian13127d82013-03-05 17:47:11 -08001304bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001305 mCurrentState.requestedTransparentRegion_legacy = transparent;
1306 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001307 setTransactionFlags(eTransactionNeeded);
1308 return true;
1309}
1310bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001311 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1312 if (mCurrentState.flags == newFlags) return false;
1313 mCurrentState.sequence++;
1314 mCurrentState.flags = newFlags;
1315 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001316 setTransactionFlags(eTransactionNeeded);
1317 return true;
1318}
Robert Carr99e27f02016-06-16 15:18:02 -07001319
Marissa Wallf58c14b2018-07-24 10:50:43 -07001320bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001321 if (mCurrentState.requestedCrop_legacy == crop) return false;
1322 mCurrentState.sequence++;
1323 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001324 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001325 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001326 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001327 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1328
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001329 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001330 setTransactionFlags(eTransactionNeeded);
1331 return true;
1332}
Robert Carr8d5227b2017-03-16 15:41:03 -07001333
Robert Carrc3574f72016-03-24 12:19:32 -07001334bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001335 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001336 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001337 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001338 return true;
1339}
1340
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001341bool Layer::setMetadata(LayerMetadata data) {
1342 bool changed = data.mMap != mCurrentState.metadata.mMap;
1343 if (!changed) return false;
1344 mCurrentState.metadata = std::move(data);
1345 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001346 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001347 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001348 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001349}
1350
Mathias Agopian13127d82013-03-05 17:47:11 -08001351bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001352 if (mCurrentState.layerStack == layerStack) return false;
1353 mCurrentState.sequence++;
1354 mCurrentState.layerStack = layerStack;
1355 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001356 setTransactionFlags(eTransactionNeeded);
1357 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001358}
1359
Robert Carr1f0a16a2016-10-24 16:27:39 -07001360uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001361 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001362 if (p == nullptr) {
1363 return getDrawingState().layerStack;
1364 }
1365 return p->getLayerStack();
1366}
1367
Marissa Wallf58c14b2018-07-24 10:50:43 -07001368void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001369 mCurrentState.barrierLayer_legacy = barrierLayer;
1370 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001371 // We don't set eTransactionNeeded, because just receiving a deferral
1372 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001373 mCurrentState.modified = true;
1374 pushPendingState();
1375 mCurrentState.barrierLayer_legacy = nullptr;
1376 mCurrentState.frameNumber_legacy = 0;
1377 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001378}
1379
Marissa Wallf58c14b2018-07-24 10:50:43 -07001380void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001381 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001382 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001383}
1384
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385// ----------------------------------------------------------------------------
1386// pageflip handling...
1387// ----------------------------------------------------------------------------
1388
Robert Carr1f0a16a2016-10-24 16:27:39 -07001389bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001390 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001391 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001392 if (parent != nullptr && parent->isHiddenByPolicy()) {
1393 return true;
1394 }
1395 return s.flags & layer_state_t::eLayerHidden;
1396}
1397
David Sodman41fdfc92017-11-06 16:09:56 -08001398uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001399 // TODO: should we do something special if mSecure is set?
1400 if (mProtectedByApp) {
1401 // need a hardware-protected path to external video sink
1402 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001403 }
Riley Andrews03414a12014-07-01 14:22:59 -07001404 if (mPotentialCursor) {
1405 usage |= GraphicBuffer::USAGE_CURSOR;
1406 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001407 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001408 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001409}
1410
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001411void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001412 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001413 // Disable setting transform hint if the debug flag is set or if the
1414 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1415 // in one orientation.
1416 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001417 // The transform hint is used to improve performance, but we can
1418 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001419 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001420 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001421 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001422 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001423 orientation = 0;
1424 }
1425 }
David Sodmaneb085e02017-10-05 18:49:04 -07001426 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001427}
1428
Mathias Agopian13127d82013-03-05 17:47:11 -08001429// ----------------------------------------------------------------------------
1430// debugging
1431// ----------------------------------------------------------------------------
1432
Marissa Wall61c58622018-07-18 10:12:20 -07001433// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001434LayerDebugInfo Layer::getLayerDebugInfo() const {
1435 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001436 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001437 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001438 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001439 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001440 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001441 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001442 info.mVisibleRegion = visibleRegion;
1443 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001444 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001445 info.mX = ds.active_legacy.transform.tx();
1446 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001447 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001448 info.mWidth = ds.active_legacy.w;
1449 info.mHeight = ds.active_legacy.h;
1450 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001451 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001452 info.mFlags = ds.flags;
1453 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001454 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001455 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1456 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1457 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1458 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001459 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001460 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001461 if (buffer != 0) {
1462 info.mActiveBufferWidth = buffer->getWidth();
1463 info.mActiveBufferHeight = buffer->getHeight();
1464 info.mActiveBufferStride = buffer->getStride();
1465 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001466 } else {
1467 info.mActiveBufferWidth = 0;
1468 info.mActiveBufferHeight = 0;
1469 info.mActiveBufferStride = 0;
1470 info.mActiveBufferFormat = 0;
1471 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001472 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001473 info.mNumQueuedFrames = getQueuedFrameCount();
1474 info.mRefreshPending = isBufferLatched();
1475 info.mIsOpaque = isOpaque(ds);
1476 info.mContentDirty = contentDirty;
1477 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001478}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001479
Yiwei Zhang5434a782018-12-05 18:06:32 -08001480void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001481 result.append("-------------------------------");
1482 result.append("-------------------------------");
1483 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001484 result.append(" Layer name\n");
1485 result.append(" Z | ");
1486 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001487 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001488 result.append(" Disp Frame (LTRB) | ");
1489 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001490 result.append("-------------------------------");
1491 result.append("-------------------------------");
1492 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001493}
1494
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001495void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1496 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1497 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001498 return;
1499 }
1500
Yiwei Zhang5434a782018-12-05 18:06:32 -08001501 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001502 if (mName.length() > 77) {
1503 std::string shortened;
1504 shortened.append(mName.string(), 36);
1505 shortened.append("[...]");
1506 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001507 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001508 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001509 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001510 }
1511
Yiwei Zhang5434a782018-12-05 18:06:32 -08001512 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001513
Alec Mourib416efd2018-09-06 21:01:59 +00001514 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001515 const auto& compositionState = outputLayer->getState();
1516
Chia-I Wu1e043612018-03-01 09:45:09 -08001517 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001518 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001519 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001520 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001521 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001522 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1523 StringAppendF(&result, "%10s | ",
1524 toString(getCompositionLayer() ? compositionState.bufferTransform
1525 : static_cast<Hwc2::Transform>(0))
1526 .c_str());
1527 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001528 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001529 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001530 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1531 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001532
Yichi Chen6ca35192018-05-29 12:20:43 +08001533 result.append("- - - - - - - - - - - - - - - -");
1534 result.append("- - - - - - - - - - - - - - - -");
1535 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001536}
Dan Stozae22aec72016-08-01 13:20:59 -07001537
Yiwei Zhang5434a782018-12-05 18:06:32 -08001538void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001539 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001540}
1541
Svetoslavd85084b2014-03-20 10:28:31 -07001542void Layer::clearFrameStats() {
1543 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001544}
1545
Jamie Gennis6547ff42013-07-16 20:12:42 -07001546void Layer::logFrameStats() {
1547 mFrameTracker.logAndResetStats(mName);
1548}
1549
Svetoslavd85084b2014-03-20 10:28:31 -07001550void Layer::getFrameStats(FrameStats* outStats) const {
1551 mFrameTracker.getStats(outStats);
1552}
1553
Yiwei Zhang5434a782018-12-05 18:06:32 -08001554void Layer::dumpFrameEvents(std::string& result) {
1555 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001556 Mutex::Autolock lock(mFrameEventHistoryMutex);
1557 mFrameEventHistory.checkFencesForCompletion();
1558 mFrameEventHistory.dump(result);
1559}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001560
Brian Anderson5ea5e592016-12-01 16:54:33 -08001561void Layer::onDisconnect() {
1562 Mutex::Autolock lock(mFrameEventHistoryMutex);
1563 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001564 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001565}
1566
Brian Anderson3890c392016-07-25 12:48:08 -07001567void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001568 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001569 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001570 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1571 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001572 }
1573
Brian Andersond6927fb2016-07-23 23:37:30 -07001574 Mutex::Autolock lock(mFrameEventHistoryMutex);
1575 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001576 // If there are any unsignaled fences in the aquire timeline at this
1577 // point, the previously queued frame hasn't been latched yet. Go ahead
1578 // and try to get the signal time here so the syscall is taken out of
1579 // the main thread's critical path.
1580 mAcquireTimeline.updateSignalTimes();
1581 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001582 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001583 mFrameEventHistory.addQueue(*newTimestamps);
1584 }
1585
Brian Anderson3890c392016-07-25 12:48:08 -07001586 if (outDelta) {
1587 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001588 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001589}
Dan Stozae77c7662016-05-13 11:37:28 -07001590
Chia-I Wu98f1c102017-05-30 14:54:08 -07001591size_t Layer::getChildrenCount() const {
1592 size_t count = 0;
1593 for (const sp<Layer>& child : mCurrentChildren) {
1594 count += 1 + child->getChildrenCount();
1595 }
1596 return count;
1597}
1598
Robert Carr1f0a16a2016-10-24 16:27:39 -07001599void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001600 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001601 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001602
Robert Carr1f0a16a2016-10-24 16:27:39 -07001603 mCurrentChildren.add(layer);
1604 layer->setParent(this);
1605}
1606
1607ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001608 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001609 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001610
Robert Carr1323c952019-01-28 18:13:27 -08001611 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001612 return mCurrentChildren.remove(layer);
1613}
1614
Robert Carr1db73f62016-12-21 12:58:51 -08001615bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1616 sp<Handle> handle = nullptr;
1617 sp<Layer> newParent = nullptr;
1618 if (newParentHandle == nullptr) {
1619 return false;
1620 }
1621 handle = static_cast<Handle*>(newParentHandle.get());
1622 newParent = handle->owner.promote();
1623 if (newParent == nullptr) {
1624 ALOGE("Unable to promote Layer handle");
1625 return false;
1626 }
1627
chaviw5aedec92018-10-22 10:40:38 -07001628 if (attachChildren()) {
1629 setTransactionFlags(eTransactionNeeded);
1630 }
Robert Carr1db73f62016-12-21 12:58:51 -08001631 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001632 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001633 }
1634 mCurrentChildren.clear();
1635
1636 return true;
1637}
1638
Robert Carr15eae092018-03-23 13:43:53 -07001639void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001640 for (const sp<Layer>& child : mDrawingChildren) {
1641 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001642 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001643 }
1644}
1645
chaviwf1961f72017-09-18 16:41:07 -07001646bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001647 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001648
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001649 // While layers are detached, we allow most operations
1650 // and simply halt performing the actual transaction. However
1651 // for reparent != null we would enter the mRemovedFromCurrentState
1652 // state, regardless of whether doTransaction was called, and
1653 // so we need to prevent the update here.
1654 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001655 return false;
1656 }
1657
Robert Carr54cf5b12019-01-25 14:02:28 -08001658 sp<Layer> newParent;
1659 if (newParentHandle != nullptr) {
1660 auto handle = static_cast<Handle*>(newParentHandle.get());
1661 newParent = handle->owner.promote();
1662 if (newParent == nullptr) {
1663 ALOGE("Unable to promote Layer handle");
1664 return false;
1665 }
1666 if (newParent == this) {
1667 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1668 return false;
1669 }
1670 }
1671
chaviwf1961f72017-09-18 16:41:07 -07001672 sp<Layer> parent = getParent();
1673 if (parent != nullptr) {
1674 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001675 }
1676
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001677 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001678 newParent->addChild(this);
1679 if (!newParent->isRemovedFromCurrentState()) {
1680 addToCurrentState();
1681 } else {
1682 onRemovedFromCurrentState();
1683 }
1684
1685 if (mLayerDetached) {
1686 mLayerDetached = false;
1687 callSetTransactionFlags = true;
1688 }
1689 } else {
1690 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001691 }
1692
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001693 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001694 setTransactionFlags(eTransactionNeeded);
1695 }
chaviw06178942017-07-27 10:25:59 -07001696 return true;
1697}
1698
Robert Carr9524cb32017-02-13 11:32:32 -08001699bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001700 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001701 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001702 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001703 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001704 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001705 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001706 }
Robert Carr7f619b22017-11-06 12:56:35 -08001707 }
Robert Carr9524cb32017-02-13 11:32:32 -08001708
1709 return true;
1710}
1711
chaviw5aedec92018-10-22 10:40:38 -07001712bool Layer::attachChildren() {
1713 bool changed = false;
1714 for (const sp<Layer>& child : mCurrentChildren) {
1715 sp<Client> parentClient = mClientRef.promote();
1716 sp<Client> client(child->mClientRef.promote());
1717 if (client != nullptr && parentClient != client) {
1718 if (child->mLayerDetached) {
1719 child->mLayerDetached = false;
1720 changed = true;
1721 }
1722 changed |= child->attachChildren();
1723 }
1724 }
1725
1726 return changed;
1727}
1728
Peiyong Lind3788632018-09-18 16:01:31 -07001729bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001730 static const mat4 identityMatrix = mat4();
1731
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001732 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001733 return false;
1734 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001735 ++mCurrentState.sequence;
1736 mCurrentState.colorTransform = matrix;
1737 mCurrentState.hasColorTransform = matrix != identityMatrix;
1738 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001739 setTransactionFlags(eTransactionNeeded);
1740 return true;
1741}
1742
chaviwf66724d2018-11-28 16:35:21 -08001743mat4 Layer::getColorTransform() const {
1744 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1745 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1746 colorTransform = parent->getColorTransform() * colorTransform;
1747 }
1748 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001749}
1750
1751bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001752 bool hasColorTransform = getDrawingState().hasColorTransform;
1753 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1754 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1755 }
1756 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001757}
1758
Chia-I Wu11481472018-05-04 10:43:19 -07001759bool Layer::isLegacyDataSpace() const {
1760 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001761 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001762 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001763}
1764
Robert Carr1f0a16a2016-10-24 16:27:39 -07001765void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001766 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001767}
1768
1769void Layer::clearSyncPoints() {
1770 for (const auto& child : mCurrentChildren) {
1771 child->clearSyncPoints();
1772 }
1773
1774 Mutex::Autolock lock(mLocalSyncPointMutex);
1775 for (auto& point : mLocalSyncPoints) {
1776 point->setFrameAvailable();
1777 }
1778 mLocalSyncPoints.clear();
1779}
1780
1781int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001782 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001783}
1784
Robert Carr29abff82017-12-04 13:51:20 -08001785bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1786 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001787 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001788 return state.zOrderRelativeOf != nullptr;
1789}
1790
David Sodman41fdfc92017-11-06 16:09:56 -08001791__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001792 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001793 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1794 "makeTraversalList received invalid stateSet");
1795 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1796 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001797 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001798
Robert Carr29abff82017-12-04 13:51:20 -08001799 if (state.zOrderRelatives.size() == 0) {
1800 *outSkipRelativeZUsers = true;
1801 return children;
1802 }
1803
chaviwfd462612018-05-31 16:11:27 -07001804 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001805 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001806 sp<Layer> strongRelative = weakRelative.promote();
1807 if (strongRelative != nullptr) {
1808 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001809 }
1810 }
1811
Dan Stoza412903f2017-04-27 13:42:17 -07001812 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001813 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001814 if (childState.zOrderRelativeOf != nullptr) {
1815 continue;
1816 }
Robert Carrdb66e622017-04-10 16:55:57 -07001817 traverse.add(child);
1818 }
1819
1820 return traverse;
1821}
1822
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823/**
Robert Carrdb66e622017-04-10 16:55:57 -07001824 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001825 */
Dan Stoza412903f2017-04-27 13:42:17 -07001826void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001827 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1828 // produce a new list for traversing, including our relatives, and not including our children
1829 // who are relatives of another surface. In the case that there are no relative Z,
1830 // makeTraversalList returns our children directly to avoid significant overhead.
1831 // However in this case we need to take the responsibility for filtering children which
1832 // are relatives of another surface here.
1833 bool skipRelativeZUsers = false;
1834 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001835
Robert Carr1f0a16a2016-10-24 16:27:39 -07001836 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001837 for (; i < list.size(); i++) {
1838 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001839 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1840 continue;
1841 }
1842
Robert Carrdb66e622017-04-10 16:55:57 -07001843 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001845 }
Dan Stoza412903f2017-04-27 13:42:17 -07001846 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001847 }
Robert Carr29abff82017-12-04 13:51:20 -08001848
Dan Stoza412903f2017-04-27 13:42:17 -07001849 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001850 for (; i < list.size(); i++) {
1851 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001852
Robert Carr29abff82017-12-04 13:51:20 -08001853 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1854 continue;
1855 }
Dan Stoza412903f2017-04-27 13:42:17 -07001856 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001857 }
1858}
1859
1860/**
Robert Carrdb66e622017-04-10 16:55:57 -07001861 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001862 */
Dan Stoza412903f2017-04-27 13:42:17 -07001863void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1864 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001865 // See traverseInZOrder for documentation.
1866 bool skipRelativeZUsers = false;
1867 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001868
Robert Carr1f0a16a2016-10-24 16:27:39 -07001869 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001870 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001871 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001872
1873 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1874 continue;
1875 }
1876
Robert Carrdb66e622017-04-10 16:55:57 -07001877 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001878 break;
1879 }
Dan Stoza412903f2017-04-27 13:42:17 -07001880 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001881 }
Dan Stoza412903f2017-04-27 13:42:17 -07001882 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001883 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001884 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001885
1886 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1887 continue;
1888 }
1889
Dan Stoza412903f2017-04-27 13:42:17 -07001890 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001891 }
1892}
1893
chaviw4b129c22018-04-09 16:19:43 -07001894LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1895 const std::vector<Layer*>& layersInTree) {
1896 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1897 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001898 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1899 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001900 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001901
chaviwfd462612018-05-31 16:11:27 -07001902 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001903 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1904 sp<Layer> strongRelative = weakRelative.promote();
1905 // Only add relative layers that are also descendents of the top most parent of the tree.
1906 // If a relative layer is not a descendent, then it should be ignored.
1907 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1908 traverse.add(strongRelative);
1909 }
1910 }
1911
1912 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001913 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001914 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1915 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1916 // the child here since it won't be added later as a relative.
1917 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1918 childState.zOrderRelativeOf.promote().get())) {
1919 continue;
1920 }
1921 traverse.add(child);
1922 }
1923
1924 return traverse;
1925}
1926
1927void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1928 LayerVector::StateSet stateSet,
1929 const LayerVector::Visitor& visitor) {
1930 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001931
1932 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001933 for (; i < list.size(); i++) {
1934 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001935 if (relative->getZ() >= 0) {
1936 break;
1937 }
chaviw4b129c22018-04-09 16:19:43 -07001938 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001939 }
chaviw4b129c22018-04-09 16:19:43 -07001940
chaviwa76b2712017-09-20 12:02:26 -07001941 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001942 for (; i < list.size(); i++) {
1943 const auto& relative = list[i];
1944 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001945 }
1946}
1947
chaviw4b129c22018-04-09 16:19:43 -07001948std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1949 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1950 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1951
1952 std::vector<Layer*> layersInTree = {this};
1953 for (size_t i = 0; i < children.size(); i++) {
1954 const auto& child = children[i];
1955 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1956 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1957 }
1958
1959 return layersInTree;
1960}
1961
1962void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1963 const LayerVector::Visitor& visitor) {
1964 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1965 std::sort(layersInTree.begin(), layersInTree.end());
1966 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1967}
1968
Peiyong Linefefaac2018-08-17 12:27:51 -07001969ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001970 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001971}
1972
chaviw13fdc492017-06-27 12:40:18 -07001973half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001974 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001975
chaviw13fdc492017-06-27 12:40:18 -07001976 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1977 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001978}
Robert Carr6452f122017-03-21 10:41:29 -07001979
chaviw13fdc492017-06-27 12:40:18 -07001980half4 Layer::getColor() const {
1981 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001982 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001983}
Robert Carr6452f122017-03-21 10:41:29 -07001984
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001985Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1986 const auto& p = mDrawingParent.promote();
1987 if (p != nullptr) {
1988 RoundedCornerState parentState = p->getRoundedCornerState();
1989 if (parentState.radius > 0) {
1990 ui::Transform t = getActiveTransform(getDrawingState());
1991 t = t.inverse();
1992 parentState.cropRect = t.transform(parentState.cropRect);
1993 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1994 // but a transform matrix can define horizontal and vertical scales.
1995 // Let's take the average between both of them and pass into the shader, practically we
1996 // never do this type of transformation on windows anyway.
1997 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1998 return parentState;
1999 }
2000 }
2001 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002002 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002003}
2004
Robert Carr1f0a16a2016-10-24 16:27:39 -07002005void Layer::commitChildList() {
2006 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2007 const auto& child = mCurrentChildren[i];
2008 child->commitChildList();
2009 }
2010 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002011 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002012}
2013
Robert Carr720e5062018-07-30 17:45:14 -07002014void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002015 mCurrentState.inputInfo = info;
2016 mCurrentState.modified = true;
2017 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002018 setTransactionFlags(eTransactionNeeded);
2019}
2020
chaviw1d044282017-09-27 12:19:28 -07002021void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2022 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2023 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002024 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002025
Peiyong Linefefaac2018-08-17 12:27:51 -07002026 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002027 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002028
2029 layerInfo->set_id(sequence);
2030 layerInfo->set_name(getName().c_str());
2031 layerInfo->set_type(String8(getTypeId()));
2032
2033 for (const auto& child : children) {
2034 layerInfo->add_children(child->sequence);
2035 }
2036
2037 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2038 sp<Layer> strongRelative = weakRelative.promote();
2039 if (strongRelative != nullptr) {
2040 layerInfo->add_relatives(strongRelative->sequence);
2041 }
2042 }
2043
Marissa Wallf58c14b2018-07-24 10:50:43 -07002044 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08002045 [&]() { return layerInfo->mutable_transparent_region(); });
2046 LayerProtoHelper::writeToProto(visibleRegion,
2047 [&]() { return layerInfo->mutable_visible_region(); });
2048 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2049 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07002050
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002051 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002052 layerInfo->set_z(state.z);
2053
Nataniel Borges797b0e42019-02-15 14:11:58 -08002054 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2055 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07002056
Nataniel Borges797b0e42019-02-15 14:11:58 -08002057 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2058 return layerInfo->mutable_requested_position();
2059 });
chaviw1d044282017-09-27 12:19:28 -07002060
Nataniel Borges797b0e42019-02-15 14:11:58 -08002061 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2062 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07002063
Nataniel Borges797b0e42019-02-15 14:11:58 -08002064 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002065 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002066
2067 layerInfo->set_is_opaque(isOpaque(state));
2068 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002069
2070 // XXX (b/79210409) mCurrentDataSpace is not protected
2071 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2072
chaviw1d044282017-09-27 12:19:28 -07002073 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08002074 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2075 LayerProtoHelper::writeToProto(state.color,
2076 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07002077 layerInfo->set_flags(state.flags);
2078
2079 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2080 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2081
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002082 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002083 if (parent != nullptr) {
2084 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002085 } else {
2086 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002087 }
2088
2089 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2090 if (zOrderRelativeOf != nullptr) {
2091 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002092 } else {
2093 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002094 }
2095
Lloyd Pique0b785d82018-12-04 17:25:27 -08002096 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002097 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08002098 LayerProtoHelper::writeToProto(buffer,
2099 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07002100 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002101 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002102 }
2103
2104 layerInfo->set_queued_frames(getQueuedFrameCount());
2105 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002106 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002107 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002108
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002109 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002110 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002111 if (barrierLayer != nullptr) {
2112 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2113 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002114 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002115 }
2116 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002117
2118 auto protoMap = layerInfo->mutable_metadata();
2119 for (const auto& entry : state.metadata.mMap) {
2120 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2121 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002122 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08002123 LayerProtoHelper::writeToProto(mSourceBounds,
2124 [&]() { return layerInfo->mutable_source_bounds(); });
2125 LayerProtoHelper::writeToProto(mScreenBounds,
2126 [&]() { return layerInfo->mutable_screen_bounds(); });
2127 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07002128}
2129
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002130void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2131 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2132 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002133 return;
2134 }
2135
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002136 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2137
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002138 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002139
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002140 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002141 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002142
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002143 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002144 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002145
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002146 const int32_t transform =
2147 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002148 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002149
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002150 const int32_t compositionType =
2151 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2152 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002153 layerInfo->set_hwc_composition_type(compositionType);
2154
2155 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2156 static_cast<BufferLayer*>(this)->isProtected()) {
2157 layerInfo->set_is_protected(true);
2158 } else {
2159 layerInfo->set_is_protected(false);
2160 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002161}
2162
Robert Carr2e102c92018-10-23 12:11:15 -07002163bool Layer::isRemovedFromCurrentState() const {
2164 return mRemovedFromCurrentState;
2165}
2166
Arthur Hungd20b2702019-01-14 18:16:16 +08002167InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002168 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002169
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002170 if (info.displayId == ADISPLAY_ID_NONE) {
2171 info.displayId = mDrawingState.layerStack;
2172 }
2173
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002174 ui::Transform t = getTransform();
2175 const float xScale = t.sx();
2176 const float yScale = t.sy();
2177 if (xScale != 1.0f || yScale != 1.0f) {
2178 info.windowXScale *= 1.0f / xScale;
2179 info.windowYScale *= 1.0f / yScale;
2180 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002181 }
Robert Carre07e1032018-11-26 12:55:53 -08002182
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002183 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002184 // If this is a portal window, set the touchableRegion to the layerBounds.
2185 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2186 ? getBufferSize(getDrawingState())
2187 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002188 if (!layerBounds.isValid()) {
2189 layerBounds = getCroppedBufferSize(getDrawingState());
2190 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002191 layerBounds = t.transform(layerBounds);
2192 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2193
Arthur Hungd20b2702019-01-14 18:16:16 +08002194 // Input coordinate should match the layer bounds.
2195 info.frameLeft = layerBounds.left;
2196 info.frameTop = layerBounds.top;
2197 info.frameRight = layerBounds.right;
2198 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002199
2200 // Position the touchable region relative to frame screen location and restrict it to frame
2201 // bounds.
2202 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002203 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002204 return info;
2205}
2206
2207bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002208 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002209}
2210
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002211std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2212 return nullptr;
2213}
2214
chaviw3e727cd2019-01-31 13:41:05 -08002215bool Layer::canReceiveInput() const {
2216 return isVisible();
2217}
2218
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002219compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2220 const sp<const DisplayDevice>& display) const {
2221 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2222}
2223
Mathias Agopian13127d82013-03-05 17:47:11 -08002224// ---------------------------------------------------------------------------
2225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002226}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002227
2228#if defined(__gl_h_)
2229#error "don't include gl/gl.h in this file"
2230#endif
2231
2232#if defined(__gl2_h_)
2233#error "don't include gl2/gl2.h in this file"
2234#endif