blob: af27ca3571a3cd20cee317c98ee969bb31fcddea [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)
Lloyd Piquefa8fd6b2019-01-29 18:42:24 -080072 : mFlinger(args.flinger), mName(args.name), mClientRef(args.client) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070073 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070074
75 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070076 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
77 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
78 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079
Dan Stozaf7ba41a2017-05-10 15:11:11 -070080 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081
Lloyd Pique0449b0f2018-12-20 16:23:45 -080082 mCurrentState.active_legacy.w = args.w;
83 mCurrentState.active_legacy.h = args.h;
84 mCurrentState.flags = layerFlags;
85 mCurrentState.active_legacy.transform.set(0, 0);
86 mCurrentState.crop_legacy.makeInvalid();
87 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
88 mCurrentState.z = 0;
89 mCurrentState.color.a = 1.0f;
90 mCurrentState.layerStack = 0;
91 mCurrentState.sequence = 0;
92 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080093 mCurrentState.active.w = UINT32_MAX;
94 mCurrentState.active.h = UINT32_MAX;
95 mCurrentState.active.transform.set(0, 0);
96 mCurrentState.transform = 0;
97 mCurrentState.transformToDisplayInverse = false;
98 mCurrentState.crop.makeInvalid();
99 mCurrentState.acquireFence = new Fence(-1);
100 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
101 mCurrentState.hdrMetadata.validTypes = 0;
102 mCurrentState.surfaceDamageRegion.clear();
103 mCurrentState.cornerRadius = 0.0f;
104 mCurrentState.api = -1;
105 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800106 mCurrentState.colorSpaceAgnostic = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
108 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800109 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700110
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800111 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700112 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800113 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200114 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700115
116 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700117}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700118
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800120 sp<Client> c(mClientRef.promote());
121 if (c != 0) {
122 c->detachLayer(this);
123 }
124
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000125 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700126
Robert Carr2e102c92018-10-23 12:11:15 -0700127 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700128}
129
Mathias Agopian13127d82013-03-05 17:47:11 -0800130// ---------------------------------------------------------------------------
131// callbacks
132// ---------------------------------------------------------------------------
133
David Sodmaneb085e02017-10-05 18:49:04 -0700134/*
135 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
136 * Layer. So, the implementation is done in BufferLayer. When called on a
137 * ColorLayer object, it's essentially a NOP.
138 */
David Sodmaneb085e02017-10-05 18:49:04 -0700139void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800140
Chia-I Wuc6657022017-08-15 11:18:17 -0700141void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700142 mRemovedFromCurrentState = true;
143
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800144 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
145 if (mCurrentState.zOrderRelativeOf != nullptr) {
146 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
147 if (strongRelative != nullptr) {
148 strongRelative->removeZOrderRelative(this);
149 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700150 }
chaviw606e5cf2019-03-01 10:12:10 -0800151 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700152 }
Rob Carr4bba3702018-10-08 21:53:30 +0000153
Robert Carr2e102c92018-10-23 12:11:15 -0700154 // Since we are no longer reachable from CurrentState SurfaceFlinger
155 // will no longer invoke doTransaction for us, and so we will
156 // never finish applying transactions. We signal the sync point
157 // now so that another layer will not become indefinitely
158 // blocked.
159 for (auto& point: mRemoteSyncPoints) {
160 point->setTransactionApplied();
161 }
162 mRemoteSyncPoints.clear();
163
164 {
165 Mutex::Autolock syncLock(mLocalSyncPointMutex);
166 for (auto& point : mLocalSyncPoints) {
167 point->setFrameAvailable();
168 }
169 mLocalSyncPoints.clear();
170 }
171
Chia-I Wuc6657022017-08-15 11:18:17 -0700172 for (const auto& child : mCurrentChildren) {
173 child->onRemovedFromCurrentState();
174 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800175
176 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700177}
Chia-I Wu38512252017-05-17 14:36:16 -0700178
chaviw61626f22018-11-15 16:26:27 -0800179void Layer::addToCurrentState() {
180 mRemovedFromCurrentState = false;
181
182 for (const auto& child : mCurrentChildren) {
183 child->addToCurrentState();
184 }
185}
186
Mathias Agopian13127d82013-03-05 17:47:11 -0800187// ---------------------------------------------------------------------------
188// set-up
189// ---------------------------------------------------------------------------
190
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700191const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800192 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
chaviw13fdc492017-06-27 12:40:18 -0700195bool Layer::getPremultipledAlpha() const {
196 return mPremultipliedAlpha;
197}
198
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700199sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800200 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700201 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800202}
203
204// ---------------------------------------------------------------------------
205// h/w composer set-up
206// ---------------------------------------------------------------------------
207
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800208bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
209 auto outputLayer = findOutputLayerForDisplay(displayDevice);
210 LOG_FATAL_IF(!outputLayer);
211 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
212}
213
214HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
215 auto outputLayer = findOutputLayerForDisplay(displayDevice);
216 if (!outputLayer || !outputLayer->getState().hwc) {
217 return nullptr;
218 }
219 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700220}
Steven Thomasb02664d2017-07-26 18:48:28 -0700221
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800222Rect Layer::getContentCrop() const {
223 // this is the crop rectangle that applies to the buffer
224 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700225 Rect crop;
226 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800227 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700228 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800229 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800230 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800231 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700232 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800233 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700234 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700235 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 return crop;
237}
238
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700239static Rect reduce(const Rect& win, const Region& exclude) {
240 if (CC_LIKELY(exclude.isEmpty())) {
241 return win;
242 }
243 if (exclude.isRect()) {
244 return win.reduce(exclude.getBounds());
245 }
246 return Region(win).subtract(exclude).getBounds();
247}
248
Dan Stoza80d61162017-12-20 15:57:52 -0800249static FloatRect reduce(const FloatRect& win, const Region& exclude) {
250 if (CC_LIKELY(exclude.isEmpty())) {
251 return win;
252 }
253 // Convert through Rect (by rounding) for lack of FloatRegion
254 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
255}
256
Vishnu Nair4351ad52019-02-11 14:13:02 -0800257Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800258 if (!reduceTransparentRegion) {
259 return Rect{mScreenBounds};
260 }
261
262 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800263 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800264 // Transform to screen space.
265 bounds = t.transform(bounds);
266 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700267}
268
Vishnu Nair4351ad52019-02-11 14:13:02 -0800269FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000270 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800271 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700272}
273
Vishnu Nairf0c28512019-02-08 12:40:28 -0800274FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
275 // Subtract the transparent region and snap to the bounds.
276 return reduce(mBounds, activeTransparentRegion);
277}
278
Vishnu Nair4351ad52019-02-11 14:13:02 -0800279ui::Transform Layer::getTransformWithScale() const {
280 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
281 // it isFixedSize) then there may be additional scaling not accounted
282 // for in the transform. We need to mirror this scaling to child surfaces
283 // or we will break the contract where WM can treat child surfaces as
284 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800285 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800286 return mEffectiveTransform;
287 }
288
289 int bufferWidth;
290 int bufferHeight;
291 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800292 bufferWidth = mActiveBuffer->getWidth();
293 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800294 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800295 bufferHeight = mActiveBuffer->getWidth();
296 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800297 }
298 float sx = getActiveWidth(getDrawingState()) / static_cast<float>(bufferWidth);
299 float sy = getActiveHeight(getDrawingState()) / static_cast<float>(bufferHeight);
300 ui::Transform extraParentScaling;
301 extraParentScaling.set(sx, 0, 0, sy);
302 return mEffectiveTransform * extraParentScaling;
303}
304
305void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
306 const State& s(getDrawingState());
307
308 // Calculate effective layer transform
309 mEffectiveTransform = parentTransform * getActiveTransform(s);
310
311 // Transform parent bounds to layer space
312 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
313
314 // Calculate display frame
315 mSourceBounds = computeSourceBounds(parentBounds);
316
317 // Calculate bounds by croping diplay frame with layer crop and parent bounds
318 FloatRect bounds = mSourceBounds;
319 const Rect layerCrop = getCrop(s);
320 if (!layerCrop.isEmpty()) {
321 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
322 }
323 bounds = bounds.intersect(parentBounds);
324
325 mBounds = bounds;
326 mScreenBounds = mEffectiveTransform.transform(mBounds);
327 for (const sp<Layer>& child : mDrawingChildren) {
328 child->computeBounds(mBounds, getTransformWithScale());
329 }
330}
331
Robert Carrb5d3d262016-03-25 15:08:13 -0700332
Robert Carr1f0a16a2016-10-24 16:27:39 -0700333
Vishnu Nair60356342018-11-13 13:00:45 -0800334Rect Layer::getCroppedBufferSize(const State& s) const {
335 Rect size = getBufferSize(s);
336 Rect crop = getCrop(s);
337 if (!crop.isEmpty() && size.isValid()) {
338 size.intersect(crop, &size);
339 } else if (!crop.isEmpty()) {
340 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700341 }
Vishnu Nair60356342018-11-13 13:00:45 -0800342 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800343}
344
Alec Mouri1f3bd182019-01-24 15:20:18 +0000345Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700346 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800347 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700348 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800349
350 // apply the projection's clipping to the window crop in
351 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700352 // if there are no window scaling involved, this operation will map to full
353 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700354
Vishnu Nair4351ad52019-02-11 14:13:02 -0800355 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000356 ui::Transform t = getTransform();
357 // Transform to screen space.
358 activeCropFloat = t.transform(activeCropFloat);
359 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
360 // Back to layer space to work with the content crop.
361 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800362 // This needs to be here as transform.transform(Rect) computes the
363 // transformed rect and then takes the bounding box of the result before
364 // returning. This means
365 // transform.inverse().transform(transform.transform(Rect)) != Rect
366 // in which case we need to make sure the final rect is clipped to the
367 // display bounds.
368 Rect activeCrop{activeCropFloat};
369 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000370 activeCrop.clear();
371 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700372 return activeCrop;
373}
374
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700375void Layer::setupRoundedCornersCropCoordinates(Rect win,
376 const FloatRect& roundedCornersCrop) const {
377 // Translate win by the rounded corners rect coordinates, to have all values in
378 // layer coordinate space.
379 win.left -= roundedCornersCrop.left;
380 win.right -= roundedCornersCrop.left;
381 win.top -= roundedCornersCrop.top;
382 win.bottom -= roundedCornersCrop.top;
383
Lloyd Pique0b785d82018-12-04 17:25:27 -0800384 renderengine::Mesh::VertexArray<vec2> cropCoords(
385 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700386 cropCoords[0] = vec2(win.left, win.top);
387 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
388 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
389 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700390}
391
Alec Mouri1f3bd182019-01-24 15:20:18 +0000392FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700393 // the content crop is the area of the content that gets scaled to the
394 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800395 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700396
397 // In addition there is a WM-specified crop we pull from our drawing state.
398 const State& s(getDrawingState());
399
Alec Mouri1f3bd182019-01-24 15:20:18 +0000400 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800401 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700402
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000403 // Transform the window crop to match the buffer coordinate system,
404 // which means using the inverse of the current transform set on the
405 // SurfaceFlingerConsumer.
406 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800407 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000408 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700409 * the code below applies the primary display's inverse transform to the
410 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000411 */
David Sodman41fdfc92017-11-06 16:09:56 -0800412 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000413 // calculate the inverse transform
414 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800415 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800416 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000417 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700418 invTransform = (ui::Transform(invTransformOrient) *
419 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800420 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000421
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800422 int winWidth = bufferSize.getWidth();
423 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000424 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
425 // If the activeCrop has been rotate the ends are rotated but not
426 // the space itself so when transforming ends back we can't rely on
427 // a modification of the axes of rotation. To account for this we
428 // need to reorient the inverse rotation in terms of the current
429 // axes of rotation.
430 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
431 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
432 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800433 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800435 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800437 const Rect winCrop =
438 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439
440 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800441 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 float yScale = crop.getHeight() / float(winHeight);
443
David Sodman41fdfc92017-11-06 16:09:56 -0800444 float insetL = winCrop.left * xScale;
445 float insetT = winCrop.top * yScale;
446 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 float insetB = (winHeight - winCrop.bottom) * yScale;
448
David Sodman41fdfc92017-11-06 16:09:56 -0800449 crop.left += insetL;
450 crop.top += insetT;
451 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 crop.bottom -= insetB;
453
Mathias Agopian13127d82013-03-05 17:47:11 -0800454 return crop;
455}
456
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700457void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800458 const auto outputLayer = findOutputLayerForDisplay(display);
459 LOG_FATAL_IF(!outputLayer);
460 LOG_FATAL_IF(!outputLayer->getState().hwc);
461 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700462
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800463 if (!hasHwcLayer(display)) {
464 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
465 display->getDebugName().c_str());
466 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800467 }
468
Lloyd Pique0b785d82018-12-04 17:25:27 -0800469 LOG_FATAL_IF(!getCompositionLayer());
470 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800471 auto& compositionState = outputLayer->editState();
472
473 // enable this layer
474 compositionState.forceClientComposition = false;
475
476 if (isSecure() && !display->isSecure()) {
477 compositionState.forceClientComposition = true;
478 }
David Sodman15094112018-10-11 09:39:37 -0700479
Mathias Agopian13127d82013-03-05 17:47:11 -0800480 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700481 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800482 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500483 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800484 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800485 blendMode =
486 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 }
David Sodman15094112018-10-11 09:39:37 -0700488 auto error = hwcLayer->setBlendMode(blendMode);
489 ALOGE_IF(error != HWC2::Error::None,
490 "[%s] Failed to set blend mode %s:"
491 " %s (%d)",
492 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
493 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800494 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800495
496 // apply the layer's transform, followed by the display's global transform
497 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700498 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800499 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700500 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800501 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700502 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700503 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000504 activeCrop.clear();
505 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700506 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800507 // This needs to be here as transform.transform(Rect) computes the
508 // transformed rect and then takes the bounding box of the result before
509 // returning. This means
510 // transform.inverse().transform(transform.transform(Rect)) != Rect
511 // in which case we need to make sure the final rect is clipped to the
512 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800513 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000514 activeCrop.clear();
515 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700516 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800517 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
518 activeTransparentRegion.orSelf(
519 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800520 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
521 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800522 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700523 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700524
Vishnu Nair4351ad52019-02-11 14:13:02 -0800525 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800526 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800527 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700528 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000529 frame.clear();
530 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700531 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800532 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700533 error = hwcLayer->setDisplayFrame(transformedFrame);
534 if (error != HWC2::Error::None) {
535 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
536 transformedFrame.left, transformedFrame.top, transformedFrame.right,
537 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
538 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800539 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700540 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700542 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700543 error = hwcLayer->setSourceCrop(sourceCrop);
544 if (error != HWC2::Error::None) {
545 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
546 "%s (%d)",
547 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
548 to_string(error).c_str(), static_cast<int32_t>(error));
549 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800550 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700551 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800553 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700554 error = hwcLayer->setPlaneAlpha(alpha);
555 ALOGE_IF(error != HWC2::Error::None,
556 "[%s] Failed to set plane alpha %.3f: "
557 "%s (%d)",
558 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800559 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560
David Sodman15094112018-10-11 09:39:37 -0700561 error = hwcLayer->setZOrder(z);
562 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
563 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800564 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500565
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800566 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
567 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700568 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400569 if (parent.get()) {
570 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800571 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
572 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
573 if (parentType >= 0 || parentAppId >= 0) {
574 type = parentType;
575 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700576 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400577 }
578
David Sodman15094112018-10-11 09:39:37 -0700579 error = hwcLayer->setInfo(type, appId);
580 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
581 static_cast<int32_t>(error));
582
Lloyd Pique0b785d82018-12-04 17:25:27 -0800583 commonCompositionState.type = type;
584 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700585
Mathias Agopian29a367b2011-07-12 14:51:45 -0700586 /*
587 * Transformations are applied in this order:
588 * 1) buffer orientation/flip/mirror
589 * 2) state transformation (window manager)
590 * 3) layer orientation (screen orientation)
591 * (NOTE: the matrices are multiplied in reverse order)
592 */
593
Peiyong Linefefaac2018-08-17 12:27:51 -0700594 const ui::Transform bufferOrientation(mCurrentTransform);
595 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700596
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800597 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700598 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700599 * the code below applies the primary display's inverse transform to the
600 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700601 */
David Sodman41fdfc92017-11-06 16:09:56 -0800602 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700603 // calculate the inverse transform
604 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800605 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700606 }
Robert Carrcae605c2017-03-29 12:10:31 -0700607
608 /*
609 * Here we cancel out the orientation component of the WM transform.
610 * The scaling and translate components are already included in our bounds
611 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000612 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700613 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700614 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700615 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700616
617 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800618 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700619 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800621 compositionState.forceClientComposition = true;
622 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 } else {
624 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700625 auto error = hwcLayer->setTransform(transform);
626 ALOGE_IF(error != HWC2::Error::None,
627 "[%s] Failed to set transform %s: "
628 "%s (%d)",
629 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
630 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800631 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800632 }
633}
634
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800635void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
636 const auto outputLayer = findOutputLayerForDisplay(display);
637 LOG_FATAL_IF(!outputLayer);
638 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639}
Dan Stozaee44edd2015-03-23 15:50:23 -0700640
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800641bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
642 const auto outputLayer = findOutputLayerForDisplay(display);
643 LOG_FATAL_IF(!outputLayer);
644 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800645}
646
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700647void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800648 const auto outputLayer = findOutputLayerForDisplay(display);
649 LOG_FATAL_IF(!outputLayer);
650
651 if (!outputLayer->getState().hwc ||
652 (*outputLayer->getState().hwc).hwcCompositionType !=
653 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 return;
655 }
656
657 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800658 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659
660 // Apply the layer's transform, followed by the display's global transform
661 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800662 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800663 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700664 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800665 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700666 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700667 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668 auto position = displayTransform.transform(frame);
669
Dominik Laskowski7e045462018-05-30 13:02:02 -0700670 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800671 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800672 ALOGE_IF(error != HWC2::Error::None,
673 "[%s] Failed to set cursor position "
674 "to (%d, %d): %s (%d)",
675 mName.string(), position.left, position.top, to_string(error).c_str(),
676 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677}
Riley Andrews03414a12014-07-01 14:22:59 -0700678
Mathias Agopian13127d82013-03-05 17:47:11 -0800679// ---------------------------------------------------------------------------
680// drawing...
681// ---------------------------------------------------------------------------
682
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000683bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800684 Region& clearRegion, const bool supportProtectedContent,
685 renderengine::LayerSettings& layer) {
686 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800687}
688
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000689bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800690 Region& clearRegion, const bool supportProtectedContent,
691 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000692 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800693 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000694}
695
696bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
697 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800698 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000699 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800700 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000701 half alpha = getAlpha();
702 layer.geometry.boundaries = bounds;
703 if (useIdentityTransform) {
704 layer.geometry.positionTransform = mat4();
705 } else {
706 const ui::Transform transform = getTransform();
707 mat4 m;
708 m[0][0] = transform[0][0];
709 m[0][1] = transform[0][1];
710 m[0][3] = transform[0][2];
711 m[1][0] = transform[1][0];
712 m[1][1] = transform[1][1];
713 m[1][3] = transform[1][2];
714 m[3][0] = transform[2][0];
715 m[3][1] = transform[2][1];
716 m[3][3] = transform[2][2];
717 layer.geometry.positionTransform = m;
718 }
719
720 if (hasColorTransform()) {
721 layer.colorTransform = getColorTransform();
722 }
723
724 const auto roundedCornerState = getRoundedCornerState();
725 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
726 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
727
728 layer.alpha = alpha;
729 layer.sourceDataspace = mCurrentDataSpace;
730 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800731}
732
David Sodman41fdfc92017-11-06 16:09:56 -0800733void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
734 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800735 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800736 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700737 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800738 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800739}
740
David Sodmanc1498e62018-09-12 14:36:26 -0700741void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
742 clearWithOpenGL(renderArea, 0, 0, 0, 0);
743}
744
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800745void Layer::setCompositionType(const sp<const DisplayDevice>& display,
746 Hwc2::IComposerClient::Composition type) {
747 const auto outputLayer = findOutputLayerForDisplay(display);
748 LOG_FATAL_IF(!outputLayer);
749 LOG_FATAL_IF(!outputLayer->getState().hwc);
750 auto& compositionState = outputLayer->editState();
751
752 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
753 toString(type).c_str(), 1);
754 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700755 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800756 (*compositionState.hwc).hwcCompositionType = type;
757
758 auto error = (*compositionState.hwc)
759 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
760 ALOGE_IF(error != HWC2::Error::None,
761 "[%s] Failed to set "
762 "composition type %s: %s (%d)",
763 mName.string(), toString(type).c_str(), to_string(error).c_str(),
764 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765 }
766}
767
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800768Hwc2::IComposerClient::Composition Layer::getCompositionType(
769 const sp<const DisplayDevice>& display) const {
770 const auto outputLayer = findOutputLayerForDisplay(display);
771 LOG_FATAL_IF(!outputLayer);
772 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
773 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800774}
775
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800776bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
777 const auto outputLayer = findOutputLayerForDisplay(display);
778 LOG_FATAL_IF(!outputLayer);
779 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781
Dan Stozacac35382016-01-27 12:21:06 -0800782bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
783 if (point->getFrameNumber() <= mCurrentFrameNumber) {
784 // Don't bother with a SyncPoint, since we've already latched the
785 // relevant frame
786 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700787 }
Robert Carr2e102c92018-10-23 12:11:15 -0700788 if (isRemovedFromCurrentState()) {
789 return false;
790 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700791
Dan Stozacac35382016-01-27 12:21:06 -0800792 Mutex::Autolock lock(mLocalSyncPointMutex);
793 mLocalSyncPoints.push_back(point);
794 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700795}
796
Mathias Agopian13127d82013-03-05 17:47:11 -0800797// ----------------------------------------------------------------------------
798// local state
799// ----------------------------------------------------------------------------
800
Peiyong Lin833074a2018-08-28 11:53:54 -0700801void Layer::computeGeometry(const RenderArea& renderArea,
802 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700803 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700804 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800805 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700806
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000807 vec2 lt = vec2(win.left, win.top);
808 vec2 lb = vec2(win.left, win.bottom);
809 vec2 rb = vec2(win.right, win.bottom);
810 vec2 rt = vec2(win.right, win.top);
811
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800812 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000813 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700814 lt = layerTransform.transform(lt);
815 lb = layerTransform.transform(lb);
816 rb = layerTransform.transform(rb);
817 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000818 }
819
Peiyong Lin833074a2018-08-28 11:53:54 -0700820 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700821 position[0] = renderAreaTransform.transform(lt);
822 position[1] = renderAreaTransform.transform(lb);
823 position[2] = renderAreaTransform.transform(rb);
824 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800825}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800826
David Sodman41fdfc92017-11-06 16:09:56 -0800827bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800828 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700829 return (s.flags & layer_state_t::eLayerSecure);
830}
831
Mathias Agopian13127d82013-03-05 17:47:11 -0800832void Layer::setVisibleRegion(const Region& visibleRegion) {
833 // always called from main thread
834 this->visibleRegion = visibleRegion;
835}
836
837void Layer::setCoveredRegion(const Region& coveredRegion) {
838 // always called from main thread
839 this->coveredRegion = coveredRegion;
840}
841
David Sodman41fdfc92017-11-06 16:09:56 -0800842void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800843 // always called from main thread
844 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
845}
846
Robert Carre5f4f692018-01-12 13:12:28 -0800847void Layer::clearVisibilityRegions() {
848 visibleRegion.clear();
849 visibleNonTransparentRegion.clear();
850 coveredRegion.clear();
851}
852
Mathias Agopian13127d82013-03-05 17:47:11 -0800853// ----------------------------------------------------------------------------
854// transaction
855// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800856
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800857void Layer::pushPendingState() {
858 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700859 return;
860 }
861
Dan Stoza7dde5992015-05-22 09:51:44 -0700862 // If this transaction is waiting on the receipt of a frame, generate a sync
863 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700864 // We don't allow installing sync points after we are removed from the current state
865 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800866 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
867 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800868 if (barrierLayer == nullptr) {
869 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700870 // If we can't promote the layer we are intended to wait on,
871 // then it is expired or otherwise invalid. Allow this transaction
872 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800874 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800875 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800876 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800877 mRemoteSyncPoints.push_back(std::move(syncPoint));
878 } else {
879 // We already missed the frame we're supposed to synchronize
880 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800882 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700883 }
884
Dan Stoza7dde5992015-05-22 09:51:44 -0700885 // Wake us up to check if the frame has been received
886 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700887 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700888 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 mPendingStates.push_back(mCurrentState);
890 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700891}
892
Pablo Ceballos05289c22016-04-14 15:49:55 -0700893void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700895
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 mPendingStates.removeAt(0);
897 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700898}
899
Pablo Ceballos05289c22016-04-14 15:49:55 -0700900bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800902 while (!mPendingStates.empty()) {
903 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700904 if (mRemoteSyncPoints.empty()) {
905 // If we don't have a sync point for this, apply it anyway. It
906 // will be visually wrong, but it should keep us from getting
907 // into too much trouble.
908 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700909 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 stateUpdateAvailable = true;
911 continue;
912 }
913
Marissa Wallf58c14b2018-07-24 10:50:43 -0700914 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800915 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800916 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800917
918 // Signal our end of the sync point and then dispose of it
919 mRemoteSyncPoints.front()->setTransactionApplied();
920 mRemoteSyncPoints.pop_front();
921 continue;
922 }
923
Dan Stoza7dde5992015-05-22 09:51:44 -0700924 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
925 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700926 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700927 stateUpdateAvailable = true;
928
929 // Signal our end of the sync point and then dispose of it
930 mRemoteSyncPoints.front()->setTransactionApplied();
931 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800932 } else {
933 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700936 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700937 stateUpdateAvailable = true;
938 }
939 }
940
941 // If we still have pending updates, wake SurfaceFlinger back up and point
942 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800943 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 setTransactionFlags(eTransactionNeeded);
945 mFlinger->setTransactionFlags(eTraversalNeeded);
946 }
947
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800948 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700949 return stateUpdateAvailable;
950}
951
Marissa Wall61c58622018-07-18 10:12:20 -0700952uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000953 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954
Marissa Wall61c58622018-07-18 10:12:20 -0700955 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
956 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700957
David Sodmaneb085e02017-10-05 18:49:04 -0700958 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700959 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000960 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800961 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
962 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
963 " requested={ wh={%4u,%4u} }}\n"
964 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
965 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700966 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700967 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
968 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
969 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
970 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
971 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700972 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
973 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
974 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700976
Robert Carre392b552017-09-19 12:16:05 -0700977 // Don't let Layer::doTransaction update the drawing state
978 // if we have a pending resize, unless we are in fixed-size mode.
979 // the drawing state will be updated only once we receive a buffer
980 // with the correct size.
981 //
982 // In particular, we want to make sure the clip (which is part
983 // of the geometry state) is latched together with the size but is
984 // latched immediately when no resizing is involved.
985 //
986 // If a sideband stream is attached, however, we want to skip this
987 // optimization so that transactions aren't missed when a buffer
988 // never arrives
989 //
990 // In the case that we don't have a buffer we ignore other factors
991 // and avoid entering the resizePending state. At a high level the
992 // resizePending state is to avoid applying the state of the new buffer
993 // to the old buffer. However in the state where we don't have an old buffer
994 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700995 const bool resizePending =
996 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
997 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800998 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700999 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -08001000 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001001 flags |= eDontUpdateGeometryState;
1002 }
1003 }
1004
Robert Carr7bf247e2017-05-18 14:02:49 -07001005 // Here we apply various requested geometry states, depending on our
1006 // latching configuration. See Layer.h for a detailed discussion of
1007 // how geometry latching is controlled.
1008 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001009 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001010
1011 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1012 // mode, which causes attributes which normally latch regardless of scaling mode,
1013 // to be delayed. We copy the requested state to the active state making sure
1014 // to respect these rules (again see Layer.h for a detailed discussion).
1015 //
1016 // There is an awkward asymmetry in the handling of the crop states in the position
1017 // states, as can be seen below. Largely this arises from position and transform
1018 // being stored in the same data structure while having different latching rules.
1019 // b/38182305
1020 //
Marissa Wall61c58622018-07-18 10:12:20 -07001021 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001022 // applyPendingStates in the presence of deferred transactions.
1023 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001024 float tx = stateToCommit->active_legacy.transform.tx();
1025 float ty = stateToCommit->active_legacy.transform.ty();
1026 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1027 stateToCommit->active_legacy.transform.set(tx, ty);
1028 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001029 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001030 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001031 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001032 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001033 }
1034
Marissa Wall61c58622018-07-18 10:12:20 -07001035 return flags;
1036}
1037
1038uint32_t Layer::doTransaction(uint32_t flags) {
1039 ATRACE_CALL();
1040
chaviw5aedec92018-10-22 10:40:38 -07001041 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001042 return flags;
1043 }
1044
1045 if (mChildrenChanged) {
1046 flags |= eVisibleRegion;
1047 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001048 }
1049
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001050 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001051 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001052 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001053 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001054 }
1055
1056 flags = doTransactionResize(flags, &c);
1057
Alec Mourib416efd2018-09-06 21:01:59 +00001058 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001059
1060 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 // invalidate and recompute the visible regions if needed
1062 flags |= Layer::eVisibleRegion;
1063 }
1064
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001065 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001066 // invalidate and recompute the visible regions if needed
1067 flags |= eVisibleRegion;
1068 this->contentDirty = true;
1069
1070 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001071 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001072 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 }
1074
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001075 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001076 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001077 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001078 }
1079
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001081 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001082 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001083 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084}
1085
Pablo Ceballos05289c22016-04-14 15:49:55 -07001086void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001087 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001088}
1089
Mathias Agopian13127d82013-03-05 17:47:11 -08001090uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001091 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001092}
1093
1094uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001095 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001096}
1097
Robert Carr82364e32016-05-15 11:27:47 -07001098bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001099 if (mCurrentState.requested_legacy.transform.tx() == x &&
1100 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001101 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001102 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001103
1104 // We update the requested and active position simultaneously because
1105 // we want to apply the position portion of the transform matrix immediately,
1106 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001107 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001108 if (immediate && !mFreezeGeometryUpdates) {
1109 // Here we directly update the active state
1110 // unlike other setters, because we store it within
1111 // the transform, but use different latching rules.
1112 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001114 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001115 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001116
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001117 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001118 setTransactionFlags(eTransactionNeeded);
1119 return true;
1120}
Robert Carr82364e32016-05-15 11:27:47 -07001121
Robert Carr1f0a16a2016-10-24 16:27:39 -07001122bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1123 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1124 if (idx < 0) {
1125 return false;
1126 }
1127 if (childLayer->setLayer(z)) {
1128 mCurrentChildren.removeAt(idx);
1129 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001130 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001131 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001132 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001133}
1134
Robert Carr503c7042017-09-27 15:06:08 -07001135bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1136 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1137 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1138 if (idx < 0) {
1139 return false;
1140 }
1141 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1142 mCurrentChildren.removeAt(idx);
1143 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001144 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001145 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001146 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001147}
1148
Robert Carrae060832016-11-28 10:51:00 -08001149bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001150 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1151 mCurrentState.sequence++;
1152 mCurrentState.z = z;
1153 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001154
1155 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001156 if (mCurrentState.zOrderRelativeOf != nullptr) {
1157 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001158 if (strongRelative != nullptr) {
1159 strongRelative->removeZOrderRelative(this);
1160 }
chaviw606e5cf2019-03-01 10:12:10 -08001161 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001162 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001163 setTransactionFlags(eTransactionNeeded);
1164 return true;
1165}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001166
Robert Carrdb66e622017-04-10 16:55:57 -07001167void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001168 mCurrentState.zOrderRelatives.remove(relative);
1169 mCurrentState.sequence++;
1170 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001171 setTransactionFlags(eTransactionNeeded);
1172}
1173
1174void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001175 mCurrentState.zOrderRelatives.add(relative);
1176 mCurrentState.modified = true;
1177 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001178 setTransactionFlags(eTransactionNeeded);
1179}
1180
chaviw606e5cf2019-03-01 10:12:10 -08001181void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1182 mCurrentState.zOrderRelativeOf = relativeOf;
1183 mCurrentState.sequence++;
1184 mCurrentState.modified = true;
1185 setTransactionFlags(eTransactionNeeded);
1186}
1187
Robert Carr503d2bd2017-12-04 15:49:47 -08001188bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001189 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1190 if (handle == nullptr) {
1191 return false;
1192 }
1193 sp<Layer> relative = handle->owner.promote();
1194 if (relative == nullptr) {
1195 return false;
1196 }
1197
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001198 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1199 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001200 return false;
1201 }
1202
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001203 mCurrentState.sequence++;
1204 mCurrentState.modified = true;
1205 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001206
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001207 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001208 if (oldZOrderRelativeOf != nullptr) {
1209 oldZOrderRelativeOf->removeZOrderRelative(this);
1210 }
chaviw606e5cf2019-03-01 10:12:10 -08001211 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001212 relative->addZOrderRelative(this);
1213
1214 setTransactionFlags(eTransactionNeeded);
1215
1216 return true;
1217}
1218
Mathias Agopian13127d82013-03-05 17:47:11 -08001219bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001220 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001221 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001222 mCurrentState.requested_legacy.w = w;
1223 mCurrentState.requested_legacy.h = h;
1224 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001225 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001226
1227 // record the new size, from this point on, when the client request
1228 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001229 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001230 return true;
1231}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001232bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001233 if (mCurrentState.color.a == alpha) return false;
1234 mCurrentState.sequence++;
1235 mCurrentState.color.a = alpha;
1236 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 setTransactionFlags(eTransactionNeeded);
1238 return true;
1239}
chaviw13fdc492017-06-27 12:40:18 -07001240
Valerie Haudd0b7572019-01-29 14:59:27 -08001241bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1242 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001243 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001244 }
1245 mCurrentState.sequence++;
1246 mCurrentState.modified = true;
1247 setTransactionFlags(eTransactionNeeded);
1248
1249 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001250 // create background color layer if one does not yet exist
1251 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1252 const String8& name = mName + "BackgroundColorLayer";
1253 mCurrentState.bgColorLayer =
1254 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001255
Valerie Haudd0b7572019-01-29 14:59:27 -08001256 // add to child list
1257 addChild(mCurrentState.bgColorLayer);
1258 mFlinger->mLayersAdded = true;
1259 // set up SF to handle added color layer
1260 if (isRemovedFromCurrentState()) {
1261 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1262 }
1263 mFlinger->setTransactionFlags(eTransactionNeeded);
1264 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1265 mCurrentState.bgColorLayer->reparent(nullptr);
1266 mCurrentState.bgColorLayer = nullptr;
1267 return true;
1268 }
1269
1270 mCurrentState.bgColorLayer->setColor(color);
1271 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1272 mCurrentState.bgColorLayer->setAlpha(alpha);
1273 mCurrentState.bgColorLayer->setDataspace(dataspace);
1274
chaviw13fdc492017-06-27 12:40:18 -07001275 return true;
1276}
1277
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001278bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001279 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001280
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001281 mCurrentState.sequence++;
1282 mCurrentState.cornerRadius = cornerRadius;
1283 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001284 setTransactionFlags(eTransactionNeeded);
1285 return true;
1286}
1287
Robert Carrd4ae7f32018-06-07 16:10:57 -07001288bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1289 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001290 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001291 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1292
1293 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1294 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1295 return false;
1296 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001297 mCurrentState.sequence++;
1298 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1299 matrix.dsdy);
1300 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001301 setTransactionFlags(eTransactionNeeded);
1302 return true;
1303}
Marissa Wall61c58622018-07-18 10:12:20 -07001304
Mathias Agopian13127d82013-03-05 17:47:11 -08001305bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001306 mCurrentState.requestedTransparentRegion_legacy = transparent;
1307 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001308 setTransactionFlags(eTransactionNeeded);
1309 return true;
1310}
1311bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001312 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1313 if (mCurrentState.flags == newFlags) return false;
1314 mCurrentState.sequence++;
1315 mCurrentState.flags = newFlags;
1316 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001317 setTransactionFlags(eTransactionNeeded);
1318 return true;
1319}
Robert Carr99e27f02016-06-16 15:18:02 -07001320
Marissa Wallf58c14b2018-07-24 10:50:43 -07001321bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001322 if (mCurrentState.requestedCrop_legacy == crop) return false;
1323 mCurrentState.sequence++;
1324 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001325 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001326 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001327 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001328 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1329
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001330 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001331 setTransactionFlags(eTransactionNeeded);
1332 return true;
1333}
Robert Carr8d5227b2017-03-16 15:41:03 -07001334
Robert Carrc3574f72016-03-24 12:19:32 -07001335bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001336 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001337 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001338 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001339 return true;
1340}
1341
Evan Roskyef876c92019-01-25 17:46:06 -08001342bool Layer::setMetadata(const LayerMetadata& data) {
1343 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001344 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001345 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001346 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001347 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001348}
1349
Mathias Agopian13127d82013-03-05 17:47:11 -08001350bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001351 if (mCurrentState.layerStack == layerStack) return false;
1352 mCurrentState.sequence++;
1353 mCurrentState.layerStack = layerStack;
1354 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001355 setTransactionFlags(eTransactionNeeded);
1356 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001357}
1358
Peiyong Linc502cb72019-03-01 15:00:23 -08001359bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1360 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1361 return false;
1362 }
1363 mCurrentState.sequence++;
1364 mCurrentState.colorSpaceAgnostic = agnostic;
1365 mCurrentState.modified = true;
1366 setTransactionFlags(eTransactionNeeded);
1367 return true;
1368}
1369
Robert Carr1f0a16a2016-10-24 16:27:39 -07001370uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001371 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001372 if (p == nullptr) {
1373 return getDrawingState().layerStack;
1374 }
1375 return p->getLayerStack();
1376}
1377
Marissa Wallf58c14b2018-07-24 10:50:43 -07001378void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001379 mCurrentState.barrierLayer_legacy = barrierLayer;
1380 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001381 // We don't set eTransactionNeeded, because just receiving a deferral
1382 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001383 mCurrentState.modified = true;
1384 pushPendingState();
1385 mCurrentState.barrierLayer_legacy = nullptr;
1386 mCurrentState.frameNumber_legacy = 0;
1387 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001388}
1389
Marissa Wallf58c14b2018-07-24 10:50:43 -07001390void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001391 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001392 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001393}
1394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395// ----------------------------------------------------------------------------
1396// pageflip handling...
1397// ----------------------------------------------------------------------------
1398
Robert Carr1f0a16a2016-10-24 16:27:39 -07001399bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001400 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001401 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001402 if (parent != nullptr && parent->isHiddenByPolicy()) {
1403 return true;
1404 }
1405 return s.flags & layer_state_t::eLayerHidden;
1406}
1407
David Sodman41fdfc92017-11-06 16:09:56 -08001408uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001409 // TODO: should we do something special if mSecure is set?
1410 if (mProtectedByApp) {
1411 // need a hardware-protected path to external video sink
1412 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001413 }
Riley Andrews03414a12014-07-01 14:22:59 -07001414 if (mPotentialCursor) {
1415 usage |= GraphicBuffer::USAGE_CURSOR;
1416 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001417 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001418 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001419}
1420
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001421void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001422 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001423 // Disable setting transform hint if the debug flag is set or if the
1424 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1425 // in one orientation.
1426 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001427 // The transform hint is used to improve performance, but we can
1428 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001429 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001430 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001431 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001432 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001433 orientation = 0;
1434 }
1435 }
David Sodmaneb085e02017-10-05 18:49:04 -07001436 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001437}
1438
Mathias Agopian13127d82013-03-05 17:47:11 -08001439// ----------------------------------------------------------------------------
1440// debugging
1441// ----------------------------------------------------------------------------
1442
Marissa Wall61c58622018-07-18 10:12:20 -07001443// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001444LayerDebugInfo Layer::getLayerDebugInfo() const {
1445 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001446 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001447 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001448 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001449 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001450 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001451 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001452 info.mVisibleRegion = visibleRegion;
1453 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001454 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001455 info.mX = ds.active_legacy.transform.tx();
1456 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001457 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001458 info.mWidth = ds.active_legacy.w;
1459 info.mHeight = ds.active_legacy.h;
1460 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001461 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001462 info.mFlags = ds.flags;
1463 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001464 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001465 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1466 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1467 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1468 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001469 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001470 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001471 if (buffer != 0) {
1472 info.mActiveBufferWidth = buffer->getWidth();
1473 info.mActiveBufferHeight = buffer->getHeight();
1474 info.mActiveBufferStride = buffer->getStride();
1475 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001476 } else {
1477 info.mActiveBufferWidth = 0;
1478 info.mActiveBufferHeight = 0;
1479 info.mActiveBufferStride = 0;
1480 info.mActiveBufferFormat = 0;
1481 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001482 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001483 info.mNumQueuedFrames = getQueuedFrameCount();
1484 info.mRefreshPending = isBufferLatched();
1485 info.mIsOpaque = isOpaque(ds);
1486 info.mContentDirty = contentDirty;
1487 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001488}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001489
Yiwei Zhang5434a782018-12-05 18:06:32 -08001490void Layer::miniDumpHeader(std::string& result) {
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 result.append(" Layer name\n");
1495 result.append(" Z | ");
1496 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001497 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001498 result.append(" Disp Frame (LTRB) | ");
1499 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001500 result.append("-------------------------------");
1501 result.append("-------------------------------");
1502 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001503}
1504
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001505void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1506 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1507 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001508 return;
1509 }
1510
Yiwei Zhang5434a782018-12-05 18:06:32 -08001511 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001512 if (mName.length() > 77) {
1513 std::string shortened;
1514 shortened.append(mName.string(), 36);
1515 shortened.append("[...]");
1516 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001517 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001518 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001519 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001520 }
1521
Yiwei Zhang5434a782018-12-05 18:06:32 -08001522 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001523
Alec Mourib416efd2018-09-06 21:01:59 +00001524 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001525 const auto& compositionState = outputLayer->getState();
1526
Chia-I Wu1e043612018-03-01 09:45:09 -08001527 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001528 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001529 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001530 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001531 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001532 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1533 StringAppendF(&result, "%10s | ",
1534 toString(getCompositionLayer() ? compositionState.bufferTransform
1535 : static_cast<Hwc2::Transform>(0))
1536 .c_str());
1537 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001538 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001539 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001540 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1541 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001542
Yichi Chen6ca35192018-05-29 12:20:43 +08001543 result.append("- - - - - - - - - - - - - - - -");
1544 result.append("- - - - - - - - - - - - - - - -");
1545 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001546}
Dan Stozae22aec72016-08-01 13:20:59 -07001547
Yiwei Zhang5434a782018-12-05 18:06:32 -08001548void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001549 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001550}
1551
Svetoslavd85084b2014-03-20 10:28:31 -07001552void Layer::clearFrameStats() {
1553 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001554}
1555
Jamie Gennis6547ff42013-07-16 20:12:42 -07001556void Layer::logFrameStats() {
1557 mFrameTracker.logAndResetStats(mName);
1558}
1559
Svetoslavd85084b2014-03-20 10:28:31 -07001560void Layer::getFrameStats(FrameStats* outStats) const {
1561 mFrameTracker.getStats(outStats);
1562}
1563
Yiwei Zhang5434a782018-12-05 18:06:32 -08001564void Layer::dumpFrameEvents(std::string& result) {
1565 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001566 Mutex::Autolock lock(mFrameEventHistoryMutex);
1567 mFrameEventHistory.checkFencesForCompletion();
1568 mFrameEventHistory.dump(result);
1569}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001570
Brian Anderson5ea5e592016-12-01 16:54:33 -08001571void Layer::onDisconnect() {
1572 Mutex::Autolock lock(mFrameEventHistoryMutex);
1573 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001574 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001575}
1576
Brian Anderson3890c392016-07-25 12:48:08 -07001577void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001578 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001579 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001580 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1581 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001582 }
1583
Brian Andersond6927fb2016-07-23 23:37:30 -07001584 Mutex::Autolock lock(mFrameEventHistoryMutex);
1585 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001586 // If there are any unsignaled fences in the aquire timeline at this
1587 // point, the previously queued frame hasn't been latched yet. Go ahead
1588 // and try to get the signal time here so the syscall is taken out of
1589 // the main thread's critical path.
1590 mAcquireTimeline.updateSignalTimes();
1591 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001592 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001593 mFrameEventHistory.addQueue(*newTimestamps);
1594 }
1595
Brian Anderson3890c392016-07-25 12:48:08 -07001596 if (outDelta) {
1597 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001598 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001599}
Dan Stozae77c7662016-05-13 11:37:28 -07001600
Chia-I Wu98f1c102017-05-30 14:54:08 -07001601size_t Layer::getChildrenCount() const {
1602 size_t count = 0;
1603 for (const sp<Layer>& child : mCurrentChildren) {
1604 count += 1 + child->getChildrenCount();
1605 }
1606 return count;
1607}
1608
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001610 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001611 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001612
Robert Carr1f0a16a2016-10-24 16:27:39 -07001613 mCurrentChildren.add(layer);
1614 layer->setParent(this);
1615}
1616
1617ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001618 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001619 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001620
Robert Carr1323c952019-01-28 18:13:27 -08001621 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001622 return mCurrentChildren.remove(layer);
1623}
1624
Robert Carr1db73f62016-12-21 12:58:51 -08001625bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1626 sp<Handle> handle = nullptr;
1627 sp<Layer> newParent = nullptr;
1628 if (newParentHandle == nullptr) {
1629 return false;
1630 }
1631 handle = static_cast<Handle*>(newParentHandle.get());
1632 newParent = handle->owner.promote();
1633 if (newParent == nullptr) {
1634 ALOGE("Unable to promote Layer handle");
1635 return false;
1636 }
1637
chaviw5aedec92018-10-22 10:40:38 -07001638 if (attachChildren()) {
1639 setTransactionFlags(eTransactionNeeded);
1640 }
Robert Carr1db73f62016-12-21 12:58:51 -08001641 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001642 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001643 }
1644 mCurrentChildren.clear();
1645
1646 return true;
1647}
1648
Robert Carr15eae092018-03-23 13:43:53 -07001649void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001650 for (const sp<Layer>& child : mDrawingChildren) {
1651 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001652 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001653 }
1654}
1655
chaviwf1961f72017-09-18 16:41:07 -07001656bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001657 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001658
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001659 // While layers are detached, we allow most operations
1660 // and simply halt performing the actual transaction. However
1661 // for reparent != null we would enter the mRemovedFromCurrentState
1662 // state, regardless of whether doTransaction was called, and
1663 // so we need to prevent the update here.
1664 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001665 return false;
1666 }
1667
Robert Carr54cf5b12019-01-25 14:02:28 -08001668 sp<Layer> newParent;
1669 if (newParentHandle != nullptr) {
1670 auto handle = static_cast<Handle*>(newParentHandle.get());
1671 newParent = handle->owner.promote();
1672 if (newParent == nullptr) {
1673 ALOGE("Unable to promote Layer handle");
1674 return false;
1675 }
1676 if (newParent == this) {
1677 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1678 return false;
1679 }
1680 }
1681
chaviwf1961f72017-09-18 16:41:07 -07001682 sp<Layer> parent = getParent();
1683 if (parent != nullptr) {
1684 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001685 }
1686
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001687 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001688 newParent->addChild(this);
1689 if (!newParent->isRemovedFromCurrentState()) {
1690 addToCurrentState();
1691 } else {
1692 onRemovedFromCurrentState();
1693 }
1694
1695 if (mLayerDetached) {
1696 mLayerDetached = false;
1697 callSetTransactionFlags = true;
1698 }
1699 } else {
1700 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001701 }
1702
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001703 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001704 setTransactionFlags(eTransactionNeeded);
1705 }
chaviw06178942017-07-27 10:25:59 -07001706 return true;
1707}
1708
Robert Carr9524cb32017-02-13 11:32:32 -08001709bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001710 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001711 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001712 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001713 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001714 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001715 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001716 }
Robert Carr7f619b22017-11-06 12:56:35 -08001717 }
Robert Carr9524cb32017-02-13 11:32:32 -08001718
1719 return true;
1720}
1721
chaviw5aedec92018-10-22 10:40:38 -07001722bool Layer::attachChildren() {
1723 bool changed = false;
1724 for (const sp<Layer>& child : mCurrentChildren) {
1725 sp<Client> parentClient = mClientRef.promote();
1726 sp<Client> client(child->mClientRef.promote());
1727 if (client != nullptr && parentClient != client) {
1728 if (child->mLayerDetached) {
1729 child->mLayerDetached = false;
1730 changed = true;
1731 }
1732 changed |= child->attachChildren();
1733 }
1734 }
1735
1736 return changed;
1737}
1738
Peiyong Lind3788632018-09-18 16:01:31 -07001739bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001740 static const mat4 identityMatrix = mat4();
1741
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001742 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001743 return false;
1744 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001745 ++mCurrentState.sequence;
1746 mCurrentState.colorTransform = matrix;
1747 mCurrentState.hasColorTransform = matrix != identityMatrix;
1748 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001749 setTransactionFlags(eTransactionNeeded);
1750 return true;
1751}
1752
chaviwf66724d2018-11-28 16:35:21 -08001753mat4 Layer::getColorTransform() const {
1754 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1755 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1756 colorTransform = parent->getColorTransform() * colorTransform;
1757 }
1758 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001759}
1760
1761bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001762 bool hasColorTransform = getDrawingState().hasColorTransform;
1763 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1764 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1765 }
1766 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001767}
1768
Chia-I Wu11481472018-05-04 10:43:19 -07001769bool Layer::isLegacyDataSpace() const {
1770 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001771 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001772 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001773}
1774
Robert Carr1f0a16a2016-10-24 16:27:39 -07001775void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001776 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001777}
1778
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001780 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781}
1782
Robert Carr29abff82017-12-04 13:51:20 -08001783bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1784 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001785 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001786 return state.zOrderRelativeOf != nullptr;
1787}
1788
David Sodman41fdfc92017-11-06 16:09:56 -08001789__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001790 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001791 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1792 "makeTraversalList received invalid stateSet");
1793 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1794 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001795 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001796
Robert Carr29abff82017-12-04 13:51:20 -08001797 if (state.zOrderRelatives.size() == 0) {
1798 *outSkipRelativeZUsers = true;
1799 return children;
1800 }
1801
chaviwfd462612018-05-31 16:11:27 -07001802 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001803 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001804 sp<Layer> strongRelative = weakRelative.promote();
1805 if (strongRelative != nullptr) {
1806 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001807 }
1808 }
1809
Dan Stoza412903f2017-04-27 13:42:17 -07001810 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001811 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001812 if (childState.zOrderRelativeOf != nullptr) {
1813 continue;
1814 }
Robert Carrdb66e622017-04-10 16:55:57 -07001815 traverse.add(child);
1816 }
1817
1818 return traverse;
1819}
1820
Robert Carr1f0a16a2016-10-24 16:27:39 -07001821/**
Robert Carrdb66e622017-04-10 16:55:57 -07001822 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823 */
Dan Stoza412903f2017-04-27 13:42:17 -07001824void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001825 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1826 // produce a new list for traversing, including our relatives, and not including our children
1827 // who are relatives of another surface. In the case that there are no relative Z,
1828 // makeTraversalList returns our children directly to avoid significant overhead.
1829 // However in this case we need to take the responsibility for filtering children which
1830 // are relatives of another surface here.
1831 bool skipRelativeZUsers = false;
1832 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001833
Robert Carr1f0a16a2016-10-24 16:27:39 -07001834 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001835 for (; i < list.size(); i++) {
1836 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001837 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1838 continue;
1839 }
1840
Robert Carrdb66e622017-04-10 16:55:57 -07001841 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001842 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001843 }
Dan Stoza412903f2017-04-27 13:42:17 -07001844 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001845 }
Robert Carr29abff82017-12-04 13:51:20 -08001846
Dan Stoza412903f2017-04-27 13:42:17 -07001847 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001848 for (; i < list.size(); i++) {
1849 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001850
Robert Carr29abff82017-12-04 13:51:20 -08001851 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1852 continue;
1853 }
Dan Stoza412903f2017-04-27 13:42:17 -07001854 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001855 }
1856}
1857
1858/**
Robert Carrdb66e622017-04-10 16:55:57 -07001859 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001860 */
Dan Stoza412903f2017-04-27 13:42:17 -07001861void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1862 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001863 // See traverseInZOrder for documentation.
1864 bool skipRelativeZUsers = false;
1865 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001866
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001868 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001869 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001870
1871 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1872 continue;
1873 }
1874
Robert Carrdb66e622017-04-10 16:55:57 -07001875 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001876 break;
1877 }
Dan Stoza412903f2017-04-27 13:42:17 -07001878 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001879 }
Dan Stoza412903f2017-04-27 13:42:17 -07001880 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001881 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001882 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001883
1884 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1885 continue;
1886 }
1887
Dan Stoza412903f2017-04-27 13:42:17 -07001888 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889 }
1890}
1891
chaviw4b129c22018-04-09 16:19:43 -07001892LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1893 const std::vector<Layer*>& layersInTree) {
1894 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1895 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001896 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1897 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001898 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001899
chaviwfd462612018-05-31 16:11:27 -07001900 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001901 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1902 sp<Layer> strongRelative = weakRelative.promote();
1903 // Only add relative layers that are also descendents of the top most parent of the tree.
1904 // If a relative layer is not a descendent, then it should be ignored.
1905 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1906 traverse.add(strongRelative);
1907 }
1908 }
1909
1910 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001911 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001912 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1913 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1914 // the child here since it won't be added later as a relative.
1915 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1916 childState.zOrderRelativeOf.promote().get())) {
1917 continue;
1918 }
1919 traverse.add(child);
1920 }
1921
1922 return traverse;
1923}
1924
1925void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1926 LayerVector::StateSet stateSet,
1927 const LayerVector::Visitor& visitor) {
1928 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001929
1930 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001931 for (; i < list.size(); i++) {
1932 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001933 if (relative->getZ() >= 0) {
1934 break;
1935 }
chaviw4b129c22018-04-09 16:19:43 -07001936 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001937 }
chaviw4b129c22018-04-09 16:19:43 -07001938
chaviwa76b2712017-09-20 12:02:26 -07001939 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001940 for (; i < list.size(); i++) {
1941 const auto& relative = list[i];
1942 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001943 }
1944}
1945
chaviw4b129c22018-04-09 16:19:43 -07001946std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1947 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1948 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1949
1950 std::vector<Layer*> layersInTree = {this};
1951 for (size_t i = 0; i < children.size(); i++) {
1952 const auto& child = children[i];
1953 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1954 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1955 }
1956
1957 return layersInTree;
1958}
1959
1960void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1961 const LayerVector::Visitor& visitor) {
1962 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1963 std::sort(layersInTree.begin(), layersInTree.end());
1964 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1965}
1966
Peiyong Linefefaac2018-08-17 12:27:51 -07001967ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001968 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001969}
1970
chaviw13fdc492017-06-27 12:40:18 -07001971half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001972 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001973
chaviw13fdc492017-06-27 12:40:18 -07001974 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1975 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001976}
Robert Carr6452f122017-03-21 10:41:29 -07001977
chaviw13fdc492017-06-27 12:40:18 -07001978half4 Layer::getColor() const {
1979 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001980 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001981}
Robert Carr6452f122017-03-21 10:41:29 -07001982
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001983Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1984 const auto& p = mDrawingParent.promote();
1985 if (p != nullptr) {
1986 RoundedCornerState parentState = p->getRoundedCornerState();
1987 if (parentState.radius > 0) {
1988 ui::Transform t = getActiveTransform(getDrawingState());
1989 t = t.inverse();
1990 parentState.cropRect = t.transform(parentState.cropRect);
1991 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1992 // but a transform matrix can define horizontal and vertical scales.
1993 // Let's take the average between both of them and pass into the shader, practically we
1994 // never do this type of transformation on windows anyway.
1995 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1996 return parentState;
1997 }
1998 }
1999 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002000 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002001}
2002
Robert Carr1f0a16a2016-10-24 16:27:39 -07002003void Layer::commitChildList() {
2004 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2005 const auto& child = mCurrentChildren[i];
2006 child->commitChildList();
2007 }
2008 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002009 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002010}
2011
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002012static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2013 if (weakBinderHandle == nullptr) {
2014 return nullptr;
2015 }
2016 sp<IBinder> binderHandle = weakBinderHandle.promote();
2017 if (binderHandle == nullptr) {
2018 return nullptr;
2019 }
2020 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2021 if (handle == nullptr) {
2022 return nullptr;
2023 }
2024 return handle->owner;
2025}
2026
Robert Carr720e5062018-07-30 17:45:14 -07002027void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002028 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002029 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002030 mCurrentState.modified = true;
2031 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002032 setTransactionFlags(eTransactionNeeded);
2033}
2034
chaviw1d044282017-09-27 12:19:28 -07002035void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2036 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2037 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002038 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002039
Peiyong Linefefaac2018-08-17 12:27:51 -07002040 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002041 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002042
2043 layerInfo->set_id(sequence);
2044 layerInfo->set_name(getName().c_str());
2045 layerInfo->set_type(String8(getTypeId()));
2046
2047 for (const auto& child : children) {
2048 layerInfo->add_children(child->sequence);
2049 }
2050
2051 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2052 sp<Layer> strongRelative = weakRelative.promote();
2053 if (strongRelative != nullptr) {
2054 layerInfo->add_relatives(strongRelative->sequence);
2055 }
2056 }
2057
Marissa Wallf58c14b2018-07-24 10:50:43 -07002058 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08002059 [&]() { return layerInfo->mutable_transparent_region(); });
2060 LayerProtoHelper::writeToProto(visibleRegion,
2061 [&]() { return layerInfo->mutable_visible_region(); });
2062 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2063 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07002064
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002065 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002066 layerInfo->set_z(state.z);
2067
Nataniel Borges797b0e42019-02-15 14:11:58 -08002068 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2069 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07002070
Nataniel Borges797b0e42019-02-15 14:11:58 -08002071 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2072 return layerInfo->mutable_requested_position();
2073 });
chaviw1d044282017-09-27 12:19:28 -07002074
Nataniel Borges797b0e42019-02-15 14:11:58 -08002075 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2076 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07002077
Nataniel Borges797b0e42019-02-15 14:11:58 -08002078 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002079 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002080
2081 layerInfo->set_is_opaque(isOpaque(state));
2082 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002083
2084 // XXX (b/79210409) mCurrentDataSpace is not protected
2085 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2086
chaviw1d044282017-09-27 12:19:28 -07002087 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08002088 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2089 LayerProtoHelper::writeToProto(state.color,
2090 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07002091 layerInfo->set_flags(state.flags);
2092
2093 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2094 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2095
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002096 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002097 if (parent != nullptr) {
2098 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002099 } else {
2100 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002101 }
2102
2103 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2104 if (zOrderRelativeOf != nullptr) {
2105 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002106 } else {
2107 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002108 }
2109
Lloyd Pique0b785d82018-12-04 17:25:27 -08002110 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002111 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08002112 LayerProtoHelper::writeToProto(buffer,
2113 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07002114 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002115 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002116 }
2117
2118 layerInfo->set_queued_frames(getQueuedFrameCount());
2119 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002120 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002121 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002122
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002123 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002124 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002125 if (barrierLayer != nullptr) {
2126 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2127 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002128 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002129 }
2130 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002131
2132 auto protoMap = layerInfo->mutable_metadata();
2133 for (const auto& entry : state.metadata.mMap) {
2134 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2135 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002136 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08002137 LayerProtoHelper::writeToProto(mSourceBounds,
2138 [&]() { return layerInfo->mutable_source_bounds(); });
2139 LayerProtoHelper::writeToProto(mScreenBounds,
2140 [&]() { return layerInfo->mutable_screen_bounds(); });
2141 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07002142}
2143
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002144void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2145 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2146 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002147 return;
2148 }
2149
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002150 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2151
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002152 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002153
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002154 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002155 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002156
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002157 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002158 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002159
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002160 const int32_t transform =
2161 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002162 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002163
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002164 const int32_t compositionType =
2165 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2166 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002167 layerInfo->set_hwc_composition_type(compositionType);
2168
2169 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2170 static_cast<BufferLayer*>(this)->isProtected()) {
2171 layerInfo->set_is_protected(true);
2172 } else {
2173 layerInfo->set_is_protected(false);
2174 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002175}
2176
Robert Carr2e102c92018-10-23 12:11:15 -07002177bool Layer::isRemovedFromCurrentState() const {
2178 return mRemovedFromCurrentState;
2179}
2180
Arthur Hungd20b2702019-01-14 18:16:16 +08002181InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002182 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002183
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002184 if (info.displayId == ADISPLAY_ID_NONE) {
2185 info.displayId = mDrawingState.layerStack;
2186 }
2187
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002188 ui::Transform t = getTransform();
2189 const float xScale = t.sx();
2190 const float yScale = t.sy();
2191 if (xScale != 1.0f || yScale != 1.0f) {
2192 info.windowXScale *= 1.0f / xScale;
2193 info.windowYScale *= 1.0f / yScale;
2194 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002195 }
Robert Carre07e1032018-11-26 12:55:53 -08002196
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002197 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002198 // If this is a portal window, set the touchableRegion to the layerBounds.
2199 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2200 ? getBufferSize(getDrawingState())
2201 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002202 if (!layerBounds.isValid()) {
2203 layerBounds = getCroppedBufferSize(getDrawingState());
2204 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002205 layerBounds = t.transform(layerBounds);
2206 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2207
Arthur Hungd20b2702019-01-14 18:16:16 +08002208 // Input coordinate should match the layer bounds.
2209 info.frameLeft = layerBounds.left;
2210 info.frameTop = layerBounds.top;
2211 info.frameRight = layerBounds.right;
2212 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002213
2214 // Position the touchable region relative to frame screen location and restrict it to frame
2215 // bounds.
2216 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002217 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002218
2219 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2220 if (info.replaceTouchableRegionWithCrop) {
2221 if (cropLayer == nullptr) {
2222 info.touchableRegion = Region(Rect{mScreenBounds});
2223 } else {
2224 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2225 }
2226 } else if (cropLayer != nullptr) {
2227 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2228 }
2229
Robert Carr720e5062018-07-30 17:45:14 -07002230 return info;
2231}
2232
2233bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002234 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002235}
2236
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002237std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2238 return nullptr;
2239}
2240
chaviw3e727cd2019-01-31 13:41:05 -08002241bool Layer::canReceiveInput() const {
2242 return isVisible();
2243}
2244
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002245compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2246 const sp<const DisplayDevice>& display) const {
2247 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2248}
2249
Mathias Agopian13127d82013-03-05 17:47:11 -08002250// ---------------------------------------------------------------------------
2251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002252}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002253
2254#if defined(__gl_h_)
2255#error "don't include gl/gl.h in this file"
2256#endif
2257
2258#if defined(__gl2_h_)
2259#error "don't include gl2/gl2.h in this file"
2260#endif