blob: 4add863c297fbce99e614e3f4af1a70cfaacfe7a [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;
Evan Roskya1f1e152019-01-24 16:17:46 -0800107 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
109 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700111
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700113 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800114 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200115 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700116
117 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700118}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800121 sp<Client> c(mClientRef.promote());
122 if (c != 0) {
123 c->detachLayer(this);
124 }
125
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000126 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700127
Robert Carr2e102c92018-10-23 12:11:15 -0700128 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian13127d82013-03-05 17:47:11 -0800131// ---------------------------------------------------------------------------
132// callbacks
133// ---------------------------------------------------------------------------
134
David Sodmaneb085e02017-10-05 18:49:04 -0700135/*
136 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
137 * Layer. So, the implementation is done in BufferLayer. When called on a
138 * ColorLayer object, it's essentially a NOP.
139 */
David Sodmaneb085e02017-10-05 18:49:04 -0700140void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800141
Chia-I Wuc6657022017-08-15 11:18:17 -0700142void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700143 mRemovedFromCurrentState = true;
144
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
146 if (mCurrentState.zOrderRelativeOf != nullptr) {
147 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
148 if (strongRelative != nullptr) {
149 strongRelative->removeZOrderRelative(this);
150 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
chaviw606e5cf2019-03-01 10:12:10 -0800152 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700153 }
Rob Carr4bba3702018-10-08 21:53:30 +0000154
Robert Carr2e102c92018-10-23 12:11:15 -0700155 // Since we are no longer reachable from CurrentState SurfaceFlinger
156 // will no longer invoke doTransaction for us, and so we will
157 // never finish applying transactions. We signal the sync point
158 // now so that another layer will not become indefinitely
159 // blocked.
160 for (auto& point: mRemoteSyncPoints) {
161 point->setTransactionApplied();
162 }
163 mRemoteSyncPoints.clear();
164
165 {
166 Mutex::Autolock syncLock(mLocalSyncPointMutex);
167 for (auto& point : mLocalSyncPoints) {
168 point->setFrameAvailable();
169 }
170 mLocalSyncPoints.clear();
171 }
172
Chia-I Wuc6657022017-08-15 11:18:17 -0700173 for (const auto& child : mCurrentChildren) {
174 child->onRemovedFromCurrentState();
175 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800176
177 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700178}
Chia-I Wu38512252017-05-17 14:36:16 -0700179
chaviw61626f22018-11-15 16:26:27 -0800180void Layer::addToCurrentState() {
181 mRemovedFromCurrentState = false;
182
183 for (const auto& child : mCurrentChildren) {
184 child->addToCurrentState();
185 }
186}
187
Mathias Agopian13127d82013-03-05 17:47:11 -0800188// ---------------------------------------------------------------------------
189// set-up
190// ---------------------------------------------------------------------------
191
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700192const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800193 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194}
195
chaviw13fdc492017-06-27 12:40:18 -0700196bool Layer::getPremultipledAlpha() const {
197 return mPremultipliedAlpha;
198}
199
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700200sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700202 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800203}
204
205// ---------------------------------------------------------------------------
206// h/w composer set-up
207// ---------------------------------------------------------------------------
208
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800209bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
210 auto outputLayer = findOutputLayerForDisplay(displayDevice);
211 LOG_FATAL_IF(!outputLayer);
212 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
213}
214
215HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
216 auto outputLayer = findOutputLayerForDisplay(displayDevice);
217 if (!outputLayer || !outputLayer->getState().hwc) {
218 return nullptr;
219 }
220 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700221}
Steven Thomasb02664d2017-07-26 18:48:28 -0700222
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800223Rect Layer::getContentCrop() const {
224 // this is the crop rectangle that applies to the buffer
225 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700226 Rect crop;
227 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800228 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700229 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800230 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800231 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800232 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700233 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800234 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700235 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700237 return crop;
238}
239
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700240static Rect reduce(const Rect& win, const Region& exclude) {
241 if (CC_LIKELY(exclude.isEmpty())) {
242 return win;
243 }
244 if (exclude.isRect()) {
245 return win.reduce(exclude.getBounds());
246 }
247 return Region(win).subtract(exclude).getBounds();
248}
249
Dan Stoza80d61162017-12-20 15:57:52 -0800250static FloatRect reduce(const FloatRect& win, const Region& exclude) {
251 if (CC_LIKELY(exclude.isEmpty())) {
252 return win;
253 }
254 // Convert through Rect (by rounding) for lack of FloatRegion
255 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
256}
257
Vishnu Nair4351ad52019-02-11 14:13:02 -0800258Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800259 if (!reduceTransparentRegion) {
260 return Rect{mScreenBounds};
261 }
262
263 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800264 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800265 // Transform to screen space.
266 bounds = t.transform(bounds);
267 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700268}
269
Vishnu Nair4351ad52019-02-11 14:13:02 -0800270FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000271 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800272 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700273}
274
Vishnu Nairf0c28512019-02-08 12:40:28 -0800275FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
276 // Subtract the transparent region and snap to the bounds.
277 return reduce(mBounds, activeTransparentRegion);
278}
279
Vishnu Nair4351ad52019-02-11 14:13:02 -0800280ui::Transform Layer::getTransformWithScale() const {
281 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
282 // it isFixedSize) then there may be additional scaling not accounted
283 // for in the transform. We need to mirror this scaling to child surfaces
284 // or we will break the contract where WM can treat child surfaces as
285 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800286 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800287 return mEffectiveTransform;
288 }
289
Marissa Wall290ad082019-03-06 13:23:47 -0800290 // If the layer is a buffer state layer, the active width and height
291 // could be infinite. In that case, return the effective transform.
292 const uint32_t activeWidth = getActiveWidth(getDrawingState());
293 const uint32_t activeHeight = getActiveHeight(getDrawingState());
294 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
295 return mEffectiveTransform;
296 }
297
Vishnu Nair4351ad52019-02-11 14:13:02 -0800298 int bufferWidth;
299 int bufferHeight;
300 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800301 bufferWidth = mActiveBuffer->getWidth();
302 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800303 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800304 bufferHeight = mActiveBuffer->getWidth();
305 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800306 }
Marissa Wall290ad082019-03-06 13:23:47 -0800307 float sx = activeWidth / static_cast<float>(bufferWidth);
308 float sy = activeHeight / static_cast<float>(bufferHeight);
309
Vishnu Nair4351ad52019-02-11 14:13:02 -0800310 ui::Transform extraParentScaling;
311 extraParentScaling.set(sx, 0, 0, sy);
312 return mEffectiveTransform * extraParentScaling;
313}
314
315void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
316 const State& s(getDrawingState());
317
318 // Calculate effective layer transform
319 mEffectiveTransform = parentTransform * getActiveTransform(s);
320
321 // Transform parent bounds to layer space
322 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
323
324 // Calculate display frame
325 mSourceBounds = computeSourceBounds(parentBounds);
326
327 // Calculate bounds by croping diplay frame with layer crop and parent bounds
328 FloatRect bounds = mSourceBounds;
329 const Rect layerCrop = getCrop(s);
330 if (!layerCrop.isEmpty()) {
331 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
332 }
333 bounds = bounds.intersect(parentBounds);
334
335 mBounds = bounds;
336 mScreenBounds = mEffectiveTransform.transform(mBounds);
337 for (const sp<Layer>& child : mDrawingChildren) {
338 child->computeBounds(mBounds, getTransformWithScale());
339 }
340}
341
Robert Carrb5d3d262016-03-25 15:08:13 -0700342
Robert Carr1f0a16a2016-10-24 16:27:39 -0700343
Vishnu Nair60356342018-11-13 13:00:45 -0800344Rect Layer::getCroppedBufferSize(const State& s) const {
345 Rect size = getBufferSize(s);
346 Rect crop = getCrop(s);
347 if (!crop.isEmpty() && size.isValid()) {
348 size.intersect(crop, &size);
349 } else if (!crop.isEmpty()) {
350 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700351 }
Vishnu Nair60356342018-11-13 13:00:45 -0800352 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800353}
354
Alec Mouri1f3bd182019-01-24 15:20:18 +0000355Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700356 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800357 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700358 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800359
360 // apply the projection's clipping to the window crop in
361 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700362 // if there are no window scaling involved, this operation will map to full
363 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700364
Vishnu Nair4351ad52019-02-11 14:13:02 -0800365 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000366 ui::Transform t = getTransform();
367 // Transform to screen space.
368 activeCropFloat = t.transform(activeCropFloat);
369 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
370 // Back to layer space to work with the content crop.
371 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800372 // This needs to be here as transform.transform(Rect) computes the
373 // transformed rect and then takes the bounding box of the result before
374 // returning. This means
375 // transform.inverse().transform(transform.transform(Rect)) != Rect
376 // in which case we need to make sure the final rect is clipped to the
377 // display bounds.
378 Rect activeCrop{activeCropFloat};
379 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000380 activeCrop.clear();
381 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700382 return activeCrop;
383}
384
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700385void Layer::setupRoundedCornersCropCoordinates(Rect win,
386 const FloatRect& roundedCornersCrop) const {
387 // Translate win by the rounded corners rect coordinates, to have all values in
388 // layer coordinate space.
389 win.left -= roundedCornersCrop.left;
390 win.right -= roundedCornersCrop.left;
391 win.top -= roundedCornersCrop.top;
392 win.bottom -= roundedCornersCrop.top;
393
Lloyd Pique0b785d82018-12-04 17:25:27 -0800394 renderengine::Mesh::VertexArray<vec2> cropCoords(
395 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700396 cropCoords[0] = vec2(win.left, win.top);
397 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
398 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
399 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700400}
401
Alec Mouri1f3bd182019-01-24 15:20:18 +0000402FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700403 // the content crop is the area of the content that gets scaled to the
404 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800405 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700406
407 // In addition there is a WM-specified crop we pull from our drawing state.
408 const State& s(getDrawingState());
409
Alec Mouri1f3bd182019-01-24 15:20:18 +0000410 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800411 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700412
Marissa Wall290ad082019-03-06 13:23:47 -0800413 int32_t winWidth = bufferSize.getWidth();
414 int32_t winHeight = bufferSize.getHeight();
415
416 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1]) if display frame
417 // hasn't been set and the parent is an unbounded layer.
418 if (winWidth < 0 && winHeight < 0) {
419 return crop;
420 }
421
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 // Transform the window crop to match the buffer coordinate system,
423 // which means using the inverse of the current transform set on the
424 // SurfaceFlingerConsumer.
425 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800426 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000427 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700428 * the code below applies the primary display's inverse transform to the
429 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 */
David Sodman41fdfc92017-11-06 16:09:56 -0800431 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000432 // calculate the inverse transform
433 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800434 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800435 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700437 invTransform = (ui::Transform(invTransformOrient) *
438 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800439 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
442 // If the activeCrop has been rotate the ends are rotated but not
443 // the space itself so when transforming ends back we can't rely on
444 // a modification of the axes of rotation. To account for this we
445 // need to reorient the inverse rotation in terms of the current
446 // axes of rotation.
447 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
448 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
449 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800450 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800452 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800454 const Rect winCrop =
455 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000456
457 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800458 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000459 float yScale = crop.getHeight() / float(winHeight);
460
David Sodman41fdfc92017-11-06 16:09:56 -0800461 float insetL = winCrop.left * xScale;
462 float insetT = winCrop.top * yScale;
463 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464 float insetB = (winHeight - winCrop.bottom) * yScale;
465
David Sodman41fdfc92017-11-06 16:09:56 -0800466 crop.left += insetL;
467 crop.top += insetT;
468 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000469 crop.bottom -= insetB;
470
Mathias Agopian13127d82013-03-05 17:47:11 -0800471 return crop;
472}
473
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700474void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800475 const auto outputLayer = findOutputLayerForDisplay(display);
476 LOG_FATAL_IF(!outputLayer);
477 LOG_FATAL_IF(!outputLayer->getState().hwc);
478 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700479
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800480 if (!hasHwcLayer(display)) {
481 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
482 display->getDebugName().c_str());
483 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800484 }
485
Lloyd Pique0b785d82018-12-04 17:25:27 -0800486 LOG_FATAL_IF(!getCompositionLayer());
487 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800488 auto& compositionState = outputLayer->editState();
489
490 // enable this layer
491 compositionState.forceClientComposition = false;
492
493 if (isSecure() && !display->isSecure()) {
494 compositionState.forceClientComposition = true;
495 }
David Sodman15094112018-10-11 09:39:37 -0700496
Mathias Agopian13127d82013-03-05 17:47:11 -0800497 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700498 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800499 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500500 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800501 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800502 blendMode =
503 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800504 }
David Sodman15094112018-10-11 09:39:37 -0700505 auto error = hwcLayer->setBlendMode(blendMode);
506 ALOGE_IF(error != HWC2::Error::None,
507 "[%s] Failed to set blend mode %s:"
508 " %s (%d)",
509 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
510 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800511 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800512
513 // apply the layer's transform, followed by the display's global transform
514 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700515 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800516 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700517 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800518 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700519 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700520 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000521 activeCrop.clear();
522 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700523 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800524 // This needs to be here as transform.transform(Rect) computes the
525 // transformed rect and then takes the bounding box of the result before
526 // returning. This means
527 // transform.inverse().transform(transform.transform(Rect)) != Rect
528 // in which case we need to make sure the final rect is clipped to the
529 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800530 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000531 activeCrop.clear();
532 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700533 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800534 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
535 activeTransparentRegion.orSelf(
536 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800537 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
538 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800539 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700540 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700541
Vishnu Nair4351ad52019-02-11 14:13:02 -0800542 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800543 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800544 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700545 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000546 frame.clear();
547 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700548 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800549 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700550 error = hwcLayer->setDisplayFrame(transformedFrame);
551 if (error != HWC2::Error::None) {
552 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
553 transformedFrame.left, transformedFrame.top, transformedFrame.right,
554 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
555 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800556 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700557 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700559 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700560 error = hwcLayer->setSourceCrop(sourceCrop);
561 if (error != HWC2::Error::None) {
562 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
563 "%s (%d)",
564 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
565 to_string(error).c_str(), static_cast<int32_t>(error));
566 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800567 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700568 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800570 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700571 error = hwcLayer->setPlaneAlpha(alpha);
572 ALOGE_IF(error != HWC2::Error::None,
573 "[%s] Failed to set plane alpha %.3f: "
574 "%s (%d)",
575 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800576 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800577
David Sodman15094112018-10-11 09:39:37 -0700578 error = hwcLayer->setZOrder(z);
579 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
580 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800581 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500582
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800583 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
584 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700585 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400586 if (parent.get()) {
587 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800588 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
589 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
590 if (parentType >= 0 || parentAppId >= 0) {
591 type = parentType;
592 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700593 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400594 }
595
David Sodman15094112018-10-11 09:39:37 -0700596 error = hwcLayer->setInfo(type, appId);
597 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
598 static_cast<int32_t>(error));
599
Lloyd Pique0b785d82018-12-04 17:25:27 -0800600 commonCompositionState.type = type;
601 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700602
Mathias Agopian29a367b2011-07-12 14:51:45 -0700603 /*
604 * Transformations are applied in this order:
605 * 1) buffer orientation/flip/mirror
606 * 2) state transformation (window manager)
607 * 3) layer orientation (screen orientation)
608 * (NOTE: the matrices are multiplied in reverse order)
609 */
610
Peiyong Linefefaac2018-08-17 12:27:51 -0700611 const ui::Transform bufferOrientation(mCurrentTransform);
612 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700613
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800614 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700615 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700616 * the code below applies the primary display's inverse transform to the
617 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 */
David Sodman41fdfc92017-11-06 16:09:56 -0800619 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700620 // calculate the inverse transform
621 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800622 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700623 }
Robert Carrcae605c2017-03-29 12:10:31 -0700624
625 /*
626 * Here we cancel out the orientation component of the WM transform.
627 * The scaling and translate components are already included in our bounds
628 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000629 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700630 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700631 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700632 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700633
634 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800635 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700636 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800638 compositionState.forceClientComposition = true;
639 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640 } else {
641 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700642 auto error = hwcLayer->setTransform(transform);
643 ALOGE_IF(error != HWC2::Error::None,
644 "[%s] Failed to set transform %s: "
645 "%s (%d)",
646 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
647 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800648 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800649 }
650}
651
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800652void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
653 const auto outputLayer = findOutputLayerForDisplay(display);
654 LOG_FATAL_IF(!outputLayer);
655 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656}
Dan Stozaee44edd2015-03-23 15:50:23 -0700657
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800658bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
659 const auto outputLayer = findOutputLayerForDisplay(display);
660 LOG_FATAL_IF(!outputLayer);
661 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800662}
663
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700664void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800665 const auto outputLayer = findOutputLayerForDisplay(display);
666 LOG_FATAL_IF(!outputLayer);
667
668 if (!outputLayer->getState().hwc ||
669 (*outputLayer->getState().hwc).hwcCompositionType !=
670 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671 return;
672 }
673
674 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800675 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676
677 // Apply the layer's transform, followed by the display's global transform
678 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800679 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700681 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800682 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700683 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700684 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685 auto position = displayTransform.transform(frame);
686
Dominik Laskowski7e045462018-05-30 13:02:02 -0700687 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800688 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800689 ALOGE_IF(error != HWC2::Error::None,
690 "[%s] Failed to set cursor position "
691 "to (%d, %d): %s (%d)",
692 mName.string(), position.left, position.top, to_string(error).c_str(),
693 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694}
Riley Andrews03414a12014-07-01 14:22:59 -0700695
Mathias Agopian13127d82013-03-05 17:47:11 -0800696// ---------------------------------------------------------------------------
697// drawing...
698// ---------------------------------------------------------------------------
699
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000700bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800701 Region& clearRegion, const bool supportProtectedContent,
702 renderengine::LayerSettings& layer) {
703 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800704}
705
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000706bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800707 Region& clearRegion, const bool supportProtectedContent,
708 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000709 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800710 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000711}
712
713bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
714 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800715 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000716 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800717 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000718 half alpha = getAlpha();
719 layer.geometry.boundaries = bounds;
720 if (useIdentityTransform) {
721 layer.geometry.positionTransform = mat4();
722 } else {
723 const ui::Transform transform = getTransform();
724 mat4 m;
725 m[0][0] = transform[0][0];
726 m[0][1] = transform[0][1];
727 m[0][3] = transform[0][2];
728 m[1][0] = transform[1][0];
729 m[1][1] = transform[1][1];
730 m[1][3] = transform[1][2];
731 m[3][0] = transform[2][0];
732 m[3][1] = transform[2][1];
733 m[3][3] = transform[2][2];
734 layer.geometry.positionTransform = m;
735 }
736
737 if (hasColorTransform()) {
738 layer.colorTransform = getColorTransform();
739 }
740
741 const auto roundedCornerState = getRoundedCornerState();
742 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
743 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
744
745 layer.alpha = alpha;
746 layer.sourceDataspace = mCurrentDataSpace;
747 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800748}
749
David Sodman41fdfc92017-11-06 16:09:56 -0800750void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
751 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800752 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800753 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700754 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800755 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800756}
757
David Sodmanc1498e62018-09-12 14:36:26 -0700758void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
759 clearWithOpenGL(renderArea, 0, 0, 0, 0);
760}
761
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800762void Layer::setCompositionType(const sp<const DisplayDevice>& display,
763 Hwc2::IComposerClient::Composition type) {
764 const auto outputLayer = findOutputLayerForDisplay(display);
765 LOG_FATAL_IF(!outputLayer);
766 LOG_FATAL_IF(!outputLayer->getState().hwc);
767 auto& compositionState = outputLayer->editState();
768
769 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
770 toString(type).c_str(), 1);
771 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700772 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800773 (*compositionState.hwc).hwcCompositionType = type;
774
775 auto error = (*compositionState.hwc)
776 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
777 ALOGE_IF(error != HWC2::Error::None,
778 "[%s] Failed to set "
779 "composition type %s: %s (%d)",
780 mName.string(), toString(type).c_str(), to_string(error).c_str(),
781 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782 }
783}
784
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800785Hwc2::IComposerClient::Composition Layer::getCompositionType(
786 const sp<const DisplayDevice>& display) const {
787 const auto outputLayer = findOutputLayerForDisplay(display);
788 LOG_FATAL_IF(!outputLayer);
789 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
790 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791}
792
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800793bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
794 const auto outputLayer = findOutputLayerForDisplay(display);
795 LOG_FATAL_IF(!outputLayer);
796 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800798
Dan Stozacac35382016-01-27 12:21:06 -0800799bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
800 if (point->getFrameNumber() <= mCurrentFrameNumber) {
801 // Don't bother with a SyncPoint, since we've already latched the
802 // relevant frame
803 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700804 }
Robert Carr2e102c92018-10-23 12:11:15 -0700805 if (isRemovedFromCurrentState()) {
806 return false;
807 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700808
Dan Stozacac35382016-01-27 12:21:06 -0800809 Mutex::Autolock lock(mLocalSyncPointMutex);
810 mLocalSyncPoints.push_back(point);
811 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700812}
813
Mathias Agopian13127d82013-03-05 17:47:11 -0800814// ----------------------------------------------------------------------------
815// local state
816// ----------------------------------------------------------------------------
817
Peiyong Lin833074a2018-08-28 11:53:54 -0700818void Layer::computeGeometry(const RenderArea& renderArea,
819 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700820 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700821 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800822 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700823
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000824 vec2 lt = vec2(win.left, win.top);
825 vec2 lb = vec2(win.left, win.bottom);
826 vec2 rb = vec2(win.right, win.bottom);
827 vec2 rt = vec2(win.right, win.top);
828
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800829 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000830 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700831 lt = layerTransform.transform(lt);
832 lb = layerTransform.transform(lb);
833 rb = layerTransform.transform(rb);
834 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000835 }
836
Peiyong Lin833074a2018-08-28 11:53:54 -0700837 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700838 position[0] = renderAreaTransform.transform(lt);
839 position[1] = renderAreaTransform.transform(lb);
840 position[2] = renderAreaTransform.transform(rb);
841 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800842}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800843
David Sodman41fdfc92017-11-06 16:09:56 -0800844bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800845 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700846 return (s.flags & layer_state_t::eLayerSecure);
847}
848
Mathias Agopian13127d82013-03-05 17:47:11 -0800849void Layer::setVisibleRegion(const Region& visibleRegion) {
850 // always called from main thread
851 this->visibleRegion = visibleRegion;
852}
853
854void Layer::setCoveredRegion(const Region& coveredRegion) {
855 // always called from main thread
856 this->coveredRegion = coveredRegion;
857}
858
David Sodman41fdfc92017-11-06 16:09:56 -0800859void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800860 // always called from main thread
861 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
862}
863
Robert Carre5f4f692018-01-12 13:12:28 -0800864void Layer::clearVisibilityRegions() {
865 visibleRegion.clear();
866 visibleNonTransparentRegion.clear();
867 coveredRegion.clear();
868}
869
Mathias Agopian13127d82013-03-05 17:47:11 -0800870// ----------------------------------------------------------------------------
871// transaction
872// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800873
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800874void Layer::pushPendingState() {
875 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700876 return;
877 }
878
Dan Stoza7dde5992015-05-22 09:51:44 -0700879 // If this transaction is waiting on the receipt of a frame, generate a sync
880 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700881 // We don't allow installing sync points after we are removed from the current state
882 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800883 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
884 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800885 if (barrierLayer == nullptr) {
886 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 // If we can't promote the layer we are intended to wait on,
888 // then it is expired or otherwise invalid. Allow this transaction
889 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800890 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800891 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800892 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800893 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800894 mRemoteSyncPoints.push_back(std::move(syncPoint));
895 } else {
896 // We already missed the frame we're supposed to synchronize
897 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800899 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 }
901
Dan Stoza7dde5992015-05-22 09:51:44 -0700902 // Wake us up to check if the frame has been received
903 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700904 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700905 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800906 mPendingStates.push_back(mCurrentState);
907 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700908}
909
Pablo Ceballos05289c22016-04-14 15:49:55 -0700910void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800911 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700912
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800913 mPendingStates.removeAt(0);
914 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700915}
916
Pablo Ceballos05289c22016-04-14 15:49:55 -0700917bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800919 while (!mPendingStates.empty()) {
920 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700921 if (mRemoteSyncPoints.empty()) {
922 // If we don't have a sync point for this, apply it anyway. It
923 // will be visually wrong, but it should keep us from getting
924 // into too much trouble.
925 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700926 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700927 stateUpdateAvailable = true;
928 continue;
929 }
930
Marissa Wallf58c14b2018-07-24 10:50:43 -0700931 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800932 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800933 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800934
935 // Signal our end of the sync point and then dispose of it
936 mRemoteSyncPoints.front()->setTransactionApplied();
937 mRemoteSyncPoints.pop_front();
938 continue;
939 }
940
Dan Stoza7dde5992015-05-22 09:51:44 -0700941 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
942 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700943 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 stateUpdateAvailable = true;
945
946 // Signal our end of the sync point and then dispose of it
947 mRemoteSyncPoints.front()->setTransactionApplied();
948 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800949 } else {
950 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700951 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700952 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700953 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700954 stateUpdateAvailable = true;
955 }
956 }
957
958 // If we still have pending updates, wake SurfaceFlinger back up and point
959 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800960 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700961 setTransactionFlags(eTransactionNeeded);
962 mFlinger->setTransactionFlags(eTraversalNeeded);
963 }
964
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800965 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 return stateUpdateAvailable;
967}
968
Marissa Wall61c58622018-07-18 10:12:20 -0700969uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000970 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
Marissa Wall61c58622018-07-18 10:12:20 -0700972 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
973 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700974
David Sodmaneb085e02017-10-05 18:49:04 -0700975 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700976 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000977 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800978 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
979 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
980 " requested={ wh={%4u,%4u} }}\n"
981 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
982 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700983 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700984 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
985 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
986 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
987 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
988 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700989 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
990 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
991 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700993
Robert Carre392b552017-09-19 12:16:05 -0700994 // Don't let Layer::doTransaction update the drawing state
995 // if we have a pending resize, unless we are in fixed-size mode.
996 // the drawing state will be updated only once we receive a buffer
997 // with the correct size.
998 //
999 // In particular, we want to make sure the clip (which is part
1000 // of the geometry state) is latched together with the size but is
1001 // latched immediately when no resizing is involved.
1002 //
1003 // If a sideband stream is attached, however, we want to skip this
1004 // optimization so that transactions aren't missed when a buffer
1005 // never arrives
1006 //
1007 // In the case that we don't have a buffer we ignore other factors
1008 // and avoid entering the resizePending state. At a high level the
1009 // resizePending state is to avoid applying the state of the new buffer
1010 // to the old buffer. However in the state where we don't have an old buffer
1011 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001012 const bool resizePending =
1013 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1014 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -08001015 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001016 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -08001017 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001018 flags |= eDontUpdateGeometryState;
1019 }
1020 }
1021
Robert Carr7bf247e2017-05-18 14:02:49 -07001022 // Here we apply various requested geometry states, depending on our
1023 // latching configuration. See Layer.h for a detailed discussion of
1024 // how geometry latching is controlled.
1025 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001026 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001027
1028 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1029 // mode, which causes attributes which normally latch regardless of scaling mode,
1030 // to be delayed. We copy the requested state to the active state making sure
1031 // to respect these rules (again see Layer.h for a detailed discussion).
1032 //
1033 // There is an awkward asymmetry in the handling of the crop states in the position
1034 // states, as can be seen below. Largely this arises from position and transform
1035 // being stored in the same data structure while having different latching rules.
1036 // b/38182305
1037 //
Marissa Wall61c58622018-07-18 10:12:20 -07001038 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001039 // applyPendingStates in the presence of deferred transactions.
1040 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001041 float tx = stateToCommit->active_legacy.transform.tx();
1042 float ty = stateToCommit->active_legacy.transform.ty();
1043 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1044 stateToCommit->active_legacy.transform.set(tx, ty);
1045 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001046 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001047 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001048 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001049 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 }
1051
Marissa Wall61c58622018-07-18 10:12:20 -07001052 return flags;
1053}
1054
1055uint32_t Layer::doTransaction(uint32_t flags) {
1056 ATRACE_CALL();
1057
chaviw5aedec92018-10-22 10:40:38 -07001058 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001059 return flags;
1060 }
1061
1062 if (mChildrenChanged) {
1063 flags |= eVisibleRegion;
1064 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001065 }
1066
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001067 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001068 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001069 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001070 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001071 }
1072
1073 flags = doTransactionResize(flags, &c);
1074
Alec Mourib416efd2018-09-06 21:01:59 +00001075 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001076
1077 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001078 // invalidate and recompute the visible regions if needed
1079 flags |= Layer::eVisibleRegion;
1080 }
1081
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001082 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001083 // invalidate and recompute the visible regions if needed
1084 flags |= eVisibleRegion;
1085 this->contentDirty = true;
1086
1087 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001088 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001089 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001090 }
1091
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001093 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001094 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001095 }
1096
Mathias Agopian13127d82013-03-05 17:47:11 -08001097 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001098 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001099 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001100 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101}
1102
Pablo Ceballos05289c22016-04-14 15:49:55 -07001103void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001104 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001105}
1106
Mathias Agopian13127d82013-03-05 17:47:11 -08001107uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001109}
1110
1111uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001112 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001113}
1114
Robert Carr82364e32016-05-15 11:27:47 -07001115bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 if (mCurrentState.requested_legacy.transform.tx() == x &&
1117 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001118 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001120
1121 // We update the requested and active position simultaneously because
1122 // we want to apply the position portion of the transform matrix immediately,
1123 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001124 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001125 if (immediate && !mFreezeGeometryUpdates) {
1126 // Here we directly update the active state
1127 // unlike other setters, because we store it within
1128 // the transform, but use different latching rules.
1129 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001130 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001131 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001132 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001133
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001134 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001135 setTransactionFlags(eTransactionNeeded);
1136 return true;
1137}
Robert Carr82364e32016-05-15 11:27:47 -07001138
Robert Carr1f0a16a2016-10-24 16:27:39 -07001139bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1140 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1141 if (idx < 0) {
1142 return false;
1143 }
1144 if (childLayer->setLayer(z)) {
1145 mCurrentChildren.removeAt(idx);
1146 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001147 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001148 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001149 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001150}
1151
Robert Carr503c7042017-09-27 15:06:08 -07001152bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1153 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1154 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1155 if (idx < 0) {
1156 return false;
1157 }
1158 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1159 mCurrentChildren.removeAt(idx);
1160 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001161 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001162 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001163 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001164}
1165
Robert Carrae060832016-11-28 10:51:00 -08001166bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001167 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1168 mCurrentState.sequence++;
1169 mCurrentState.z = z;
1170 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001171
1172 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001173 if (mCurrentState.zOrderRelativeOf != nullptr) {
1174 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001175 if (strongRelative != nullptr) {
1176 strongRelative->removeZOrderRelative(this);
1177 }
chaviw606e5cf2019-03-01 10:12:10 -08001178 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001179 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001180 setTransactionFlags(eTransactionNeeded);
1181 return true;
1182}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001183
Robert Carrdb66e622017-04-10 16:55:57 -07001184void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001185 mCurrentState.zOrderRelatives.remove(relative);
1186 mCurrentState.sequence++;
1187 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001188 setTransactionFlags(eTransactionNeeded);
1189}
1190
1191void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001192 mCurrentState.zOrderRelatives.add(relative);
1193 mCurrentState.modified = true;
1194 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001195 setTransactionFlags(eTransactionNeeded);
1196}
1197
chaviw606e5cf2019-03-01 10:12:10 -08001198void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1199 mCurrentState.zOrderRelativeOf = relativeOf;
1200 mCurrentState.sequence++;
1201 mCurrentState.modified = true;
1202 setTransactionFlags(eTransactionNeeded);
1203}
1204
Robert Carr503d2bd2017-12-04 15:49:47 -08001205bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001206 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1207 if (handle == nullptr) {
1208 return false;
1209 }
1210 sp<Layer> relative = handle->owner.promote();
1211 if (relative == nullptr) {
1212 return false;
1213 }
1214
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001215 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1216 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001217 return false;
1218 }
1219
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001220 mCurrentState.sequence++;
1221 mCurrentState.modified = true;
1222 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001223
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001224 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001225 if (oldZOrderRelativeOf != nullptr) {
1226 oldZOrderRelativeOf->removeZOrderRelative(this);
1227 }
chaviw606e5cf2019-03-01 10:12:10 -08001228 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001229 relative->addZOrderRelative(this);
1230
1231 setTransactionFlags(eTransactionNeeded);
1232
1233 return true;
1234}
1235
Mathias Agopian13127d82013-03-05 17:47:11 -08001236bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001237 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001238 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001239 mCurrentState.requested_legacy.w = w;
1240 mCurrentState.requested_legacy.h = h;
1241 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001242 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001243
1244 // record the new size, from this point on, when the client request
1245 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001246 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001247 return true;
1248}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001249bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001250 if (mCurrentState.color.a == alpha) return false;
1251 mCurrentState.sequence++;
1252 mCurrentState.color.a = alpha;
1253 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 setTransactionFlags(eTransactionNeeded);
1255 return true;
1256}
chaviw13fdc492017-06-27 12:40:18 -07001257
Valerie Haudd0b7572019-01-29 14:59:27 -08001258bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1259 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001260 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001261 }
1262 mCurrentState.sequence++;
1263 mCurrentState.modified = true;
1264 setTransactionFlags(eTransactionNeeded);
1265
1266 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001267 // create background color layer if one does not yet exist
1268 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1269 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001270 mCurrentState.bgColorLayer = new ColorLayer(
1271 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001272
Valerie Haudd0b7572019-01-29 14:59:27 -08001273 // add to child list
1274 addChild(mCurrentState.bgColorLayer);
1275 mFlinger->mLayersAdded = true;
1276 // set up SF to handle added color layer
1277 if (isRemovedFromCurrentState()) {
1278 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1279 }
1280 mFlinger->setTransactionFlags(eTransactionNeeded);
1281 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1282 mCurrentState.bgColorLayer->reparent(nullptr);
1283 mCurrentState.bgColorLayer = nullptr;
1284 return true;
1285 }
1286
1287 mCurrentState.bgColorLayer->setColor(color);
1288 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1289 mCurrentState.bgColorLayer->setAlpha(alpha);
1290 mCurrentState.bgColorLayer->setDataspace(dataspace);
1291
chaviw13fdc492017-06-27 12:40:18 -07001292 return true;
1293}
1294
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001295bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001296 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001297
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001298 mCurrentState.sequence++;
1299 mCurrentState.cornerRadius = cornerRadius;
1300 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001301 setTransactionFlags(eTransactionNeeded);
1302 return true;
1303}
1304
Robert Carrd4ae7f32018-06-07 16:10:57 -07001305bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1306 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001307 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001308 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1309
1310 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1311 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1312 return false;
1313 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001314 mCurrentState.sequence++;
1315 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1316 matrix.dsdy);
1317 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001318 setTransactionFlags(eTransactionNeeded);
1319 return true;
1320}
Marissa Wall61c58622018-07-18 10:12:20 -07001321
Mathias Agopian13127d82013-03-05 17:47:11 -08001322bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001323 mCurrentState.requestedTransparentRegion_legacy = transparent;
1324 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001325 setTransactionFlags(eTransactionNeeded);
1326 return true;
1327}
1328bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001329 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1330 if (mCurrentState.flags == newFlags) return false;
1331 mCurrentState.sequence++;
1332 mCurrentState.flags = newFlags;
1333 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001334 setTransactionFlags(eTransactionNeeded);
1335 return true;
1336}
Robert Carr99e27f02016-06-16 15:18:02 -07001337
Marissa Wallf58c14b2018-07-24 10:50:43 -07001338bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001339 if (mCurrentState.requestedCrop_legacy == crop) return false;
1340 mCurrentState.sequence++;
1341 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001342 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001343 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001344 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001345 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1346
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001347 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001348 setTransactionFlags(eTransactionNeeded);
1349 return true;
1350}
Robert Carr8d5227b2017-03-16 15:41:03 -07001351
Robert Carrc3574f72016-03-24 12:19:32 -07001352bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001353 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001354 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001355 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001356 return true;
1357}
1358
Evan Roskyef876c92019-01-25 17:46:06 -08001359bool Layer::setMetadata(const LayerMetadata& data) {
1360 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001361 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001362 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001363 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001364 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001365}
1366
Mathias Agopian13127d82013-03-05 17:47:11 -08001367bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001368 if (mCurrentState.layerStack == layerStack) return false;
1369 mCurrentState.sequence++;
1370 mCurrentState.layerStack = layerStack;
1371 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001372 setTransactionFlags(eTransactionNeeded);
1373 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001374}
1375
Peiyong Linc502cb72019-03-01 15:00:23 -08001376bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1377 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1378 return false;
1379 }
1380 mCurrentState.sequence++;
1381 mCurrentState.colorSpaceAgnostic = agnostic;
1382 mCurrentState.modified = true;
1383 setTransactionFlags(eTransactionNeeded);
1384 return true;
1385}
1386
Robert Carr1f0a16a2016-10-24 16:27:39 -07001387uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001388 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001389 if (p == nullptr) {
1390 return getDrawingState().layerStack;
1391 }
1392 return p->getLayerStack();
1393}
1394
Marissa Wallf58c14b2018-07-24 10:50:43 -07001395void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001396 mCurrentState.barrierLayer_legacy = barrierLayer;
1397 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001398 // We don't set eTransactionNeeded, because just receiving a deferral
1399 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001400 mCurrentState.modified = true;
1401 pushPendingState();
1402 mCurrentState.barrierLayer_legacy = nullptr;
1403 mCurrentState.frameNumber_legacy = 0;
1404 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001405}
1406
Marissa Wallf58c14b2018-07-24 10:50:43 -07001407void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001408 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001409 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001410}
1411
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412// ----------------------------------------------------------------------------
1413// pageflip handling...
1414// ----------------------------------------------------------------------------
1415
Robert Carr1f0a16a2016-10-24 16:27:39 -07001416bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001417 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001418 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001419 if (parent != nullptr && parent->isHiddenByPolicy()) {
1420 return true;
1421 }
1422 return s.flags & layer_state_t::eLayerHidden;
1423}
1424
David Sodman41fdfc92017-11-06 16:09:56 -08001425uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001426 // TODO: should we do something special if mSecure is set?
1427 if (mProtectedByApp) {
1428 // need a hardware-protected path to external video sink
1429 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001430 }
Riley Andrews03414a12014-07-01 14:22:59 -07001431 if (mPotentialCursor) {
1432 usage |= GraphicBuffer::USAGE_CURSOR;
1433 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001434 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001435 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001436}
1437
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001438void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001439 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001440 // Disable setting transform hint if the debug flag is set or if the
1441 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1442 // in one orientation.
1443 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001444 // The transform hint is used to improve performance, but we can
1445 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001446 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001447 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001448 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001449 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001450 orientation = 0;
1451 }
1452 }
David Sodmaneb085e02017-10-05 18:49:04 -07001453 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001454}
1455
Mathias Agopian13127d82013-03-05 17:47:11 -08001456// ----------------------------------------------------------------------------
1457// debugging
1458// ----------------------------------------------------------------------------
1459
Marissa Wall61c58622018-07-18 10:12:20 -07001460// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001461LayerDebugInfo Layer::getLayerDebugInfo() const {
1462 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001463 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001464 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001465 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001466 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001467 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001468 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001469 info.mVisibleRegion = visibleRegion;
1470 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001471 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001472 info.mX = ds.active_legacy.transform.tx();
1473 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001474 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001475 info.mWidth = ds.active_legacy.w;
1476 info.mHeight = ds.active_legacy.h;
1477 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001478 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001479 info.mFlags = ds.flags;
1480 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001481 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001482 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1483 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1484 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1485 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001486 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001487 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001488 if (buffer != 0) {
1489 info.mActiveBufferWidth = buffer->getWidth();
1490 info.mActiveBufferHeight = buffer->getHeight();
1491 info.mActiveBufferStride = buffer->getStride();
1492 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001493 } else {
1494 info.mActiveBufferWidth = 0;
1495 info.mActiveBufferHeight = 0;
1496 info.mActiveBufferStride = 0;
1497 info.mActiveBufferFormat = 0;
1498 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001499 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001500 info.mNumQueuedFrames = getQueuedFrameCount();
1501 info.mRefreshPending = isBufferLatched();
1502 info.mIsOpaque = isOpaque(ds);
1503 info.mContentDirty = contentDirty;
1504 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001505}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001506
Yiwei Zhang5434a782018-12-05 18:06:32 -08001507void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001508 result.append("-------------------------------");
1509 result.append("-------------------------------");
1510 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001511 result.append(" Layer name\n");
1512 result.append(" Z | ");
1513 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001514 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001515 result.append(" Disp Frame (LTRB) | ");
1516 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001517 result.append("-------------------------------");
1518 result.append("-------------------------------");
1519 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001520}
1521
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001522void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1523 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1524 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001525 return;
1526 }
1527
Yiwei Zhang5434a782018-12-05 18:06:32 -08001528 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001529 if (mName.length() > 77) {
1530 std::string shortened;
1531 shortened.append(mName.string(), 36);
1532 shortened.append("[...]");
1533 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001534 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001535 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001536 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001537 }
1538
Yiwei Zhang5434a782018-12-05 18:06:32 -08001539 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001540
Alec Mourib416efd2018-09-06 21:01:59 +00001541 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001542 const auto& compositionState = outputLayer->getState();
1543
Chia-I Wu1e043612018-03-01 09:45:09 -08001544 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001545 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001546 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001547 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001548 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001549 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1550 StringAppendF(&result, "%10s | ",
1551 toString(getCompositionLayer() ? compositionState.bufferTransform
1552 : static_cast<Hwc2::Transform>(0))
1553 .c_str());
1554 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001555 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001556 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001557 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1558 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001559
Yichi Chen6ca35192018-05-29 12:20:43 +08001560 result.append("- - - - - - - - - - - - - - - -");
1561 result.append("- - - - - - - - - - - - - - - -");
1562 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001563}
Dan Stozae22aec72016-08-01 13:20:59 -07001564
Yiwei Zhang5434a782018-12-05 18:06:32 -08001565void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001566 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001567}
1568
Svetoslavd85084b2014-03-20 10:28:31 -07001569void Layer::clearFrameStats() {
1570 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001571}
1572
Jamie Gennis6547ff42013-07-16 20:12:42 -07001573void Layer::logFrameStats() {
1574 mFrameTracker.logAndResetStats(mName);
1575}
1576
Svetoslavd85084b2014-03-20 10:28:31 -07001577void Layer::getFrameStats(FrameStats* outStats) const {
1578 mFrameTracker.getStats(outStats);
1579}
1580
Yiwei Zhang5434a782018-12-05 18:06:32 -08001581void Layer::dumpFrameEvents(std::string& result) {
1582 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001583 Mutex::Autolock lock(mFrameEventHistoryMutex);
1584 mFrameEventHistory.checkFencesForCompletion();
1585 mFrameEventHistory.dump(result);
1586}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001587
Brian Anderson5ea5e592016-12-01 16:54:33 -08001588void Layer::onDisconnect() {
1589 Mutex::Autolock lock(mFrameEventHistoryMutex);
1590 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001591 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001592}
1593
Brian Anderson3890c392016-07-25 12:48:08 -07001594void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001595 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001596 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001597 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1598 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001599 }
1600
Brian Andersond6927fb2016-07-23 23:37:30 -07001601 Mutex::Autolock lock(mFrameEventHistoryMutex);
1602 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001603 // If there are any unsignaled fences in the aquire timeline at this
1604 // point, the previously queued frame hasn't been latched yet. Go ahead
1605 // and try to get the signal time here so the syscall is taken out of
1606 // the main thread's critical path.
1607 mAcquireTimeline.updateSignalTimes();
1608 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001609 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001610 mFrameEventHistory.addQueue(*newTimestamps);
1611 }
1612
Brian Anderson3890c392016-07-25 12:48:08 -07001613 if (outDelta) {
1614 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001615 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001616}
Dan Stozae77c7662016-05-13 11:37:28 -07001617
Chia-I Wu98f1c102017-05-30 14:54:08 -07001618size_t Layer::getChildrenCount() const {
1619 size_t count = 0;
1620 for (const sp<Layer>& child : mCurrentChildren) {
1621 count += 1 + child->getChildrenCount();
1622 }
1623 return count;
1624}
1625
Robert Carr1f0a16a2016-10-24 16:27:39 -07001626void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001627 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001628 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001629
Robert Carr1f0a16a2016-10-24 16:27:39 -07001630 mCurrentChildren.add(layer);
1631 layer->setParent(this);
1632}
1633
1634ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001635 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001636 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001637
Robert Carr1323c952019-01-28 18:13:27 -08001638 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639 return mCurrentChildren.remove(layer);
1640}
1641
Robert Carr1db73f62016-12-21 12:58:51 -08001642bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1643 sp<Handle> handle = nullptr;
1644 sp<Layer> newParent = nullptr;
1645 if (newParentHandle == nullptr) {
1646 return false;
1647 }
1648 handle = static_cast<Handle*>(newParentHandle.get());
1649 newParent = handle->owner.promote();
1650 if (newParent == nullptr) {
1651 ALOGE("Unable to promote Layer handle");
1652 return false;
1653 }
1654
chaviw5aedec92018-10-22 10:40:38 -07001655 if (attachChildren()) {
1656 setTransactionFlags(eTransactionNeeded);
1657 }
Robert Carr1db73f62016-12-21 12:58:51 -08001658 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001659 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001660 }
1661 mCurrentChildren.clear();
1662
1663 return true;
1664}
1665
Robert Carr15eae092018-03-23 13:43:53 -07001666void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001667 for (const sp<Layer>& child : mDrawingChildren) {
1668 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001669 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001670 }
1671}
1672
chaviwf1961f72017-09-18 16:41:07 -07001673bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001674 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001675
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001676 // While layers are detached, we allow most operations
1677 // and simply halt performing the actual transaction. However
1678 // for reparent != null we would enter the mRemovedFromCurrentState
1679 // state, regardless of whether doTransaction was called, and
1680 // so we need to prevent the update here.
1681 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001682 return false;
1683 }
1684
Robert Carr54cf5b12019-01-25 14:02:28 -08001685 sp<Layer> newParent;
1686 if (newParentHandle != nullptr) {
1687 auto handle = static_cast<Handle*>(newParentHandle.get());
1688 newParent = handle->owner.promote();
1689 if (newParent == nullptr) {
1690 ALOGE("Unable to promote Layer handle");
1691 return false;
1692 }
1693 if (newParent == this) {
1694 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1695 return false;
1696 }
1697 }
1698
chaviwf1961f72017-09-18 16:41:07 -07001699 sp<Layer> parent = getParent();
1700 if (parent != nullptr) {
1701 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001702 }
1703
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001704 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001705 newParent->addChild(this);
1706 if (!newParent->isRemovedFromCurrentState()) {
1707 addToCurrentState();
1708 } else {
1709 onRemovedFromCurrentState();
1710 }
1711
1712 if (mLayerDetached) {
1713 mLayerDetached = false;
1714 callSetTransactionFlags = true;
1715 }
1716 } else {
1717 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001718 }
1719
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001720 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001721 setTransactionFlags(eTransactionNeeded);
1722 }
chaviw06178942017-07-27 10:25:59 -07001723 return true;
1724}
1725
Robert Carr9524cb32017-02-13 11:32:32 -08001726bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001727 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001728 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001729 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001730 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001731 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001732 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001733 }
Robert Carr7f619b22017-11-06 12:56:35 -08001734 }
Robert Carr9524cb32017-02-13 11:32:32 -08001735
1736 return true;
1737}
1738
chaviw5aedec92018-10-22 10:40:38 -07001739bool Layer::attachChildren() {
1740 bool changed = false;
1741 for (const sp<Layer>& child : mCurrentChildren) {
1742 sp<Client> parentClient = mClientRef.promote();
1743 sp<Client> client(child->mClientRef.promote());
1744 if (client != nullptr && parentClient != client) {
1745 if (child->mLayerDetached) {
1746 child->mLayerDetached = false;
1747 changed = true;
1748 }
1749 changed |= child->attachChildren();
1750 }
1751 }
1752
1753 return changed;
1754}
1755
Peiyong Lind3788632018-09-18 16:01:31 -07001756bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001757 static const mat4 identityMatrix = mat4();
1758
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001759 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001760 return false;
1761 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001762 ++mCurrentState.sequence;
1763 mCurrentState.colorTransform = matrix;
1764 mCurrentState.hasColorTransform = matrix != identityMatrix;
1765 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001766 setTransactionFlags(eTransactionNeeded);
1767 return true;
1768}
1769
chaviwf66724d2018-11-28 16:35:21 -08001770mat4 Layer::getColorTransform() const {
1771 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1772 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1773 colorTransform = parent->getColorTransform() * colorTransform;
1774 }
1775 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001776}
1777
1778bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001779 bool hasColorTransform = getDrawingState().hasColorTransform;
1780 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1781 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1782 }
1783 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001784}
1785
Chia-I Wu11481472018-05-04 10:43:19 -07001786bool Layer::isLegacyDataSpace() const {
1787 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001788 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001789 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001790}
1791
Robert Carr1f0a16a2016-10-24 16:27:39 -07001792void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001793 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001794}
1795
Robert Carr1f0a16a2016-10-24 16:27:39 -07001796int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001797 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001798}
1799
Robert Carr29abff82017-12-04 13:51:20 -08001800bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1801 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001802 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001803 return state.zOrderRelativeOf != nullptr;
1804}
1805
David Sodman41fdfc92017-11-06 16:09:56 -08001806__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001807 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001808 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1809 "makeTraversalList received invalid stateSet");
1810 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1811 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001812 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001813
Robert Carr29abff82017-12-04 13:51:20 -08001814 if (state.zOrderRelatives.size() == 0) {
1815 *outSkipRelativeZUsers = true;
1816 return children;
1817 }
1818
chaviwfd462612018-05-31 16:11:27 -07001819 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001820 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001821 sp<Layer> strongRelative = weakRelative.promote();
1822 if (strongRelative != nullptr) {
1823 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001824 }
1825 }
1826
Dan Stoza412903f2017-04-27 13:42:17 -07001827 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001828 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001829 if (childState.zOrderRelativeOf != nullptr) {
1830 continue;
1831 }
Robert Carrdb66e622017-04-10 16:55:57 -07001832 traverse.add(child);
1833 }
1834
1835 return traverse;
1836}
1837
Robert Carr1f0a16a2016-10-24 16:27:39 -07001838/**
Robert Carrdb66e622017-04-10 16:55:57 -07001839 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001840 */
Dan Stoza412903f2017-04-27 13:42:17 -07001841void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001842 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1843 // produce a new list for traversing, including our relatives, and not including our children
1844 // who are relatives of another surface. In the case that there are no relative Z,
1845 // makeTraversalList returns our children directly to avoid significant overhead.
1846 // However in this case we need to take the responsibility for filtering children which
1847 // are relatives of another surface here.
1848 bool skipRelativeZUsers = false;
1849 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001850
Robert Carr1f0a16a2016-10-24 16:27:39 -07001851 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001852 for (; i < list.size(); i++) {
1853 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001854 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1855 continue;
1856 }
1857
Robert Carrdb66e622017-04-10 16:55:57 -07001858 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001859 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001860 }
Dan Stoza412903f2017-04-27 13:42:17 -07001861 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001862 }
Robert Carr29abff82017-12-04 13:51:20 -08001863
Dan Stoza412903f2017-04-27 13:42:17 -07001864 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001865 for (; i < list.size(); i++) {
1866 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001867
Robert Carr29abff82017-12-04 13:51:20 -08001868 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1869 continue;
1870 }
Dan Stoza412903f2017-04-27 13:42:17 -07001871 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001872 }
1873}
1874
1875/**
Robert Carrdb66e622017-04-10 16:55:57 -07001876 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001877 */
Dan Stoza412903f2017-04-27 13:42:17 -07001878void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1879 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001880 // See traverseInZOrder for documentation.
1881 bool skipRelativeZUsers = false;
1882 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001883
Robert Carr1f0a16a2016-10-24 16:27:39 -07001884 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001885 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001886 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001887
1888 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1889 continue;
1890 }
1891
Robert Carrdb66e622017-04-10 16:55:57 -07001892 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001893 break;
1894 }
Dan Stoza412903f2017-04-27 13:42:17 -07001895 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001896 }
Dan Stoza412903f2017-04-27 13:42:17 -07001897 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001898 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001899 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001900
1901 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1902 continue;
1903 }
1904
Dan Stoza412903f2017-04-27 13:42:17 -07001905 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001906 }
1907}
1908
chaviw4b129c22018-04-09 16:19:43 -07001909LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1910 const std::vector<Layer*>& layersInTree) {
1911 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1912 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001913 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1914 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001915 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001916
chaviwfd462612018-05-31 16:11:27 -07001917 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001918 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1919 sp<Layer> strongRelative = weakRelative.promote();
1920 // Only add relative layers that are also descendents of the top most parent of the tree.
1921 // If a relative layer is not a descendent, then it should be ignored.
1922 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1923 traverse.add(strongRelative);
1924 }
1925 }
1926
1927 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001928 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001929 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1930 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1931 // the child here since it won't be added later as a relative.
1932 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1933 childState.zOrderRelativeOf.promote().get())) {
1934 continue;
1935 }
1936 traverse.add(child);
1937 }
1938
1939 return traverse;
1940}
1941
1942void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1943 LayerVector::StateSet stateSet,
1944 const LayerVector::Visitor& visitor) {
1945 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001946
1947 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001948 for (; i < list.size(); i++) {
1949 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001950 if (relative->getZ() >= 0) {
1951 break;
1952 }
chaviw4b129c22018-04-09 16:19:43 -07001953 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001954 }
chaviw4b129c22018-04-09 16:19:43 -07001955
chaviwa76b2712017-09-20 12:02:26 -07001956 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001957 for (; i < list.size(); i++) {
1958 const auto& relative = list[i];
1959 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001960 }
1961}
1962
chaviw4b129c22018-04-09 16:19:43 -07001963std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1964 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1965 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1966
1967 std::vector<Layer*> layersInTree = {this};
1968 for (size_t i = 0; i < children.size(); i++) {
1969 const auto& child = children[i];
1970 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1971 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1972 }
1973
1974 return layersInTree;
1975}
1976
1977void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1978 const LayerVector::Visitor& visitor) {
1979 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1980 std::sort(layersInTree.begin(), layersInTree.end());
1981 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1982}
1983
Peiyong Linefefaac2018-08-17 12:27:51 -07001984ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001985 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001986}
1987
chaviw13fdc492017-06-27 12:40:18 -07001988half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001989 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001990
chaviw13fdc492017-06-27 12:40:18 -07001991 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1992 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001993}
Robert Carr6452f122017-03-21 10:41:29 -07001994
chaviw13fdc492017-06-27 12:40:18 -07001995half4 Layer::getColor() const {
1996 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001997 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001998}
Robert Carr6452f122017-03-21 10:41:29 -07001999
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002000Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2001 const auto& p = mDrawingParent.promote();
2002 if (p != nullptr) {
2003 RoundedCornerState parentState = p->getRoundedCornerState();
2004 if (parentState.radius > 0) {
2005 ui::Transform t = getActiveTransform(getDrawingState());
2006 t = t.inverse();
2007 parentState.cropRect = t.transform(parentState.cropRect);
2008 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2009 // but a transform matrix can define horizontal and vertical scales.
2010 // Let's take the average between both of them and pass into the shader, practically we
2011 // never do this type of transformation on windows anyway.
2012 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2013 return parentState;
2014 }
2015 }
2016 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002017 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002018}
2019
Robert Carr1f0a16a2016-10-24 16:27:39 -07002020void Layer::commitChildList() {
2021 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2022 const auto& child = mCurrentChildren[i];
2023 child->commitChildList();
2024 }
2025 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002026 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002027}
2028
Robert Carr720e5062018-07-30 17:45:14 -07002029void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002030 mCurrentState.inputInfo = info;
2031 mCurrentState.modified = true;
2032 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002033 setTransactionFlags(eTransactionNeeded);
2034}
2035
chaviw1d044282017-09-27 12:19:28 -07002036void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2037 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2038 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002039 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002040
Peiyong Linefefaac2018-08-17 12:27:51 -07002041 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002042 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002043
2044 layerInfo->set_id(sequence);
2045 layerInfo->set_name(getName().c_str());
2046 layerInfo->set_type(String8(getTypeId()));
2047
2048 for (const auto& child : children) {
2049 layerInfo->add_children(child->sequence);
2050 }
2051
2052 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2053 sp<Layer> strongRelative = weakRelative.promote();
2054 if (strongRelative != nullptr) {
2055 layerInfo->add_relatives(strongRelative->sequence);
2056 }
2057 }
2058
Marissa Wallf58c14b2018-07-24 10:50:43 -07002059 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08002060 [&]() { return layerInfo->mutable_transparent_region(); });
2061 LayerProtoHelper::writeToProto(visibleRegion,
2062 [&]() { return layerInfo->mutable_visible_region(); });
2063 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2064 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07002065
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002066 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002067 layerInfo->set_z(state.z);
2068
Nataniel Borges797b0e42019-02-15 14:11:58 -08002069 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2070 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07002071
Nataniel Borges797b0e42019-02-15 14:11:58 -08002072 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2073 return layerInfo->mutable_requested_position();
2074 });
chaviw1d044282017-09-27 12:19:28 -07002075
Nataniel Borges797b0e42019-02-15 14:11:58 -08002076 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2077 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07002078
Nataniel Borges797b0e42019-02-15 14:11:58 -08002079 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002080 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002081
2082 layerInfo->set_is_opaque(isOpaque(state));
2083 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002084
2085 // XXX (b/79210409) mCurrentDataSpace is not protected
2086 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2087
chaviw1d044282017-09-27 12:19:28 -07002088 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08002089 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2090 LayerProtoHelper::writeToProto(state.color,
2091 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07002092 layerInfo->set_flags(state.flags);
2093
2094 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2095 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2096
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002097 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002098 if (parent != nullptr) {
2099 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002100 } else {
2101 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002102 }
2103
2104 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2105 if (zOrderRelativeOf != nullptr) {
2106 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002107 } else {
2108 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002109 }
2110
Lloyd Pique0b785d82018-12-04 17:25:27 -08002111 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002112 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08002113 LayerProtoHelper::writeToProto(buffer,
2114 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07002115 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002116 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002117 }
2118
2119 layerInfo->set_queued_frames(getQueuedFrameCount());
2120 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002121 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002122 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002123
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002124 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002125 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002126 if (barrierLayer != nullptr) {
2127 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2128 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002129 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002130 }
2131 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002132
2133 auto protoMap = layerInfo->mutable_metadata();
2134 for (const auto& entry : state.metadata.mMap) {
2135 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2136 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002137 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08002138 LayerProtoHelper::writeToProto(mSourceBounds,
2139 [&]() { return layerInfo->mutable_source_bounds(); });
2140 LayerProtoHelper::writeToProto(mScreenBounds,
2141 [&]() { return layerInfo->mutable_screen_bounds(); });
2142 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07002143}
2144
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002145void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2146 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2147 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002148 return;
2149 }
2150
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002151 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2152
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002153 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002154
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002155 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002156 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002157
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002158 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002159 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002160
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002161 const int32_t transform =
2162 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002163 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002164
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002165 const int32_t compositionType =
2166 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2167 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002168 layerInfo->set_hwc_composition_type(compositionType);
2169
2170 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2171 static_cast<BufferLayer*>(this)->isProtected()) {
2172 layerInfo->set_is_protected(true);
2173 } else {
2174 layerInfo->set_is_protected(false);
2175 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002176}
2177
Robert Carr2e102c92018-10-23 12:11:15 -07002178bool Layer::isRemovedFromCurrentState() const {
2179 return mRemovedFromCurrentState;
2180}
2181
Arthur Hungd20b2702019-01-14 18:16:16 +08002182InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002183 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002184
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002185 if (info.displayId == ADISPLAY_ID_NONE) {
2186 info.displayId = mDrawingState.layerStack;
2187 }
2188
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002189 ui::Transform t = getTransform();
2190 const float xScale = t.sx();
2191 const float yScale = t.sy();
2192 if (xScale != 1.0f || yScale != 1.0f) {
2193 info.windowXScale *= 1.0f / xScale;
2194 info.windowYScale *= 1.0f / yScale;
2195 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002196 }
Robert Carre07e1032018-11-26 12:55:53 -08002197
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002198 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002199 // If this is a portal window, set the touchableRegion to the layerBounds.
2200 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2201 ? getBufferSize(getDrawingState())
2202 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002203 if (!layerBounds.isValid()) {
2204 layerBounds = getCroppedBufferSize(getDrawingState());
2205 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002206 layerBounds = t.transform(layerBounds);
2207 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2208
Arthur Hungd20b2702019-01-14 18:16:16 +08002209 // Input coordinate should match the layer bounds.
2210 info.frameLeft = layerBounds.left;
2211 info.frameTop = layerBounds.top;
2212 info.frameRight = layerBounds.right;
2213 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002214
2215 // Position the touchable region relative to frame screen location and restrict it to frame
2216 // bounds.
2217 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002218 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002219 return info;
2220}
2221
2222bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002223 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002224}
2225
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002226std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2227 return nullptr;
2228}
2229
chaviw3e727cd2019-01-31 13:41:05 -08002230bool Layer::canReceiveInput() const {
2231 return isVisible();
2232}
2233
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002234compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2235 const sp<const DisplayDevice>& display) const {
2236 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2237}
2238
Mathias Agopian13127d82013-03-05 17:47:11 -08002239// ---------------------------------------------------------------------------
2240
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002241}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002242
2243#if defined(__gl_h_)
2244#error "don't include gl/gl.h in this file"
2245#endif
2246
2247#if defined(__gl2_h_)
2248#error "don't include gl2/gl2.h in this file"
2249#endif