blob: 4ac7a90cd30b8cd51987458672e9a5f3bb0e2813 [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,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800686 Region& clearRegion, const bool supportProtectedContent,
687 renderengine::LayerSettings& layer) {
688 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800689}
690
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000691bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800692 Region& clearRegion, const bool supportProtectedContent,
693 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000694 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800695 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000696}
697
698bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
699 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800700 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000701 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800702 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000703 half alpha = getAlpha();
704 layer.geometry.boundaries = bounds;
705 if (useIdentityTransform) {
706 layer.geometry.positionTransform = mat4();
707 } else {
708 const ui::Transform transform = getTransform();
709 mat4 m;
710 m[0][0] = transform[0][0];
711 m[0][1] = transform[0][1];
712 m[0][3] = transform[0][2];
713 m[1][0] = transform[1][0];
714 m[1][1] = transform[1][1];
715 m[1][3] = transform[1][2];
716 m[3][0] = transform[2][0];
717 m[3][1] = transform[2][1];
718 m[3][3] = transform[2][2];
719 layer.geometry.positionTransform = m;
720 }
721
722 if (hasColorTransform()) {
723 layer.colorTransform = getColorTransform();
724 }
725
726 const auto roundedCornerState = getRoundedCornerState();
727 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
728 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
729
730 layer.alpha = alpha;
731 layer.sourceDataspace = mCurrentDataSpace;
732 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800733}
734
David Sodman41fdfc92017-11-06 16:09:56 -0800735void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
736 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800737 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800738 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700739 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800740 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800741}
742
David Sodmanc1498e62018-09-12 14:36:26 -0700743void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
744 clearWithOpenGL(renderArea, 0, 0, 0, 0);
745}
746
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800747void Layer::setCompositionType(const sp<const DisplayDevice>& display,
748 Hwc2::IComposerClient::Composition type) {
749 const auto outputLayer = findOutputLayerForDisplay(display);
750 LOG_FATAL_IF(!outputLayer);
751 LOG_FATAL_IF(!outputLayer->getState().hwc);
752 auto& compositionState = outputLayer->editState();
753
754 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
755 toString(type).c_str(), 1);
756 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700757 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800758 (*compositionState.hwc).hwcCompositionType = type;
759
760 auto error = (*compositionState.hwc)
761 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
762 ALOGE_IF(error != HWC2::Error::None,
763 "[%s] Failed to set "
764 "composition type %s: %s (%d)",
765 mName.string(), toString(type).c_str(), to_string(error).c_str(),
766 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767 }
768}
769
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800770Hwc2::IComposerClient::Composition Layer::getCompositionType(
771 const sp<const DisplayDevice>& display) const {
772 const auto outputLayer = findOutputLayerForDisplay(display);
773 LOG_FATAL_IF(!outputLayer);
774 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
775 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776}
777
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800778bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
779 const auto outputLayer = findOutputLayerForDisplay(display);
780 LOG_FATAL_IF(!outputLayer);
781 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800783
Dan Stozacac35382016-01-27 12:21:06 -0800784bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
785 if (point->getFrameNumber() <= mCurrentFrameNumber) {
786 // Don't bother with a SyncPoint, since we've already latched the
787 // relevant frame
788 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700789 }
Robert Carr2e102c92018-10-23 12:11:15 -0700790 if (isRemovedFromCurrentState()) {
791 return false;
792 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700793
Dan Stozacac35382016-01-27 12:21:06 -0800794 Mutex::Autolock lock(mLocalSyncPointMutex);
795 mLocalSyncPoints.push_back(point);
796 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700797}
798
Mathias Agopian13127d82013-03-05 17:47:11 -0800799// ----------------------------------------------------------------------------
800// local state
801// ----------------------------------------------------------------------------
802
Peiyong Lin833074a2018-08-28 11:53:54 -0700803void Layer::computeGeometry(const RenderArea& renderArea,
804 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700805 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700806 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800807 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700808
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000809 vec2 lt = vec2(win.left, win.top);
810 vec2 lb = vec2(win.left, win.bottom);
811 vec2 rb = vec2(win.right, win.bottom);
812 vec2 rt = vec2(win.right, win.top);
813
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800814 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000815 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700816 lt = layerTransform.transform(lt);
817 lb = layerTransform.transform(lb);
818 rb = layerTransform.transform(rb);
819 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000820 }
821
Peiyong Lin833074a2018-08-28 11:53:54 -0700822 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700823 position[0] = renderAreaTransform.transform(lt);
824 position[1] = renderAreaTransform.transform(lb);
825 position[2] = renderAreaTransform.transform(rb);
826 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800827}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800828
David Sodman41fdfc92017-11-06 16:09:56 -0800829bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800830 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700831 return (s.flags & layer_state_t::eLayerSecure);
832}
833
Mathias Agopian13127d82013-03-05 17:47:11 -0800834void Layer::setVisibleRegion(const Region& visibleRegion) {
835 // always called from main thread
836 this->visibleRegion = visibleRegion;
837}
838
839void Layer::setCoveredRegion(const Region& coveredRegion) {
840 // always called from main thread
841 this->coveredRegion = coveredRegion;
842}
843
David Sodman41fdfc92017-11-06 16:09:56 -0800844void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800845 // always called from main thread
846 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
847}
848
Robert Carre5f4f692018-01-12 13:12:28 -0800849void Layer::clearVisibilityRegions() {
850 visibleRegion.clear();
851 visibleNonTransparentRegion.clear();
852 coveredRegion.clear();
853}
854
Mathias Agopian13127d82013-03-05 17:47:11 -0800855// ----------------------------------------------------------------------------
856// transaction
857// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800858
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800859void Layer::pushPendingState() {
860 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700861 return;
862 }
863
Dan Stoza7dde5992015-05-22 09:51:44 -0700864 // If this transaction is waiting on the receipt of a frame, generate a sync
865 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700866 // We don't allow installing sync points after we are removed from the current state
867 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800868 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
869 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800870 if (barrierLayer == nullptr) {
871 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700872 // If we can't promote the layer we are intended to wait on,
873 // then it is expired or otherwise invalid. Allow this transaction
874 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800875 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800876 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800878 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800879 mRemoteSyncPoints.push_back(std::move(syncPoint));
880 } else {
881 // We already missed the frame we're supposed to synchronize
882 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800883 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800884 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700885 }
886
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 // Wake us up to check if the frame has been received
888 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700889 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700890 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 mPendingStates.push_back(mCurrentState);
892 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700893}
894
Pablo Ceballos05289c22016-04-14 15:49:55 -0700895void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700897
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 mPendingStates.removeAt(0);
899 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700900}
901
Pablo Ceballos05289c22016-04-14 15:49:55 -0700902bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700903 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800904 while (!mPendingStates.empty()) {
905 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700906 if (mRemoteSyncPoints.empty()) {
907 // If we don't have a sync point for this, apply it anyway. It
908 // will be visually wrong, but it should keep us from getting
909 // into too much trouble.
910 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700911 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700912 stateUpdateAvailable = true;
913 continue;
914 }
915
Marissa Wallf58c14b2018-07-24 10:50:43 -0700916 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800917 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800918 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800919
920 // Signal our end of the sync point and then dispose of it
921 mRemoteSyncPoints.front()->setTransactionApplied();
922 mRemoteSyncPoints.pop_front();
923 continue;
924 }
925
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
927 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700928 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700929 stateUpdateAvailable = true;
930
931 // Signal our end of the sync point and then dispose of it
932 mRemoteSyncPoints.front()->setTransactionApplied();
933 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800934 } else {
935 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700937 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700938 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 stateUpdateAvailable = true;
940 }
941 }
942
943 // If we still have pending updates, wake SurfaceFlinger back up and point
944 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800945 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700946 setTransactionFlags(eTransactionNeeded);
947 mFlinger->setTransactionFlags(eTraversalNeeded);
948 }
949
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800950 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700951 return stateUpdateAvailable;
952}
953
Marissa Wall61c58622018-07-18 10:12:20 -0700954uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000955 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956
Marissa Wall61c58622018-07-18 10:12:20 -0700957 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
958 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700959
David Sodmaneb085e02017-10-05 18:49:04 -0700960 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700961 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000962 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800963 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
964 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
965 " requested={ wh={%4u,%4u} }}\n"
966 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
967 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700968 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700969 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
970 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
971 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
972 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
973 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700974 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
975 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
976 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700978
Robert Carre392b552017-09-19 12:16:05 -0700979 // Don't let Layer::doTransaction update the drawing state
980 // if we have a pending resize, unless we are in fixed-size mode.
981 // the drawing state will be updated only once we receive a buffer
982 // with the correct size.
983 //
984 // In particular, we want to make sure the clip (which is part
985 // of the geometry state) is latched together with the size but is
986 // latched immediately when no resizing is involved.
987 //
988 // If a sideband stream is attached, however, we want to skip this
989 // optimization so that transactions aren't missed when a buffer
990 // never arrives
991 //
992 // In the case that we don't have a buffer we ignore other factors
993 // and avoid entering the resizePending state. At a high level the
994 // resizePending state is to avoid applying the state of the new buffer
995 // to the old buffer. However in the state where we don't have an old buffer
996 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700997 const bool resizePending =
998 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
999 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -08001000 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001001 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -08001002 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001003 flags |= eDontUpdateGeometryState;
1004 }
1005 }
1006
Robert Carr7bf247e2017-05-18 14:02:49 -07001007 // Here we apply various requested geometry states, depending on our
1008 // latching configuration. See Layer.h for a detailed discussion of
1009 // how geometry latching is controlled.
1010 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001011 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001012
1013 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1014 // mode, which causes attributes which normally latch regardless of scaling mode,
1015 // to be delayed. We copy the requested state to the active state making sure
1016 // to respect these rules (again see Layer.h for a detailed discussion).
1017 //
1018 // There is an awkward asymmetry in the handling of the crop states in the position
1019 // states, as can be seen below. Largely this arises from position and transform
1020 // being stored in the same data structure while having different latching rules.
1021 // b/38182305
1022 //
Marissa Wall61c58622018-07-18 10:12:20 -07001023 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001024 // applyPendingStates in the presence of deferred transactions.
1025 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001026 float tx = stateToCommit->active_legacy.transform.tx();
1027 float ty = stateToCommit->active_legacy.transform.ty();
1028 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1029 stateToCommit->active_legacy.transform.set(tx, ty);
1030 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001031 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001032 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001033 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001034 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001035 }
1036
Marissa Wall61c58622018-07-18 10:12:20 -07001037 return flags;
1038}
1039
1040uint32_t Layer::doTransaction(uint32_t flags) {
1041 ATRACE_CALL();
1042
chaviw5aedec92018-10-22 10:40:38 -07001043 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001044 return flags;
1045 }
1046
1047 if (mChildrenChanged) {
1048 flags |= eVisibleRegion;
1049 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001050 }
1051
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001052 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001053 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001054 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001055 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001056 }
1057
1058 flags = doTransactionResize(flags, &c);
1059
Alec Mourib416efd2018-09-06 21:01:59 +00001060 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001061
1062 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 // invalidate and recompute the visible regions if needed
1064 flags |= Layer::eVisibleRegion;
1065 }
1066
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001067 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001068 // invalidate and recompute the visible regions if needed
1069 flags |= eVisibleRegion;
1070 this->contentDirty = true;
1071
1072 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001073 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001074 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 }
1076
Dan Stozac8145172016-04-28 16:29:06 -07001077 // If the layer is hidden, signal and clear out all local sync points so
1078 // that transactions for layers depending on this layer's frames becoming
1079 // visible are not blocked
1080 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001081 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001082 }
1083
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001084 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001085 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001087 }
1088
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001090 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001091 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001092 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093}
1094
Pablo Ceballos05289c22016-04-14 15:49:55 -07001095void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001096 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001097}
1098
Mathias Agopian13127d82013-03-05 17:47:11 -08001099uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001100 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001101}
1102
1103uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001104 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001105}
1106
Robert Carr82364e32016-05-15 11:27:47 -07001107bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 if (mCurrentState.requested_legacy.transform.tx() == x &&
1109 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001110 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001111 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001112
1113 // We update the requested and active position simultaneously because
1114 // we want to apply the position portion of the transform matrix immediately,
1115 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001117 if (immediate && !mFreezeGeometryUpdates) {
1118 // Here we directly update the active state
1119 // unlike other setters, because we store it within
1120 // the transform, but use different latching rules.
1121 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001122 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001123 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001124 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001125
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001126 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001127 setTransactionFlags(eTransactionNeeded);
1128 return true;
1129}
Robert Carr82364e32016-05-15 11:27:47 -07001130
Robert Carr1f0a16a2016-10-24 16:27:39 -07001131bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1132 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1133 if (idx < 0) {
1134 return false;
1135 }
1136 if (childLayer->setLayer(z)) {
1137 mCurrentChildren.removeAt(idx);
1138 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001139 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001140 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001141 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001142}
1143
Robert Carr503c7042017-09-27 15:06:08 -07001144bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1145 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1146 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1147 if (idx < 0) {
1148 return false;
1149 }
1150 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1151 mCurrentChildren.removeAt(idx);
1152 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001153 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001154 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001155 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001156}
1157
Robert Carrae060832016-11-28 10:51:00 -08001158bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001159 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1160 mCurrentState.sequence++;
1161 mCurrentState.z = z;
1162 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001163
1164 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001165 if (mCurrentState.zOrderRelativeOf != nullptr) {
1166 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001167 if (strongRelative != nullptr) {
1168 strongRelative->removeZOrderRelative(this);
1169 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001170 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001171 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001172 setTransactionFlags(eTransactionNeeded);
1173 return true;
1174}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001175
Robert Carrdb66e622017-04-10 16:55:57 -07001176void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001177 mCurrentState.zOrderRelatives.remove(relative);
1178 mCurrentState.sequence++;
1179 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001180 setTransactionFlags(eTransactionNeeded);
1181}
1182
1183void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001184 mCurrentState.zOrderRelatives.add(relative);
1185 mCurrentState.modified = true;
1186 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001187 setTransactionFlags(eTransactionNeeded);
1188}
1189
Robert Carr503d2bd2017-12-04 15:49:47 -08001190bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001191 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1192 if (handle == nullptr) {
1193 return false;
1194 }
1195 sp<Layer> relative = handle->owner.promote();
1196 if (relative == nullptr) {
1197 return false;
1198 }
1199
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001200 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1201 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001202 return false;
1203 }
1204
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001205 mCurrentState.sequence++;
1206 mCurrentState.modified = true;
1207 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001208
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001209 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001210 if (oldZOrderRelativeOf != nullptr) {
1211 oldZOrderRelativeOf->removeZOrderRelative(this);
1212 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001213 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001214 relative->addZOrderRelative(this);
1215
1216 setTransactionFlags(eTransactionNeeded);
1217
1218 return true;
1219}
1220
Mathias Agopian13127d82013-03-05 17:47:11 -08001221bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001222 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001223 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001224 mCurrentState.requested_legacy.w = w;
1225 mCurrentState.requested_legacy.h = h;
1226 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001228
1229 // record the new size, from this point on, when the client request
1230 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001231 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001232 return true;
1233}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001234bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001235 if (mCurrentState.color.a == alpha) return false;
1236 mCurrentState.sequence++;
1237 mCurrentState.color.a = alpha;
1238 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001239 setTransactionFlags(eTransactionNeeded);
1240 return true;
1241}
chaviw13fdc492017-06-27 12:40:18 -07001242
Valerie Haudd0b7572019-01-29 14:59:27 -08001243bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1244 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001245 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001246 }
1247 mCurrentState.sequence++;
1248 mCurrentState.modified = true;
1249 setTransactionFlags(eTransactionNeeded);
1250
1251 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001252 // create background color layer if one does not yet exist
1253 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1254 const String8& name = mName + "BackgroundColorLayer";
1255 mCurrentState.bgColorLayer =
1256 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001257
Valerie Haudd0b7572019-01-29 14:59:27 -08001258 // add to child list
1259 addChild(mCurrentState.bgColorLayer);
1260 mFlinger->mLayersAdded = true;
1261 // set up SF to handle added color layer
1262 if (isRemovedFromCurrentState()) {
1263 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1264 }
1265 mFlinger->setTransactionFlags(eTransactionNeeded);
1266 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1267 mCurrentState.bgColorLayer->reparent(nullptr);
1268 mCurrentState.bgColorLayer = nullptr;
1269 return true;
1270 }
1271
1272 mCurrentState.bgColorLayer->setColor(color);
1273 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1274 mCurrentState.bgColorLayer->setAlpha(alpha);
1275 mCurrentState.bgColorLayer->setDataspace(dataspace);
1276
chaviw13fdc492017-06-27 12:40:18 -07001277 return true;
1278}
1279
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001280bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001281 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001282
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001283 mCurrentState.sequence++;
1284 mCurrentState.cornerRadius = cornerRadius;
1285 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001286 setTransactionFlags(eTransactionNeeded);
1287 return true;
1288}
1289
Robert Carrd4ae7f32018-06-07 16:10:57 -07001290bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1291 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001292 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001293 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1294
1295 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1296 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1297 return false;
1298 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001299 mCurrentState.sequence++;
1300 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1301 matrix.dsdy);
1302 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001303 setTransactionFlags(eTransactionNeeded);
1304 return true;
1305}
Marissa Wall61c58622018-07-18 10:12:20 -07001306
Mathias Agopian13127d82013-03-05 17:47:11 -08001307bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001308 mCurrentState.requestedTransparentRegion_legacy = transparent;
1309 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001310 setTransactionFlags(eTransactionNeeded);
1311 return true;
1312}
1313bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001314 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1315 if (mCurrentState.flags == newFlags) return false;
1316 mCurrentState.sequence++;
1317 mCurrentState.flags = newFlags;
1318 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001319 setTransactionFlags(eTransactionNeeded);
1320 return true;
1321}
Robert Carr99e27f02016-06-16 15:18:02 -07001322
Marissa Wallf58c14b2018-07-24 10:50:43 -07001323bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001324 if (mCurrentState.requestedCrop_legacy == crop) return false;
1325 mCurrentState.sequence++;
1326 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001327 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001328 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001329 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001330 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1331
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001332 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001333 setTransactionFlags(eTransactionNeeded);
1334 return true;
1335}
Robert Carr8d5227b2017-03-16 15:41:03 -07001336
Robert Carrc3574f72016-03-24 12:19:32 -07001337bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001338 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001339 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001340 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001341 return true;
1342}
1343
Evan Roskyef876c92019-01-25 17:46:06 -08001344bool Layer::setMetadata(const LayerMetadata& data) {
1345 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001346 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001347 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001348 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001349 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001350}
1351
Mathias Agopian13127d82013-03-05 17:47:11 -08001352bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001353 if (mCurrentState.layerStack == layerStack) return false;
1354 mCurrentState.sequence++;
1355 mCurrentState.layerStack = layerStack;
1356 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001357 setTransactionFlags(eTransactionNeeded);
1358 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001359}
1360
Robert Carr1f0a16a2016-10-24 16:27:39 -07001361uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001362 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001363 if (p == nullptr) {
1364 return getDrawingState().layerStack;
1365 }
1366 return p->getLayerStack();
1367}
1368
Marissa Wallf58c14b2018-07-24 10:50:43 -07001369void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001370 mCurrentState.barrierLayer_legacy = barrierLayer;
1371 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001372 // We don't set eTransactionNeeded, because just receiving a deferral
1373 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001374 mCurrentState.modified = true;
1375 pushPendingState();
1376 mCurrentState.barrierLayer_legacy = nullptr;
1377 mCurrentState.frameNumber_legacy = 0;
1378 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001379}
1380
Marissa Wallf58c14b2018-07-24 10:50:43 -07001381void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001382 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001383 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001384}
1385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386// ----------------------------------------------------------------------------
1387// pageflip handling...
1388// ----------------------------------------------------------------------------
1389
Robert Carr1f0a16a2016-10-24 16:27:39 -07001390bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001391 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001392 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001393 if (parent != nullptr && parent->isHiddenByPolicy()) {
1394 return true;
1395 }
1396 return s.flags & layer_state_t::eLayerHidden;
1397}
1398
David Sodman41fdfc92017-11-06 16:09:56 -08001399uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001400 // TODO: should we do something special if mSecure is set?
1401 if (mProtectedByApp) {
1402 // need a hardware-protected path to external video sink
1403 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001404 }
Riley Andrews03414a12014-07-01 14:22:59 -07001405 if (mPotentialCursor) {
1406 usage |= GraphicBuffer::USAGE_CURSOR;
1407 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001408 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001409 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001410}
1411
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001412void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001413 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001414 // Disable setting transform hint if the debug flag is set or if the
1415 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1416 // in one orientation.
1417 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001418 // The transform hint is used to improve performance, but we can
1419 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001420 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001421 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001422 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001423 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001424 orientation = 0;
1425 }
1426 }
David Sodmaneb085e02017-10-05 18:49:04 -07001427 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001428}
1429
Mathias Agopian13127d82013-03-05 17:47:11 -08001430// ----------------------------------------------------------------------------
1431// debugging
1432// ----------------------------------------------------------------------------
1433
Marissa Wall61c58622018-07-18 10:12:20 -07001434// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001435LayerDebugInfo Layer::getLayerDebugInfo() const {
1436 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001437 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001438 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001439 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001440 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001441 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001442 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001443 info.mVisibleRegion = visibleRegion;
1444 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001445 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001446 info.mX = ds.active_legacy.transform.tx();
1447 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001448 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001449 info.mWidth = ds.active_legacy.w;
1450 info.mHeight = ds.active_legacy.h;
1451 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001452 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001453 info.mFlags = ds.flags;
1454 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001455 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001456 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1457 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1458 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1459 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001460 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001461 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001462 if (buffer != 0) {
1463 info.mActiveBufferWidth = buffer->getWidth();
1464 info.mActiveBufferHeight = buffer->getHeight();
1465 info.mActiveBufferStride = buffer->getStride();
1466 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001467 } else {
1468 info.mActiveBufferWidth = 0;
1469 info.mActiveBufferHeight = 0;
1470 info.mActiveBufferStride = 0;
1471 info.mActiveBufferFormat = 0;
1472 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001473 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001474 info.mNumQueuedFrames = getQueuedFrameCount();
1475 info.mRefreshPending = isBufferLatched();
1476 info.mIsOpaque = isOpaque(ds);
1477 info.mContentDirty = contentDirty;
1478 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001479}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001480
Yiwei Zhang5434a782018-12-05 18:06:32 -08001481void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001482 result.append("-------------------------------");
1483 result.append("-------------------------------");
1484 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001485 result.append(" Layer name\n");
1486 result.append(" Z | ");
1487 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001488 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001489 result.append(" Disp Frame (LTRB) | ");
1490 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001491 result.append("-------------------------------");
1492 result.append("-------------------------------");
1493 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001494}
1495
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001496void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1497 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1498 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001499 return;
1500 }
1501
Yiwei Zhang5434a782018-12-05 18:06:32 -08001502 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001503 if (mName.length() > 77) {
1504 std::string shortened;
1505 shortened.append(mName.string(), 36);
1506 shortened.append("[...]");
1507 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001508 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001509 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001510 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001511 }
1512
Yiwei Zhang5434a782018-12-05 18:06:32 -08001513 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001514
Alec Mourib416efd2018-09-06 21:01:59 +00001515 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001516 const auto& compositionState = outputLayer->getState();
1517
Chia-I Wu1e043612018-03-01 09:45:09 -08001518 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001519 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001520 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001521 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001522 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001523 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1524 StringAppendF(&result, "%10s | ",
1525 toString(getCompositionLayer() ? compositionState.bufferTransform
1526 : static_cast<Hwc2::Transform>(0))
1527 .c_str());
1528 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001529 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001530 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001531 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1532 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001533
Yichi Chen6ca35192018-05-29 12:20:43 +08001534 result.append("- - - - - - - - - - - - - - - -");
1535 result.append("- - - - - - - - - - - - - - - -");
1536 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001537}
Dan Stozae22aec72016-08-01 13:20:59 -07001538
Yiwei Zhang5434a782018-12-05 18:06:32 -08001539void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001540 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001541}
1542
Svetoslavd85084b2014-03-20 10:28:31 -07001543void Layer::clearFrameStats() {
1544 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001545}
1546
Jamie Gennis6547ff42013-07-16 20:12:42 -07001547void Layer::logFrameStats() {
1548 mFrameTracker.logAndResetStats(mName);
1549}
1550
Svetoslavd85084b2014-03-20 10:28:31 -07001551void Layer::getFrameStats(FrameStats* outStats) const {
1552 mFrameTracker.getStats(outStats);
1553}
1554
Yiwei Zhang5434a782018-12-05 18:06:32 -08001555void Layer::dumpFrameEvents(std::string& result) {
1556 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001557 Mutex::Autolock lock(mFrameEventHistoryMutex);
1558 mFrameEventHistory.checkFencesForCompletion();
1559 mFrameEventHistory.dump(result);
1560}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001561
Brian Anderson5ea5e592016-12-01 16:54:33 -08001562void Layer::onDisconnect() {
1563 Mutex::Autolock lock(mFrameEventHistoryMutex);
1564 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001565 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001566}
1567
Brian Anderson3890c392016-07-25 12:48:08 -07001568void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001569 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001570 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001571 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1572 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001573 }
1574
Brian Andersond6927fb2016-07-23 23:37:30 -07001575 Mutex::Autolock lock(mFrameEventHistoryMutex);
1576 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001577 // If there are any unsignaled fences in the aquire timeline at this
1578 // point, the previously queued frame hasn't been latched yet. Go ahead
1579 // and try to get the signal time here so the syscall is taken out of
1580 // the main thread's critical path.
1581 mAcquireTimeline.updateSignalTimes();
1582 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001583 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001584 mFrameEventHistory.addQueue(*newTimestamps);
1585 }
1586
Brian Anderson3890c392016-07-25 12:48:08 -07001587 if (outDelta) {
1588 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001589 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001590}
Dan Stozae77c7662016-05-13 11:37:28 -07001591
Chia-I Wu98f1c102017-05-30 14:54:08 -07001592size_t Layer::getChildrenCount() const {
1593 size_t count = 0;
1594 for (const sp<Layer>& child : mCurrentChildren) {
1595 count += 1 + child->getChildrenCount();
1596 }
1597 return count;
1598}
1599
Robert Carr1f0a16a2016-10-24 16:27:39 -07001600void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001601 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001602 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001603
Robert Carr1f0a16a2016-10-24 16:27:39 -07001604 mCurrentChildren.add(layer);
1605 layer->setParent(this);
1606}
1607
1608ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001609 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001610 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001611
Robert Carr1323c952019-01-28 18:13:27 -08001612 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001613 return mCurrentChildren.remove(layer);
1614}
1615
Robert Carr1db73f62016-12-21 12:58:51 -08001616bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1617 sp<Handle> handle = nullptr;
1618 sp<Layer> newParent = nullptr;
1619 if (newParentHandle == nullptr) {
1620 return false;
1621 }
1622 handle = static_cast<Handle*>(newParentHandle.get());
1623 newParent = handle->owner.promote();
1624 if (newParent == nullptr) {
1625 ALOGE("Unable to promote Layer handle");
1626 return false;
1627 }
1628
chaviw5aedec92018-10-22 10:40:38 -07001629 if (attachChildren()) {
1630 setTransactionFlags(eTransactionNeeded);
1631 }
Robert Carr1db73f62016-12-21 12:58:51 -08001632 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001633 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001634 }
1635 mCurrentChildren.clear();
1636
1637 return true;
1638}
1639
Robert Carr15eae092018-03-23 13:43:53 -07001640void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001641 for (const sp<Layer>& child : mDrawingChildren) {
1642 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001643 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001644 }
1645}
1646
chaviwf1961f72017-09-18 16:41:07 -07001647bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001648 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001649
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001650 // While layers are detached, we allow most operations
1651 // and simply halt performing the actual transaction. However
1652 // for reparent != null we would enter the mRemovedFromCurrentState
1653 // state, regardless of whether doTransaction was called, and
1654 // so we need to prevent the update here.
1655 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001656 return false;
1657 }
1658
Robert Carr54cf5b12019-01-25 14:02:28 -08001659 sp<Layer> newParent;
1660 if (newParentHandle != nullptr) {
1661 auto handle = static_cast<Handle*>(newParentHandle.get());
1662 newParent = handle->owner.promote();
1663 if (newParent == nullptr) {
1664 ALOGE("Unable to promote Layer handle");
1665 return false;
1666 }
1667 if (newParent == this) {
1668 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1669 return false;
1670 }
1671 }
1672
chaviwf1961f72017-09-18 16:41:07 -07001673 sp<Layer> parent = getParent();
1674 if (parent != nullptr) {
1675 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001676 }
1677
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001678 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001679 newParent->addChild(this);
1680 if (!newParent->isRemovedFromCurrentState()) {
1681 addToCurrentState();
1682 } else {
1683 onRemovedFromCurrentState();
1684 }
1685
1686 if (mLayerDetached) {
1687 mLayerDetached = false;
1688 callSetTransactionFlags = true;
1689 }
1690 } else {
1691 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001692 }
1693
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001694 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001695 setTransactionFlags(eTransactionNeeded);
1696 }
chaviw06178942017-07-27 10:25:59 -07001697 return true;
1698}
1699
Robert Carr9524cb32017-02-13 11:32:32 -08001700bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001701 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001702 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001703 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001704 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001705 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001706 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001707 }
Robert Carr7f619b22017-11-06 12:56:35 -08001708 }
Robert Carr9524cb32017-02-13 11:32:32 -08001709
1710 return true;
1711}
1712
chaviw5aedec92018-10-22 10:40:38 -07001713bool Layer::attachChildren() {
1714 bool changed = false;
1715 for (const sp<Layer>& child : mCurrentChildren) {
1716 sp<Client> parentClient = mClientRef.promote();
1717 sp<Client> client(child->mClientRef.promote());
1718 if (client != nullptr && parentClient != client) {
1719 if (child->mLayerDetached) {
1720 child->mLayerDetached = false;
1721 changed = true;
1722 }
1723 changed |= child->attachChildren();
1724 }
1725 }
1726
1727 return changed;
1728}
1729
Peiyong Lind3788632018-09-18 16:01:31 -07001730bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001731 static const mat4 identityMatrix = mat4();
1732
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001733 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001734 return false;
1735 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001736 ++mCurrentState.sequence;
1737 mCurrentState.colorTransform = matrix;
1738 mCurrentState.hasColorTransform = matrix != identityMatrix;
1739 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001740 setTransactionFlags(eTransactionNeeded);
1741 return true;
1742}
1743
chaviwf66724d2018-11-28 16:35:21 -08001744mat4 Layer::getColorTransform() const {
1745 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1746 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1747 colorTransform = parent->getColorTransform() * colorTransform;
1748 }
1749 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001750}
1751
1752bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001753 bool hasColorTransform = getDrawingState().hasColorTransform;
1754 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1755 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1756 }
1757 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001758}
1759
Chia-I Wu11481472018-05-04 10:43:19 -07001760bool Layer::isLegacyDataSpace() const {
1761 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001762 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001763 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001764}
1765
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001767 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768}
1769
1770void Layer::clearSyncPoints() {
1771 for (const auto& child : mCurrentChildren) {
1772 child->clearSyncPoints();
1773 }
1774
1775 Mutex::Autolock lock(mLocalSyncPointMutex);
1776 for (auto& point : mLocalSyncPoints) {
1777 point->setFrameAvailable();
1778 }
1779 mLocalSyncPoints.clear();
1780}
1781
1782int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001783 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001784}
1785
Robert Carr29abff82017-12-04 13:51:20 -08001786bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1787 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001788 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001789 return state.zOrderRelativeOf != nullptr;
1790}
1791
David Sodman41fdfc92017-11-06 16:09:56 -08001792__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001793 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001794 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1795 "makeTraversalList received invalid stateSet");
1796 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1797 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001798 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001799
Robert Carr29abff82017-12-04 13:51:20 -08001800 if (state.zOrderRelatives.size() == 0) {
1801 *outSkipRelativeZUsers = true;
1802 return children;
1803 }
1804
chaviwfd462612018-05-31 16:11:27 -07001805 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001806 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001807 sp<Layer> strongRelative = weakRelative.promote();
1808 if (strongRelative != nullptr) {
1809 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001810 }
1811 }
1812
Dan Stoza412903f2017-04-27 13:42:17 -07001813 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001814 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001815 if (childState.zOrderRelativeOf != nullptr) {
1816 continue;
1817 }
Robert Carrdb66e622017-04-10 16:55:57 -07001818 traverse.add(child);
1819 }
1820
1821 return traverse;
1822}
1823
Robert Carr1f0a16a2016-10-24 16:27:39 -07001824/**
Robert Carrdb66e622017-04-10 16:55:57 -07001825 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001826 */
Dan Stoza412903f2017-04-27 13:42:17 -07001827void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001828 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1829 // produce a new list for traversing, including our relatives, and not including our children
1830 // who are relatives of another surface. In the case that there are no relative Z,
1831 // makeTraversalList returns our children directly to avoid significant overhead.
1832 // However in this case we need to take the responsibility for filtering children which
1833 // are relatives of another surface here.
1834 bool skipRelativeZUsers = false;
1835 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001836
Robert Carr1f0a16a2016-10-24 16:27:39 -07001837 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001838 for (; i < list.size(); i++) {
1839 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001840 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1841 continue;
1842 }
1843
Robert Carrdb66e622017-04-10 16:55:57 -07001844 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001845 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001846 }
Dan Stoza412903f2017-04-27 13:42:17 -07001847 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001848 }
Robert Carr29abff82017-12-04 13:51:20 -08001849
Dan Stoza412903f2017-04-27 13:42:17 -07001850 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001851 for (; i < list.size(); i++) {
1852 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001853
Robert Carr29abff82017-12-04 13:51:20 -08001854 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1855 continue;
1856 }
Dan Stoza412903f2017-04-27 13:42:17 -07001857 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001858 }
1859}
1860
1861/**
Robert Carrdb66e622017-04-10 16:55:57 -07001862 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001863 */
Dan Stoza412903f2017-04-27 13:42:17 -07001864void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1865 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001866 // See traverseInZOrder for documentation.
1867 bool skipRelativeZUsers = false;
1868 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001869
Robert Carr1f0a16a2016-10-24 16:27:39 -07001870 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001871 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001872 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001873
1874 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1875 continue;
1876 }
1877
Robert Carrdb66e622017-04-10 16:55:57 -07001878 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001879 break;
1880 }
Dan Stoza412903f2017-04-27 13:42:17 -07001881 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001882 }
Dan Stoza412903f2017-04-27 13:42:17 -07001883 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001884 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001885 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001886
1887 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1888 continue;
1889 }
1890
Dan Stoza412903f2017-04-27 13:42:17 -07001891 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001892 }
1893}
1894
chaviw4b129c22018-04-09 16:19:43 -07001895LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1896 const std::vector<Layer*>& layersInTree) {
1897 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1898 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001899 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1900 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001901 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001902
chaviwfd462612018-05-31 16:11:27 -07001903 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001904 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1905 sp<Layer> strongRelative = weakRelative.promote();
1906 // Only add relative layers that are also descendents of the top most parent of the tree.
1907 // If a relative layer is not a descendent, then it should be ignored.
1908 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1909 traverse.add(strongRelative);
1910 }
1911 }
1912
1913 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001914 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001915 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1916 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1917 // the child here since it won't be added later as a relative.
1918 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1919 childState.zOrderRelativeOf.promote().get())) {
1920 continue;
1921 }
1922 traverse.add(child);
1923 }
1924
1925 return traverse;
1926}
1927
1928void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1929 LayerVector::StateSet stateSet,
1930 const LayerVector::Visitor& visitor) {
1931 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001932
1933 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001934 for (; i < list.size(); i++) {
1935 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001936 if (relative->getZ() >= 0) {
1937 break;
1938 }
chaviw4b129c22018-04-09 16:19:43 -07001939 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001940 }
chaviw4b129c22018-04-09 16:19:43 -07001941
chaviwa76b2712017-09-20 12:02:26 -07001942 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001943 for (; i < list.size(); i++) {
1944 const auto& relative = list[i];
1945 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001946 }
1947}
1948
chaviw4b129c22018-04-09 16:19:43 -07001949std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1950 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1951 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1952
1953 std::vector<Layer*> layersInTree = {this};
1954 for (size_t i = 0; i < children.size(); i++) {
1955 const auto& child = children[i];
1956 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1957 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1958 }
1959
1960 return layersInTree;
1961}
1962
1963void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1964 const LayerVector::Visitor& visitor) {
1965 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1966 std::sort(layersInTree.begin(), layersInTree.end());
1967 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1968}
1969
Peiyong Linefefaac2018-08-17 12:27:51 -07001970ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001971 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001972}
1973
chaviw13fdc492017-06-27 12:40:18 -07001974half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001975 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001976
chaviw13fdc492017-06-27 12:40:18 -07001977 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1978 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001979}
Robert Carr6452f122017-03-21 10:41:29 -07001980
chaviw13fdc492017-06-27 12:40:18 -07001981half4 Layer::getColor() const {
1982 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001983 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001984}
Robert Carr6452f122017-03-21 10:41:29 -07001985
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001986Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1987 const auto& p = mDrawingParent.promote();
1988 if (p != nullptr) {
1989 RoundedCornerState parentState = p->getRoundedCornerState();
1990 if (parentState.radius > 0) {
1991 ui::Transform t = getActiveTransform(getDrawingState());
1992 t = t.inverse();
1993 parentState.cropRect = t.transform(parentState.cropRect);
1994 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1995 // but a transform matrix can define horizontal and vertical scales.
1996 // Let's take the average between both of them and pass into the shader, practically we
1997 // never do this type of transformation on windows anyway.
1998 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1999 return parentState;
2000 }
2001 }
2002 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002003 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002004}
2005
Robert Carr1f0a16a2016-10-24 16:27:39 -07002006void Layer::commitChildList() {
2007 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2008 const auto& child = mCurrentChildren[i];
2009 child->commitChildList();
2010 }
2011 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002012 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002013}
2014
Robert Carr720e5062018-07-30 17:45:14 -07002015void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002016 mCurrentState.inputInfo = info;
2017 mCurrentState.modified = true;
2018 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002019 setTransactionFlags(eTransactionNeeded);
2020}
2021
chaviw1d044282017-09-27 12:19:28 -07002022void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2023 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2024 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002025 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002026
Peiyong Linefefaac2018-08-17 12:27:51 -07002027 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002028 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002029
2030 layerInfo->set_id(sequence);
2031 layerInfo->set_name(getName().c_str());
2032 layerInfo->set_type(String8(getTypeId()));
2033
2034 for (const auto& child : children) {
2035 layerInfo->add_children(child->sequence);
2036 }
2037
2038 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2039 sp<Layer> strongRelative = weakRelative.promote();
2040 if (strongRelative != nullptr) {
2041 layerInfo->add_relatives(strongRelative->sequence);
2042 }
2043 }
2044
Marissa Wallf58c14b2018-07-24 10:50:43 -07002045 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002046 layerInfo->mutable_transparent_region());
2047 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2048 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2049
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002050 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002051 layerInfo->set_z(state.z);
2052
2053 PositionProto* position = layerInfo->mutable_position();
2054 position->set_x(transform.tx());
2055 position->set_y(transform.ty());
2056
2057 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2058 requestedPosition->set_x(requestedTransform.tx());
2059 requestedPosition->set_y(requestedTransform.ty());
2060
2061 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002062 size->set_w(state.active_legacy.w);
2063 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002064
Marissa Wallf58c14b2018-07-24 10:50:43 -07002065 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002066 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002067
2068 layerInfo->set_is_opaque(isOpaque(state));
2069 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002070
2071 // XXX (b/79210409) mCurrentDataSpace is not protected
2072 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2073
chaviw1d044282017-09-27 12:19:28 -07002074 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2075 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2076 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2077 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) {
2098 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002099 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002100 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002101 }
2102
2103 layerInfo->set_queued_frames(getQueuedFrameCount());
2104 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002105 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002106 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002107
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002108 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002109 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002110 if (barrierLayer != nullptr) {
2111 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2112 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002113 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002114 }
2115 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002116
2117 auto protoMap = layerInfo->mutable_metadata();
2118 for (const auto& entry : state.metadata.mMap) {
2119 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2120 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002121 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
2122 LayerProtoHelper::writeToProto(mSourceBounds, layerInfo->mutable_source_bounds());
2123 LayerProtoHelper::writeToProto(mScreenBounds, layerInfo->mutable_screen_bounds());
2124 LayerProtoHelper::writeToProto(mBounds, layerInfo->mutable_bounds());
chaviw1d044282017-09-27 12:19:28 -07002125}
2126
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002127void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2128 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2129 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002130 return;
2131 }
2132
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002133 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2134
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002135 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002136
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002137 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002138 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2139
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002140 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002141 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2142
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002143 const int32_t transform =
2144 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002145 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002146
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002147 const int32_t compositionType =
2148 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2149 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002150 layerInfo->set_hwc_composition_type(compositionType);
2151
2152 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2153 static_cast<BufferLayer*>(this)->isProtected()) {
2154 layerInfo->set_is_protected(true);
2155 } else {
2156 layerInfo->set_is_protected(false);
2157 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002158}
2159
Robert Carr2e102c92018-10-23 12:11:15 -07002160bool Layer::isRemovedFromCurrentState() const {
2161 return mRemovedFromCurrentState;
2162}
2163
Arthur Hungd20b2702019-01-14 18:16:16 +08002164InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002165 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002166
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002167 if (info.displayId == ADISPLAY_ID_NONE) {
2168 info.displayId = mDrawingState.layerStack;
2169 }
2170
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002171 ui::Transform t = getTransform();
2172 const float xScale = t.sx();
2173 const float yScale = t.sy();
2174 if (xScale != 1.0f || yScale != 1.0f) {
2175 info.windowXScale *= 1.0f / xScale;
2176 info.windowYScale *= 1.0f / yScale;
2177 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002178 }
Robert Carre07e1032018-11-26 12:55:53 -08002179
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002180 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002181 // If this is a portal window, set the touchableRegion to the layerBounds.
2182 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2183 ? getBufferSize(getDrawingState())
2184 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002185 if (!layerBounds.isValid()) {
2186 layerBounds = getCroppedBufferSize(getDrawingState());
2187 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002188 layerBounds = t.transform(layerBounds);
2189 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2190
Arthur Hungd20b2702019-01-14 18:16:16 +08002191 // Input coordinate should match the layer bounds.
2192 info.frameLeft = layerBounds.left;
2193 info.frameTop = layerBounds.top;
2194 info.frameRight = layerBounds.right;
2195 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002196
2197 // Position the touchable region relative to frame screen location and restrict it to frame
2198 // bounds.
2199 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002200 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002201 return info;
2202}
2203
2204bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002205 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002206}
2207
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002208std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2209 return nullptr;
2210}
2211
chaviw3e727cd2019-01-31 13:41:05 -08002212bool Layer::canReceiveInput() const {
2213 return isVisible();
2214}
2215
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002216compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2217 const sp<const DisplayDevice>& display) const {
2218 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2219}
2220
Mathias Agopian13127d82013-03-05 17:47:11 -08002221// ---------------------------------------------------------------------------
2222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002223}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002224
2225#if defined(__gl_h_)
2226#error "don't include gl/gl.h in this file"
2227#endif
2228
2229#if defined(__gl2_h_)
2230#error "don't include gl2/gl2.h in this file"
2231#endif