blob: bdc2fa9c42c51acad879b00cef0c5aa4f7241bfd [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
Marissa Wall290ad082019-03-06 13:23:47 -0800289 // If the layer is a buffer state layer, the active width and height
290 // could be infinite. In that case, return the effective transform.
291 const uint32_t activeWidth = getActiveWidth(getDrawingState());
292 const uint32_t activeHeight = getActiveHeight(getDrawingState());
293 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
294 return mEffectiveTransform;
295 }
296
Vishnu Nair4351ad52019-02-11 14:13:02 -0800297 int bufferWidth;
298 int bufferHeight;
299 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800300 bufferWidth = mActiveBuffer->getWidth();
301 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800302 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800303 bufferHeight = mActiveBuffer->getWidth();
304 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800305 }
Marissa Wall290ad082019-03-06 13:23:47 -0800306 float sx = activeWidth / static_cast<float>(bufferWidth);
307 float sy = activeHeight / static_cast<float>(bufferHeight);
308
Vishnu Nair4351ad52019-02-11 14:13:02 -0800309 ui::Transform extraParentScaling;
310 extraParentScaling.set(sx, 0, 0, sy);
311 return mEffectiveTransform * extraParentScaling;
312}
313
314void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
315 const State& s(getDrawingState());
316
317 // Calculate effective layer transform
318 mEffectiveTransform = parentTransform * getActiveTransform(s);
319
320 // Transform parent bounds to layer space
321 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
322
323 // Calculate display frame
324 mSourceBounds = computeSourceBounds(parentBounds);
325
326 // Calculate bounds by croping diplay frame with layer crop and parent bounds
327 FloatRect bounds = mSourceBounds;
328 const Rect layerCrop = getCrop(s);
329 if (!layerCrop.isEmpty()) {
330 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
331 }
332 bounds = bounds.intersect(parentBounds);
333
334 mBounds = bounds;
335 mScreenBounds = mEffectiveTransform.transform(mBounds);
336 for (const sp<Layer>& child : mDrawingChildren) {
337 child->computeBounds(mBounds, getTransformWithScale());
338 }
339}
340
Robert Carrb5d3d262016-03-25 15:08:13 -0700341
Robert Carr1f0a16a2016-10-24 16:27:39 -0700342
Vishnu Nair60356342018-11-13 13:00:45 -0800343Rect Layer::getCroppedBufferSize(const State& s) const {
344 Rect size = getBufferSize(s);
345 Rect crop = getCrop(s);
346 if (!crop.isEmpty() && size.isValid()) {
347 size.intersect(crop, &size);
348 } else if (!crop.isEmpty()) {
349 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350 }
Vishnu Nair60356342018-11-13 13:00:45 -0800351 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800352}
353
Alec Mouri1f3bd182019-01-24 15:20:18 +0000354Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700355 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800356 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700357 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800358
359 // apply the projection's clipping to the window crop in
360 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700361 // if there are no window scaling involved, this operation will map to full
362 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700363
Vishnu Nair4351ad52019-02-11 14:13:02 -0800364 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000365 ui::Transform t = getTransform();
366 // Transform to screen space.
367 activeCropFloat = t.transform(activeCropFloat);
368 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
369 // Back to layer space to work with the content crop.
370 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800371 // This needs to be here as transform.transform(Rect) computes the
372 // transformed rect and then takes the bounding box of the result before
373 // returning. This means
374 // transform.inverse().transform(transform.transform(Rect)) != Rect
375 // in which case we need to make sure the final rect is clipped to the
376 // display bounds.
377 Rect activeCrop{activeCropFloat};
378 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000379 activeCrop.clear();
380 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700381 return activeCrop;
382}
383
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700384void Layer::setupRoundedCornersCropCoordinates(Rect win,
385 const FloatRect& roundedCornersCrop) const {
386 // Translate win by the rounded corners rect coordinates, to have all values in
387 // layer coordinate space.
388 win.left -= roundedCornersCrop.left;
389 win.right -= roundedCornersCrop.left;
390 win.top -= roundedCornersCrop.top;
391 win.bottom -= roundedCornersCrop.top;
392
Lloyd Pique0b785d82018-12-04 17:25:27 -0800393 renderengine::Mesh::VertexArray<vec2> cropCoords(
394 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700395 cropCoords[0] = vec2(win.left, win.top);
396 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
397 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
398 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700399}
400
Alec Mouri1f3bd182019-01-24 15:20:18 +0000401FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700402 // the content crop is the area of the content that gets scaled to the
403 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800404 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700405
406 // In addition there is a WM-specified crop we pull from our drawing state.
407 const State& s(getDrawingState());
408
Alec Mouri1f3bd182019-01-24 15:20:18 +0000409 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800410 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700411
Marissa Wall290ad082019-03-06 13:23:47 -0800412 int32_t winWidth = bufferSize.getWidth();
413 int32_t winHeight = bufferSize.getHeight();
414
415 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1]) if display frame
416 // hasn't been set and the parent is an unbounded layer.
417 if (winWidth < 0 && winHeight < 0) {
418 return crop;
419 }
420
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000421 // Transform the window crop to match the buffer coordinate system,
422 // which means using the inverse of the current transform set on the
423 // SurfaceFlingerConsumer.
424 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800425 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700427 * the code below applies the primary display's inverse transform to the
428 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 */
David Sodman41fdfc92017-11-06 16:09:56 -0800430 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431 // calculate the inverse transform
432 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800433 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800434 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700436 invTransform = (ui::Transform(invTransformOrient) *
437 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800438 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
441 // If the activeCrop has been rotate the ends are rotated but not
442 // the space itself so when transforming ends back we can't rely on
443 // a modification of the axes of rotation. To account for this we
444 // need to reorient the inverse rotation in terms of the current
445 // axes of rotation.
446 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
447 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
448 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800449 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800451 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800453 const Rect winCrop =
454 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455
456 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800457 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000458 float yScale = crop.getHeight() / float(winHeight);
459
David Sodman41fdfc92017-11-06 16:09:56 -0800460 float insetL = winCrop.left * xScale;
461 float insetT = winCrop.top * yScale;
462 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463 float insetB = (winHeight - winCrop.bottom) * yScale;
464
David Sodman41fdfc92017-11-06 16:09:56 -0800465 crop.left += insetL;
466 crop.top += insetT;
467 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468 crop.bottom -= insetB;
469
Mathias Agopian13127d82013-03-05 17:47:11 -0800470 return crop;
471}
472
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700473void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800474 const auto outputLayer = findOutputLayerForDisplay(display);
475 LOG_FATAL_IF(!outputLayer);
476 LOG_FATAL_IF(!outputLayer->getState().hwc);
477 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700478
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800479 if (!hasHwcLayer(display)) {
480 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
481 display->getDebugName().c_str());
482 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800483 }
484
Lloyd Pique0b785d82018-12-04 17:25:27 -0800485 LOG_FATAL_IF(!getCompositionLayer());
486 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800487 auto& compositionState = outputLayer->editState();
488
489 // enable this layer
490 compositionState.forceClientComposition = false;
491
492 if (isSecure() && !display->isSecure()) {
493 compositionState.forceClientComposition = true;
494 }
David Sodman15094112018-10-11 09:39:37 -0700495
Mathias Agopian13127d82013-03-05 17:47:11 -0800496 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700497 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800498 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500499 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800500 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800501 blendMode =
502 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503 }
David Sodman15094112018-10-11 09:39:37 -0700504 auto error = hwcLayer->setBlendMode(blendMode);
505 ALOGE_IF(error != HWC2::Error::None,
506 "[%s] Failed to set blend mode %s:"
507 " %s (%d)",
508 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
509 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800510 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800511
512 // apply the layer's transform, followed by the display's global transform
513 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700514 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800515 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700516 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800517 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700518 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700519 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000520 activeCrop.clear();
521 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700522 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800523 // This needs to be here as transform.transform(Rect) computes the
524 // transformed rect and then takes the bounding box of the result before
525 // returning. This means
526 // transform.inverse().transform(transform.transform(Rect)) != Rect
527 // in which case we need to make sure the final rect is clipped to the
528 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800529 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000530 activeCrop.clear();
531 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700532 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800533 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
534 activeTransparentRegion.orSelf(
535 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800536 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
537 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800538 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700539 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700540
Vishnu Nair4351ad52019-02-11 14:13:02 -0800541 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800542 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800543 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700544 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000545 frame.clear();
546 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700547 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800548 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700549 error = hwcLayer->setDisplayFrame(transformedFrame);
550 if (error != HWC2::Error::None) {
551 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
552 transformedFrame.left, transformedFrame.top, transformedFrame.right,
553 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
554 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800555 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700556 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700558 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700559 error = hwcLayer->setSourceCrop(sourceCrop);
560 if (error != HWC2::Error::None) {
561 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
562 "%s (%d)",
563 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
564 to_string(error).c_str(), static_cast<int32_t>(error));
565 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800566 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700567 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800569 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700570 error = hwcLayer->setPlaneAlpha(alpha);
571 ALOGE_IF(error != HWC2::Error::None,
572 "[%s] Failed to set plane alpha %.3f: "
573 "%s (%d)",
574 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800575 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576
David Sodman15094112018-10-11 09:39:37 -0700577 error = hwcLayer->setZOrder(z);
578 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
579 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800580 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500581
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800582 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
583 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700584 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400585 if (parent.get()) {
586 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800587 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
588 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
589 if (parentType >= 0 || parentAppId >= 0) {
590 type = parentType;
591 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700592 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400593 }
594
David Sodman15094112018-10-11 09:39:37 -0700595 error = hwcLayer->setInfo(type, appId);
596 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
597 static_cast<int32_t>(error));
598
Lloyd Pique0b785d82018-12-04 17:25:27 -0800599 commonCompositionState.type = type;
600 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700601
Mathias Agopian29a367b2011-07-12 14:51:45 -0700602 /*
603 * Transformations are applied in this order:
604 * 1) buffer orientation/flip/mirror
605 * 2) state transformation (window manager)
606 * 3) layer orientation (screen orientation)
607 * (NOTE: the matrices are multiplied in reverse order)
608 */
609
Peiyong Linefefaac2018-08-17 12:27:51 -0700610 const ui::Transform bufferOrientation(mCurrentTransform);
611 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700612
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800613 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700614 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700615 * the code below applies the primary display's inverse transform to the
616 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617 */
David Sodman41fdfc92017-11-06 16:09:56 -0800618 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619 // calculate the inverse transform
620 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800621 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700622 }
Robert Carrcae605c2017-03-29 12:10:31 -0700623
624 /*
625 * Here we cancel out the orientation component of the WM transform.
626 * The scaling and translate components are already included in our bounds
627 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000628 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700629 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700630 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700631 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700632
633 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800634 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700635 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800637 compositionState.forceClientComposition = true;
638 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639 } else {
640 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700641 auto error = hwcLayer->setTransform(transform);
642 ALOGE_IF(error != HWC2::Error::None,
643 "[%s] Failed to set transform %s: "
644 "%s (%d)",
645 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
646 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800647 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800648 }
649}
650
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800651void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
652 const auto outputLayer = findOutputLayerForDisplay(display);
653 LOG_FATAL_IF(!outputLayer);
654 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800655}
Dan Stozaee44edd2015-03-23 15:50:23 -0700656
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800657bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
658 const auto outputLayer = findOutputLayerForDisplay(display);
659 LOG_FATAL_IF(!outputLayer);
660 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800661}
662
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700663void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800664 const auto outputLayer = findOutputLayerForDisplay(display);
665 LOG_FATAL_IF(!outputLayer);
666
667 if (!outputLayer->getState().hwc ||
668 (*outputLayer->getState().hwc).hwcCompositionType !=
669 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670 return;
671 }
672
673 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800674 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675
676 // Apply the layer's transform, followed by the display's global transform
677 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800678 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700680 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800681 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700682 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700683 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684 auto position = displayTransform.transform(frame);
685
Dominik Laskowski7e045462018-05-30 13:02:02 -0700686 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800687 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800688 ALOGE_IF(error != HWC2::Error::None,
689 "[%s] Failed to set cursor position "
690 "to (%d, %d): %s (%d)",
691 mName.string(), position.left, position.top, to_string(error).c_str(),
692 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800693}
Riley Andrews03414a12014-07-01 14:22:59 -0700694
Mathias Agopian13127d82013-03-05 17:47:11 -0800695// ---------------------------------------------------------------------------
696// drawing...
697// ---------------------------------------------------------------------------
698
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000699bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800700 Region& clearRegion, const bool supportProtectedContent,
701 renderengine::LayerSettings& layer) {
702 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800703}
704
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000705bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800706 Region& clearRegion, const bool supportProtectedContent,
707 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000708 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800709 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000710}
711
712bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
713 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800714 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000715 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800716 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000717 half alpha = getAlpha();
718 layer.geometry.boundaries = bounds;
719 if (useIdentityTransform) {
720 layer.geometry.positionTransform = mat4();
721 } else {
722 const ui::Transform transform = getTransform();
723 mat4 m;
724 m[0][0] = transform[0][0];
725 m[0][1] = transform[0][1];
726 m[0][3] = transform[0][2];
727 m[1][0] = transform[1][0];
728 m[1][1] = transform[1][1];
729 m[1][3] = transform[1][2];
730 m[3][0] = transform[2][0];
731 m[3][1] = transform[2][1];
732 m[3][3] = transform[2][2];
733 layer.geometry.positionTransform = m;
734 }
735
736 if (hasColorTransform()) {
737 layer.colorTransform = getColorTransform();
738 }
739
740 const auto roundedCornerState = getRoundedCornerState();
741 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
742 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
743
744 layer.alpha = alpha;
745 layer.sourceDataspace = mCurrentDataSpace;
746 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800747}
748
David Sodman41fdfc92017-11-06 16:09:56 -0800749void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
750 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800751 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800752 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700753 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800754 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800755}
756
David Sodmanc1498e62018-09-12 14:36:26 -0700757void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
758 clearWithOpenGL(renderArea, 0, 0, 0, 0);
759}
760
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800761void Layer::setCompositionType(const sp<const DisplayDevice>& display,
762 Hwc2::IComposerClient::Composition type) {
763 const auto outputLayer = findOutputLayerForDisplay(display);
764 LOG_FATAL_IF(!outputLayer);
765 LOG_FATAL_IF(!outputLayer->getState().hwc);
766 auto& compositionState = outputLayer->editState();
767
768 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
769 toString(type).c_str(), 1);
770 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700771 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800772 (*compositionState.hwc).hwcCompositionType = type;
773
774 auto error = (*compositionState.hwc)
775 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
776 ALOGE_IF(error != HWC2::Error::None,
777 "[%s] Failed to set "
778 "composition type %s: %s (%d)",
779 mName.string(), toString(type).c_str(), to_string(error).c_str(),
780 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781 }
782}
783
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800784Hwc2::IComposerClient::Composition Layer::getCompositionType(
785 const sp<const DisplayDevice>& display) const {
786 const auto outputLayer = findOutputLayerForDisplay(display);
787 LOG_FATAL_IF(!outputLayer);
788 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
789 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790}
791
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800792bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
793 const auto outputLayer = findOutputLayerForDisplay(display);
794 LOG_FATAL_IF(!outputLayer);
795 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800796}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797
Dan Stozacac35382016-01-27 12:21:06 -0800798bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
799 if (point->getFrameNumber() <= mCurrentFrameNumber) {
800 // Don't bother with a SyncPoint, since we've already latched the
801 // relevant frame
802 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700803 }
Robert Carr2e102c92018-10-23 12:11:15 -0700804 if (isRemovedFromCurrentState()) {
805 return false;
806 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700807
Dan Stozacac35382016-01-27 12:21:06 -0800808 Mutex::Autolock lock(mLocalSyncPointMutex);
809 mLocalSyncPoints.push_back(point);
810 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700811}
812
Mathias Agopian13127d82013-03-05 17:47:11 -0800813// ----------------------------------------------------------------------------
814// local state
815// ----------------------------------------------------------------------------
816
Peiyong Lin833074a2018-08-28 11:53:54 -0700817void Layer::computeGeometry(const RenderArea& renderArea,
818 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700819 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700820 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800821 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700822
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000823 vec2 lt = vec2(win.left, win.top);
824 vec2 lb = vec2(win.left, win.bottom);
825 vec2 rb = vec2(win.right, win.bottom);
826 vec2 rt = vec2(win.right, win.top);
827
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800828 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000829 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700830 lt = layerTransform.transform(lt);
831 lb = layerTransform.transform(lb);
832 rb = layerTransform.transform(rb);
833 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000834 }
835
Peiyong Lin833074a2018-08-28 11:53:54 -0700836 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700837 position[0] = renderAreaTransform.transform(lt);
838 position[1] = renderAreaTransform.transform(lb);
839 position[2] = renderAreaTransform.transform(rb);
840 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800841}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800842
David Sodman41fdfc92017-11-06 16:09:56 -0800843bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800844 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700845 return (s.flags & layer_state_t::eLayerSecure);
846}
847
Mathias Agopian13127d82013-03-05 17:47:11 -0800848void Layer::setVisibleRegion(const Region& visibleRegion) {
849 // always called from main thread
850 this->visibleRegion = visibleRegion;
851}
852
853void Layer::setCoveredRegion(const Region& coveredRegion) {
854 // always called from main thread
855 this->coveredRegion = coveredRegion;
856}
857
David Sodman41fdfc92017-11-06 16:09:56 -0800858void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800859 // always called from main thread
860 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
861}
862
Robert Carre5f4f692018-01-12 13:12:28 -0800863void Layer::clearVisibilityRegions() {
864 visibleRegion.clear();
865 visibleNonTransparentRegion.clear();
866 coveredRegion.clear();
867}
868
Mathias Agopian13127d82013-03-05 17:47:11 -0800869// ----------------------------------------------------------------------------
870// transaction
871// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800872
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873void Layer::pushPendingState() {
874 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700875 return;
876 }
877
Dan Stoza7dde5992015-05-22 09:51:44 -0700878 // If this transaction is waiting on the receipt of a frame, generate a sync
879 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700880 // We don't allow installing sync points after we are removed from the current state
881 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800882 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
883 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800884 if (barrierLayer == nullptr) {
885 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700886 // If we can't promote the layer we are intended to wait on,
887 // then it is expired or otherwise invalid. Allow this transaction
888 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800890 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800892 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800893 mRemoteSyncPoints.push_back(std::move(syncPoint));
894 } else {
895 // We already missed the frame we're supposed to synchronize
896 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800898 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700899 }
900
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 // Wake us up to check if the frame has been received
902 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700903 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700904 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800905 mPendingStates.push_back(mCurrentState);
906 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700907}
908
Pablo Ceballos05289c22016-04-14 15:49:55 -0700909void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800910 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700911
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800912 mPendingStates.removeAt(0);
913 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700914}
915
Pablo Ceballos05289c22016-04-14 15:49:55 -0700916bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800918 while (!mPendingStates.empty()) {
919 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700920 if (mRemoteSyncPoints.empty()) {
921 // If we don't have a sync point for this, apply it anyway. It
922 // will be visually wrong, but it should keep us from getting
923 // into too much trouble.
924 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 stateUpdateAvailable = true;
927 continue;
928 }
929
Marissa Wallf58c14b2018-07-24 10:50:43 -0700930 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800932 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800933
934 // Signal our end of the sync point and then dispose of it
935 mRemoteSyncPoints.front()->setTransactionApplied();
936 mRemoteSyncPoints.pop_front();
937 continue;
938 }
939
Dan Stoza7dde5992015-05-22 09:51:44 -0700940 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
941 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700942 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700943 stateUpdateAvailable = true;
944
945 // Signal our end of the sync point and then dispose of it
946 mRemoteSyncPoints.front()->setTransactionApplied();
947 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800948 } else {
949 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700950 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700951 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700952 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700953 stateUpdateAvailable = true;
954 }
955 }
956
957 // If we still have pending updates, wake SurfaceFlinger back up and point
958 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800959 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 setTransactionFlags(eTransactionNeeded);
961 mFlinger->setTransactionFlags(eTraversalNeeded);
962 }
963
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800964 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700965 return stateUpdateAvailable;
966}
967
Marissa Wall61c58622018-07-18 10:12:20 -0700968uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000969 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970
Marissa Wall61c58622018-07-18 10:12:20 -0700971 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
972 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700973
David Sodmaneb085e02017-10-05 18:49:04 -0700974 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700975 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000976 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800977 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
978 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
979 " requested={ wh={%4u,%4u} }}\n"
980 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
981 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700982 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700983 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
984 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
985 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
986 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
987 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700988 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
989 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
990 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700992
Robert Carre392b552017-09-19 12:16:05 -0700993 // Don't let Layer::doTransaction update the drawing state
994 // if we have a pending resize, unless we are in fixed-size mode.
995 // the drawing state will be updated only once we receive a buffer
996 // with the correct size.
997 //
998 // In particular, we want to make sure the clip (which is part
999 // of the geometry state) is latched together with the size but is
1000 // latched immediately when no resizing is involved.
1001 //
1002 // If a sideband stream is attached, however, we want to skip this
1003 // optimization so that transactions aren't missed when a buffer
1004 // never arrives
1005 //
1006 // In the case that we don't have a buffer we ignore other factors
1007 // and avoid entering the resizePending state. At a high level the
1008 // resizePending state is to avoid applying the state of the new buffer
1009 // to the old buffer. However in the state where we don't have an old buffer
1010 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001011 const bool resizePending =
1012 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1013 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -08001014 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001015 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -08001016 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001017 flags |= eDontUpdateGeometryState;
1018 }
1019 }
1020
Robert Carr7bf247e2017-05-18 14:02:49 -07001021 // Here we apply various requested geometry states, depending on our
1022 // latching configuration. See Layer.h for a detailed discussion of
1023 // how geometry latching is controlled.
1024 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001025 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001026
1027 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1028 // mode, which causes attributes which normally latch regardless of scaling mode,
1029 // to be delayed. We copy the requested state to the active state making sure
1030 // to respect these rules (again see Layer.h for a detailed discussion).
1031 //
1032 // There is an awkward asymmetry in the handling of the crop states in the position
1033 // states, as can be seen below. Largely this arises from position and transform
1034 // being stored in the same data structure while having different latching rules.
1035 // b/38182305
1036 //
Marissa Wall61c58622018-07-18 10:12:20 -07001037 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001038 // applyPendingStates in the presence of deferred transactions.
1039 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001040 float tx = stateToCommit->active_legacy.transform.tx();
1041 float ty = stateToCommit->active_legacy.transform.ty();
1042 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1043 stateToCommit->active_legacy.transform.set(tx, ty);
1044 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001045 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001046 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001047 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001048 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001049 }
1050
Marissa Wall61c58622018-07-18 10:12:20 -07001051 return flags;
1052}
1053
1054uint32_t Layer::doTransaction(uint32_t flags) {
1055 ATRACE_CALL();
1056
chaviw5aedec92018-10-22 10:40:38 -07001057 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001058 return flags;
1059 }
1060
1061 if (mChildrenChanged) {
1062 flags |= eVisibleRegion;
1063 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001064 }
1065
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001066 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001067 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001068 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001069 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001070 }
1071
1072 flags = doTransactionResize(flags, &c);
1073
Alec Mourib416efd2018-09-06 21:01:59 +00001074 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001075
1076 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001077 // invalidate and recompute the visible regions if needed
1078 flags |= Layer::eVisibleRegion;
1079 }
1080
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001081 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001082 // invalidate and recompute the visible regions if needed
1083 flags |= eVisibleRegion;
1084 this->contentDirty = true;
1085
1086 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001087 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001088 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 }
1090
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001091 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001092 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001094 }
1095
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001097 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001098 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001099 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100}
1101
Pablo Ceballos05289c22016-04-14 15:49:55 -07001102void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001103 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001104}
1105
Mathias Agopian13127d82013-03-05 17:47:11 -08001106uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001107 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001108}
1109
1110uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001111 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001112}
1113
Robert Carr82364e32016-05-15 11:27:47 -07001114bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001115 if (mCurrentState.requested_legacy.transform.tx() == x &&
1116 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001118 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001119
1120 // We update the requested and active position simultaneously because
1121 // we want to apply the position portion of the transform matrix immediately,
1122 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001123 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001124 if (immediate && !mFreezeGeometryUpdates) {
1125 // Here we directly update the active state
1126 // unlike other setters, because we store it within
1127 // the transform, but use different latching rules.
1128 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001129 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001130 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001131 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001132
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001133 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001134 setTransactionFlags(eTransactionNeeded);
1135 return true;
1136}
Robert Carr82364e32016-05-15 11:27:47 -07001137
Robert Carr1f0a16a2016-10-24 16:27:39 -07001138bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1139 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1140 if (idx < 0) {
1141 return false;
1142 }
1143 if (childLayer->setLayer(z)) {
1144 mCurrentChildren.removeAt(idx);
1145 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001146 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001147 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001148 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001149}
1150
Robert Carr503c7042017-09-27 15:06:08 -07001151bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1152 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1153 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1154 if (idx < 0) {
1155 return false;
1156 }
1157 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1158 mCurrentChildren.removeAt(idx);
1159 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001160 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001161 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001162 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001163}
1164
Robert Carrae060832016-11-28 10:51:00 -08001165bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001166 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1167 mCurrentState.sequence++;
1168 mCurrentState.z = z;
1169 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001170
1171 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001172 if (mCurrentState.zOrderRelativeOf != nullptr) {
1173 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001174 if (strongRelative != nullptr) {
1175 strongRelative->removeZOrderRelative(this);
1176 }
chaviw606e5cf2019-03-01 10:12:10 -08001177 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001178 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001179 setTransactionFlags(eTransactionNeeded);
1180 return true;
1181}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001182
Robert Carrdb66e622017-04-10 16:55:57 -07001183void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001184 mCurrentState.zOrderRelatives.remove(relative);
1185 mCurrentState.sequence++;
1186 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001187 setTransactionFlags(eTransactionNeeded);
1188}
1189
1190void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001191 mCurrentState.zOrderRelatives.add(relative);
1192 mCurrentState.modified = true;
1193 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001194 setTransactionFlags(eTransactionNeeded);
1195}
1196
chaviw606e5cf2019-03-01 10:12:10 -08001197void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1198 mCurrentState.zOrderRelativeOf = relativeOf;
1199 mCurrentState.sequence++;
1200 mCurrentState.modified = true;
1201 setTransactionFlags(eTransactionNeeded);
1202}
1203
Robert Carr503d2bd2017-12-04 15:49:47 -08001204bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001205 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1206 if (handle == nullptr) {
1207 return false;
1208 }
1209 sp<Layer> relative = handle->owner.promote();
1210 if (relative == nullptr) {
1211 return false;
1212 }
1213
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001214 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1215 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001216 return false;
1217 }
1218
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001219 mCurrentState.sequence++;
1220 mCurrentState.modified = true;
1221 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001222
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001223 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001224 if (oldZOrderRelativeOf != nullptr) {
1225 oldZOrderRelativeOf->removeZOrderRelative(this);
1226 }
chaviw606e5cf2019-03-01 10:12:10 -08001227 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001228 relative->addZOrderRelative(this);
1229
1230 setTransactionFlags(eTransactionNeeded);
1231
1232 return true;
1233}
1234
Mathias Agopian13127d82013-03-05 17:47:11 -08001235bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001236 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001237 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001238 mCurrentState.requested_legacy.w = w;
1239 mCurrentState.requested_legacy.h = h;
1240 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001241 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001242
1243 // record the new size, from this point on, when the client request
1244 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001245 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001246 return true;
1247}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001248bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001249 if (mCurrentState.color.a == alpha) return false;
1250 mCurrentState.sequence++;
1251 mCurrentState.color.a = alpha;
1252 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001253 setTransactionFlags(eTransactionNeeded);
1254 return true;
1255}
chaviw13fdc492017-06-27 12:40:18 -07001256
Valerie Haudd0b7572019-01-29 14:59:27 -08001257bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1258 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001259 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001260 }
1261 mCurrentState.sequence++;
1262 mCurrentState.modified = true;
1263 setTransactionFlags(eTransactionNeeded);
1264
1265 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001266 // create background color layer if one does not yet exist
1267 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1268 const String8& name = mName + "BackgroundColorLayer";
1269 mCurrentState.bgColorLayer =
1270 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001271
Valerie Haudd0b7572019-01-29 14:59:27 -08001272 // add to child list
1273 addChild(mCurrentState.bgColorLayer);
1274 mFlinger->mLayersAdded = true;
1275 // set up SF to handle added color layer
1276 if (isRemovedFromCurrentState()) {
1277 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1278 }
1279 mFlinger->setTransactionFlags(eTransactionNeeded);
1280 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1281 mCurrentState.bgColorLayer->reparent(nullptr);
1282 mCurrentState.bgColorLayer = nullptr;
1283 return true;
1284 }
1285
1286 mCurrentState.bgColorLayer->setColor(color);
1287 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1288 mCurrentState.bgColorLayer->setAlpha(alpha);
1289 mCurrentState.bgColorLayer->setDataspace(dataspace);
1290
chaviw13fdc492017-06-27 12:40:18 -07001291 return true;
1292}
1293
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001294bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001295 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001296
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001297 mCurrentState.sequence++;
1298 mCurrentState.cornerRadius = cornerRadius;
1299 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001300 setTransactionFlags(eTransactionNeeded);
1301 return true;
1302}
1303
Robert Carrd4ae7f32018-06-07 16:10:57 -07001304bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1305 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001306 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001307 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1308
1309 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1310 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1311 return false;
1312 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001313 mCurrentState.sequence++;
1314 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1315 matrix.dsdy);
1316 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001317 setTransactionFlags(eTransactionNeeded);
1318 return true;
1319}
Marissa Wall61c58622018-07-18 10:12:20 -07001320
Mathias Agopian13127d82013-03-05 17:47:11 -08001321bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001322 mCurrentState.requestedTransparentRegion_legacy = transparent;
1323 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001324 setTransactionFlags(eTransactionNeeded);
1325 return true;
1326}
1327bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001328 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1329 if (mCurrentState.flags == newFlags) return false;
1330 mCurrentState.sequence++;
1331 mCurrentState.flags = newFlags;
1332 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001333 setTransactionFlags(eTransactionNeeded);
1334 return true;
1335}
Robert Carr99e27f02016-06-16 15:18:02 -07001336
Marissa Wallf58c14b2018-07-24 10:50:43 -07001337bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001338 if (mCurrentState.requestedCrop_legacy == crop) return false;
1339 mCurrentState.sequence++;
1340 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001341 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001342 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001343 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001344 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1345
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001346 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001347 setTransactionFlags(eTransactionNeeded);
1348 return true;
1349}
Robert Carr8d5227b2017-03-16 15:41:03 -07001350
Robert Carrc3574f72016-03-24 12:19:32 -07001351bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001352 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001353 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001354 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001355 return true;
1356}
1357
Evan Roskyef876c92019-01-25 17:46:06 -08001358bool Layer::setMetadata(const LayerMetadata& data) {
1359 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001360 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001361 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001362 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001363 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001364}
1365
Mathias Agopian13127d82013-03-05 17:47:11 -08001366bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001367 if (mCurrentState.layerStack == layerStack) return false;
1368 mCurrentState.sequence++;
1369 mCurrentState.layerStack = layerStack;
1370 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001371 setTransactionFlags(eTransactionNeeded);
1372 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001373}
1374
Peiyong Linc502cb72019-03-01 15:00:23 -08001375bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1376 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1377 return false;
1378 }
1379 mCurrentState.sequence++;
1380 mCurrentState.colorSpaceAgnostic = agnostic;
1381 mCurrentState.modified = true;
1382 setTransactionFlags(eTransactionNeeded);
1383 return true;
1384}
1385
Robert Carr1f0a16a2016-10-24 16:27:39 -07001386uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001387 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001388 if (p == nullptr) {
1389 return getDrawingState().layerStack;
1390 }
1391 return p->getLayerStack();
1392}
1393
Marissa Wallf58c14b2018-07-24 10:50:43 -07001394void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001395 mCurrentState.barrierLayer_legacy = barrierLayer;
1396 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001397 // We don't set eTransactionNeeded, because just receiving a deferral
1398 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001399 mCurrentState.modified = true;
1400 pushPendingState();
1401 mCurrentState.barrierLayer_legacy = nullptr;
1402 mCurrentState.frameNumber_legacy = 0;
1403 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001404}
1405
Marissa Wallf58c14b2018-07-24 10:50:43 -07001406void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001407 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001408 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001409}
1410
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411// ----------------------------------------------------------------------------
1412// pageflip handling...
1413// ----------------------------------------------------------------------------
1414
Robert Carr1f0a16a2016-10-24 16:27:39 -07001415bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001416 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001417 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001418 if (parent != nullptr && parent->isHiddenByPolicy()) {
1419 return true;
1420 }
1421 return s.flags & layer_state_t::eLayerHidden;
1422}
1423
David Sodman41fdfc92017-11-06 16:09:56 -08001424uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001425 // TODO: should we do something special if mSecure is set?
1426 if (mProtectedByApp) {
1427 // need a hardware-protected path to external video sink
1428 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001429 }
Riley Andrews03414a12014-07-01 14:22:59 -07001430 if (mPotentialCursor) {
1431 usage |= GraphicBuffer::USAGE_CURSOR;
1432 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001433 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001434 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001435}
1436
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001437void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001438 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001439 // Disable setting transform hint if the debug flag is set or if the
1440 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1441 // in one orientation.
1442 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001443 // The transform hint is used to improve performance, but we can
1444 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001445 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001446 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001447 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001448 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001449 orientation = 0;
1450 }
1451 }
David Sodmaneb085e02017-10-05 18:49:04 -07001452 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001453}
1454
Mathias Agopian13127d82013-03-05 17:47:11 -08001455// ----------------------------------------------------------------------------
1456// debugging
1457// ----------------------------------------------------------------------------
1458
Marissa Wall61c58622018-07-18 10:12:20 -07001459// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001460LayerDebugInfo Layer::getLayerDebugInfo() const {
1461 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001462 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001463 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001464 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001465 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001466 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001467 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001468 info.mVisibleRegion = visibleRegion;
1469 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001470 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001471 info.mX = ds.active_legacy.transform.tx();
1472 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001473 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001474 info.mWidth = ds.active_legacy.w;
1475 info.mHeight = ds.active_legacy.h;
1476 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001477 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001478 info.mFlags = ds.flags;
1479 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001480 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001481 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1482 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1483 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1484 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001485 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001486 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001487 if (buffer != 0) {
1488 info.mActiveBufferWidth = buffer->getWidth();
1489 info.mActiveBufferHeight = buffer->getHeight();
1490 info.mActiveBufferStride = buffer->getStride();
1491 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001492 } else {
1493 info.mActiveBufferWidth = 0;
1494 info.mActiveBufferHeight = 0;
1495 info.mActiveBufferStride = 0;
1496 info.mActiveBufferFormat = 0;
1497 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001498 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001499 info.mNumQueuedFrames = getQueuedFrameCount();
1500 info.mRefreshPending = isBufferLatched();
1501 info.mIsOpaque = isOpaque(ds);
1502 info.mContentDirty = contentDirty;
1503 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001504}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001505
Yiwei Zhang5434a782018-12-05 18:06:32 -08001506void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001507 result.append("-------------------------------");
1508 result.append("-------------------------------");
1509 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001510 result.append(" Layer name\n");
1511 result.append(" Z | ");
1512 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001513 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001514 result.append(" Disp Frame (LTRB) | ");
1515 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001516 result.append("-------------------------------");
1517 result.append("-------------------------------");
1518 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001519}
1520
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001521void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1522 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1523 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001524 return;
1525 }
1526
Yiwei Zhang5434a782018-12-05 18:06:32 -08001527 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001528 if (mName.length() > 77) {
1529 std::string shortened;
1530 shortened.append(mName.string(), 36);
1531 shortened.append("[...]");
1532 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001533 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001534 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001535 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001536 }
1537
Yiwei Zhang5434a782018-12-05 18:06:32 -08001538 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001539
Alec Mourib416efd2018-09-06 21:01:59 +00001540 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001541 const auto& compositionState = outputLayer->getState();
1542
Chia-I Wu1e043612018-03-01 09:45:09 -08001543 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001544 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001545 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001546 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001547 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001548 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1549 StringAppendF(&result, "%10s | ",
1550 toString(getCompositionLayer() ? compositionState.bufferTransform
1551 : static_cast<Hwc2::Transform>(0))
1552 .c_str());
1553 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001554 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001555 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001556 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1557 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001558
Yichi Chen6ca35192018-05-29 12:20:43 +08001559 result.append("- - - - - - - - - - - - - - - -");
1560 result.append("- - - - - - - - - - - - - - - -");
1561 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001562}
Dan Stozae22aec72016-08-01 13:20:59 -07001563
Yiwei Zhang5434a782018-12-05 18:06:32 -08001564void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001565 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001566}
1567
Svetoslavd85084b2014-03-20 10:28:31 -07001568void Layer::clearFrameStats() {
1569 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001570}
1571
Jamie Gennis6547ff42013-07-16 20:12:42 -07001572void Layer::logFrameStats() {
1573 mFrameTracker.logAndResetStats(mName);
1574}
1575
Svetoslavd85084b2014-03-20 10:28:31 -07001576void Layer::getFrameStats(FrameStats* outStats) const {
1577 mFrameTracker.getStats(outStats);
1578}
1579
Yiwei Zhang5434a782018-12-05 18:06:32 -08001580void Layer::dumpFrameEvents(std::string& result) {
1581 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001582 Mutex::Autolock lock(mFrameEventHistoryMutex);
1583 mFrameEventHistory.checkFencesForCompletion();
1584 mFrameEventHistory.dump(result);
1585}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001586
Brian Anderson5ea5e592016-12-01 16:54:33 -08001587void Layer::onDisconnect() {
1588 Mutex::Autolock lock(mFrameEventHistoryMutex);
1589 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001590 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001591}
1592
Brian Anderson3890c392016-07-25 12:48:08 -07001593void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001594 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001595 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001596 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1597 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001598 }
1599
Brian Andersond6927fb2016-07-23 23:37:30 -07001600 Mutex::Autolock lock(mFrameEventHistoryMutex);
1601 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001602 // If there are any unsignaled fences in the aquire timeline at this
1603 // point, the previously queued frame hasn't been latched yet. Go ahead
1604 // and try to get the signal time here so the syscall is taken out of
1605 // the main thread's critical path.
1606 mAcquireTimeline.updateSignalTimes();
1607 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001608 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001609 mFrameEventHistory.addQueue(*newTimestamps);
1610 }
1611
Brian Anderson3890c392016-07-25 12:48:08 -07001612 if (outDelta) {
1613 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001614 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001615}
Dan Stozae77c7662016-05-13 11:37:28 -07001616
Chia-I Wu98f1c102017-05-30 14:54:08 -07001617size_t Layer::getChildrenCount() const {
1618 size_t count = 0;
1619 for (const sp<Layer>& child : mCurrentChildren) {
1620 count += 1 + child->getChildrenCount();
1621 }
1622 return count;
1623}
1624
Robert Carr1f0a16a2016-10-24 16:27:39 -07001625void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001626 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001627 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001628
Robert Carr1f0a16a2016-10-24 16:27:39 -07001629 mCurrentChildren.add(layer);
1630 layer->setParent(this);
1631}
1632
1633ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001634 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001635 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001636
Robert Carr1323c952019-01-28 18:13:27 -08001637 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001638 return mCurrentChildren.remove(layer);
1639}
1640
Robert Carr1db73f62016-12-21 12:58:51 -08001641bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1642 sp<Handle> handle = nullptr;
1643 sp<Layer> newParent = nullptr;
1644 if (newParentHandle == nullptr) {
1645 return false;
1646 }
1647 handle = static_cast<Handle*>(newParentHandle.get());
1648 newParent = handle->owner.promote();
1649 if (newParent == nullptr) {
1650 ALOGE("Unable to promote Layer handle");
1651 return false;
1652 }
1653
chaviw5aedec92018-10-22 10:40:38 -07001654 if (attachChildren()) {
1655 setTransactionFlags(eTransactionNeeded);
1656 }
Robert Carr1db73f62016-12-21 12:58:51 -08001657 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001658 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001659 }
1660 mCurrentChildren.clear();
1661
1662 return true;
1663}
1664
Robert Carr15eae092018-03-23 13:43:53 -07001665void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001666 for (const sp<Layer>& child : mDrawingChildren) {
1667 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001668 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001669 }
1670}
1671
chaviwf1961f72017-09-18 16:41:07 -07001672bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001673 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001674
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001675 // While layers are detached, we allow most operations
1676 // and simply halt performing the actual transaction. However
1677 // for reparent != null we would enter the mRemovedFromCurrentState
1678 // state, regardless of whether doTransaction was called, and
1679 // so we need to prevent the update here.
1680 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001681 return false;
1682 }
1683
Robert Carr54cf5b12019-01-25 14:02:28 -08001684 sp<Layer> newParent;
1685 if (newParentHandle != nullptr) {
1686 auto handle = static_cast<Handle*>(newParentHandle.get());
1687 newParent = handle->owner.promote();
1688 if (newParent == nullptr) {
1689 ALOGE("Unable to promote Layer handle");
1690 return false;
1691 }
1692 if (newParent == this) {
1693 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1694 return false;
1695 }
1696 }
1697
chaviwf1961f72017-09-18 16:41:07 -07001698 sp<Layer> parent = getParent();
1699 if (parent != nullptr) {
1700 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001701 }
1702
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001703 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001704 newParent->addChild(this);
1705 if (!newParent->isRemovedFromCurrentState()) {
1706 addToCurrentState();
1707 } else {
1708 onRemovedFromCurrentState();
1709 }
1710
1711 if (mLayerDetached) {
1712 mLayerDetached = false;
1713 callSetTransactionFlags = true;
1714 }
1715 } else {
1716 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001717 }
1718
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001719 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001720 setTransactionFlags(eTransactionNeeded);
1721 }
chaviw06178942017-07-27 10:25:59 -07001722 return true;
1723}
1724
Robert Carr9524cb32017-02-13 11:32:32 -08001725bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001726 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001727 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001728 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001729 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001730 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001731 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001732 }
Robert Carr7f619b22017-11-06 12:56:35 -08001733 }
Robert Carr9524cb32017-02-13 11:32:32 -08001734
1735 return true;
1736}
1737
chaviw5aedec92018-10-22 10:40:38 -07001738bool Layer::attachChildren() {
1739 bool changed = false;
1740 for (const sp<Layer>& child : mCurrentChildren) {
1741 sp<Client> parentClient = mClientRef.promote();
1742 sp<Client> client(child->mClientRef.promote());
1743 if (client != nullptr && parentClient != client) {
1744 if (child->mLayerDetached) {
1745 child->mLayerDetached = false;
1746 changed = true;
1747 }
1748 changed |= child->attachChildren();
1749 }
1750 }
1751
1752 return changed;
1753}
1754
Peiyong Lind3788632018-09-18 16:01:31 -07001755bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001756 static const mat4 identityMatrix = mat4();
1757
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001758 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001759 return false;
1760 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001761 ++mCurrentState.sequence;
1762 mCurrentState.colorTransform = matrix;
1763 mCurrentState.hasColorTransform = matrix != identityMatrix;
1764 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001765 setTransactionFlags(eTransactionNeeded);
1766 return true;
1767}
1768
chaviwf66724d2018-11-28 16:35:21 -08001769mat4 Layer::getColorTransform() const {
1770 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1771 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1772 colorTransform = parent->getColorTransform() * colorTransform;
1773 }
1774 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001775}
1776
1777bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001778 bool hasColorTransform = getDrawingState().hasColorTransform;
1779 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1780 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1781 }
1782 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001783}
1784
Chia-I Wu11481472018-05-04 10:43:19 -07001785bool Layer::isLegacyDataSpace() const {
1786 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001787 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001788 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001789}
1790
Robert Carr1f0a16a2016-10-24 16:27:39 -07001791void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001792 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001793}
1794
Robert Carr1f0a16a2016-10-24 16:27:39 -07001795int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001796 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001797}
1798
Robert Carr29abff82017-12-04 13:51:20 -08001799bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1800 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001801 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001802 return state.zOrderRelativeOf != nullptr;
1803}
1804
David Sodman41fdfc92017-11-06 16:09:56 -08001805__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001806 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001807 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1808 "makeTraversalList received invalid stateSet");
1809 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1810 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001811 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001812
Robert Carr29abff82017-12-04 13:51:20 -08001813 if (state.zOrderRelatives.size() == 0) {
1814 *outSkipRelativeZUsers = true;
1815 return children;
1816 }
1817
chaviwfd462612018-05-31 16:11:27 -07001818 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001819 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001820 sp<Layer> strongRelative = weakRelative.promote();
1821 if (strongRelative != nullptr) {
1822 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001823 }
1824 }
1825
Dan Stoza412903f2017-04-27 13:42:17 -07001826 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001827 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001828 if (childState.zOrderRelativeOf != nullptr) {
1829 continue;
1830 }
Robert Carrdb66e622017-04-10 16:55:57 -07001831 traverse.add(child);
1832 }
1833
1834 return traverse;
1835}
1836
Robert Carr1f0a16a2016-10-24 16:27:39 -07001837/**
Robert Carrdb66e622017-04-10 16:55:57 -07001838 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001839 */
Dan Stoza412903f2017-04-27 13:42:17 -07001840void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001841 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1842 // produce a new list for traversing, including our relatives, and not including our children
1843 // who are relatives of another surface. In the case that there are no relative Z,
1844 // makeTraversalList returns our children directly to avoid significant overhead.
1845 // However in this case we need to take the responsibility for filtering children which
1846 // are relatives of another surface here.
1847 bool skipRelativeZUsers = false;
1848 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001849
Robert Carr1f0a16a2016-10-24 16:27:39 -07001850 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001851 for (; i < list.size(); i++) {
1852 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001853 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1854 continue;
1855 }
1856
Robert Carrdb66e622017-04-10 16:55:57 -07001857 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001858 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001859 }
Dan Stoza412903f2017-04-27 13:42:17 -07001860 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001861 }
Robert Carr29abff82017-12-04 13:51:20 -08001862
Dan Stoza412903f2017-04-27 13:42:17 -07001863 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001864 for (; i < list.size(); i++) {
1865 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001866
Robert Carr29abff82017-12-04 13:51:20 -08001867 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1868 continue;
1869 }
Dan Stoza412903f2017-04-27 13:42:17 -07001870 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001871 }
1872}
1873
1874/**
Robert Carrdb66e622017-04-10 16:55:57 -07001875 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001876 */
Dan Stoza412903f2017-04-27 13:42:17 -07001877void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1878 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001879 // See traverseInZOrder for documentation.
1880 bool skipRelativeZUsers = false;
1881 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001882
Robert Carr1f0a16a2016-10-24 16:27:39 -07001883 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001884 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001885 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001886
1887 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1888 continue;
1889 }
1890
Robert Carrdb66e622017-04-10 16:55:57 -07001891 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001892 break;
1893 }
Dan Stoza412903f2017-04-27 13:42:17 -07001894 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001895 }
Dan Stoza412903f2017-04-27 13:42:17 -07001896 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001897 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001898 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001899
1900 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1901 continue;
1902 }
1903
Dan Stoza412903f2017-04-27 13:42:17 -07001904 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001905 }
1906}
1907
chaviw4b129c22018-04-09 16:19:43 -07001908LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1909 const std::vector<Layer*>& layersInTree) {
1910 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1911 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001912 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1913 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001914 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001915
chaviwfd462612018-05-31 16:11:27 -07001916 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001917 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1918 sp<Layer> strongRelative = weakRelative.promote();
1919 // Only add relative layers that are also descendents of the top most parent of the tree.
1920 // If a relative layer is not a descendent, then it should be ignored.
1921 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1922 traverse.add(strongRelative);
1923 }
1924 }
1925
1926 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001927 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001928 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1929 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1930 // the child here since it won't be added later as a relative.
1931 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1932 childState.zOrderRelativeOf.promote().get())) {
1933 continue;
1934 }
1935 traverse.add(child);
1936 }
1937
1938 return traverse;
1939}
1940
1941void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1942 LayerVector::StateSet stateSet,
1943 const LayerVector::Visitor& visitor) {
1944 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001945
1946 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001947 for (; i < list.size(); i++) {
1948 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001949 if (relative->getZ() >= 0) {
1950 break;
1951 }
chaviw4b129c22018-04-09 16:19:43 -07001952 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001953 }
chaviw4b129c22018-04-09 16:19:43 -07001954
chaviwa76b2712017-09-20 12:02:26 -07001955 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001956 for (; i < list.size(); i++) {
1957 const auto& relative = list[i];
1958 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001959 }
1960}
1961
chaviw4b129c22018-04-09 16:19:43 -07001962std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1963 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1964 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1965
1966 std::vector<Layer*> layersInTree = {this};
1967 for (size_t i = 0; i < children.size(); i++) {
1968 const auto& child = children[i];
1969 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1970 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1971 }
1972
1973 return layersInTree;
1974}
1975
1976void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1977 const LayerVector::Visitor& visitor) {
1978 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1979 std::sort(layersInTree.begin(), layersInTree.end());
1980 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1981}
1982
Peiyong Linefefaac2018-08-17 12:27:51 -07001983ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001984 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001985}
1986
chaviw13fdc492017-06-27 12:40:18 -07001987half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001988 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001989
chaviw13fdc492017-06-27 12:40:18 -07001990 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1991 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001992}
Robert Carr6452f122017-03-21 10:41:29 -07001993
chaviw13fdc492017-06-27 12:40:18 -07001994half4 Layer::getColor() const {
1995 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001996 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001997}
Robert Carr6452f122017-03-21 10:41:29 -07001998
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001999Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2000 const auto& p = mDrawingParent.promote();
2001 if (p != nullptr) {
2002 RoundedCornerState parentState = p->getRoundedCornerState();
2003 if (parentState.radius > 0) {
2004 ui::Transform t = getActiveTransform(getDrawingState());
2005 t = t.inverse();
2006 parentState.cropRect = t.transform(parentState.cropRect);
2007 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2008 // but a transform matrix can define horizontal and vertical scales.
2009 // Let's take the average between both of them and pass into the shader, practically we
2010 // never do this type of transformation on windows anyway.
2011 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2012 return parentState;
2013 }
2014 }
2015 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002016 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002017}
2018
Robert Carr1f0a16a2016-10-24 16:27:39 -07002019void Layer::commitChildList() {
2020 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2021 const auto& child = mCurrentChildren[i];
2022 child->commitChildList();
2023 }
2024 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002025 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002026}
2027
Robert Carr720e5062018-07-30 17:45:14 -07002028void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002029 mCurrentState.inputInfo = info;
2030 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();
Robert Carr720e5062018-07-30 17:45:14 -07002218 return info;
2219}
2220
2221bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002222 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002223}
2224
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002225std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2226 return nullptr;
2227}
2228
chaviw3e727cd2019-01-31 13:41:05 -08002229bool Layer::canReceiveInput() const {
2230 return isVisible();
2231}
2232
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002233compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2234 const sp<const DisplayDevice>& display) const {
2235 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2236}
2237
Mathias Agopian13127d82013-03-05 17:47:11 -08002238// ---------------------------------------------------------------------------
2239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002240}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002241
2242#if defined(__gl_h_)
2243#error "don't include gl/gl.h in this file"
2244#endif
2245
2246#if defined(__gl2_h_)
2247#error "don't include gl2/gl2.h in this file"
2248#endif