blob: d28a4848b09cf57b8b24e5d0fd402c83949db1a2 [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 Nairc652ff82019-03-15 12:48:54 -0700280ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800281 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
282 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700283 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800284 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700285 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800286 }
287
Marissa Wall290ad082019-03-06 13:23:47 -0800288 // If the layer is a buffer state layer, the active width and height
289 // could be infinite. In that case, return the effective transform.
290 const uint32_t activeWidth = getActiveWidth(getDrawingState());
291 const uint32_t activeHeight = getActiveHeight(getDrawingState());
292 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700293 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800294 }
295
Vishnu Nairc652ff82019-03-15 12:48:54 -0700296 int bufferWidth = mActiveBuffer->getWidth();
297 int bufferHeight = mActiveBuffer->getHeight();
298
299 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
300 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800301 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700302
Marissa Wall290ad082019-03-06 13:23:47 -0800303 float sx = activeWidth / static_cast<float>(bufferWidth);
304 float sy = activeHeight / static_cast<float>(bufferHeight);
305
Vishnu Nair4351ad52019-02-11 14:13:02 -0800306 ui::Transform extraParentScaling;
307 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700308 return extraParentScaling;
309}
310
311ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
312 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
313 // treat child surfaces as pixels in the parent surface.
314 if (!isFixedSize() || !mActiveBuffer) {
315 return mEffectiveTransform;
316 }
317 return mEffectiveTransform * bufferScaleTransform;
318}
319
320FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
321 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
322 // cropped to its parent layer after any buffer transform scaling is applied.
323 if (!isFixedSize() || !mActiveBuffer) {
324 return mBounds;
325 }
326 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800327}
328
329void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
330 const State& s(getDrawingState());
331
332 // Calculate effective layer transform
333 mEffectiveTransform = parentTransform * getActiveTransform(s);
334
335 // Transform parent bounds to layer space
336 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
337
Vishnu Nairc652ff82019-03-15 12:48:54 -0700338 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800339 mSourceBounds = computeSourceBounds(parentBounds);
340
341 // Calculate bounds by croping diplay frame with layer crop and parent bounds
342 FloatRect bounds = mSourceBounds;
343 const Rect layerCrop = getCrop(s);
344 if (!layerCrop.isEmpty()) {
345 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
346 }
347 bounds = bounds.intersect(parentBounds);
348
349 mBounds = bounds;
350 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700351
352 // Add any buffer scaling to the layer's children.
353 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800354 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700355 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
356 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800357 }
358}
359
Vishnu Nair60356342018-11-13 13:00:45 -0800360Rect Layer::getCroppedBufferSize(const State& s) const {
361 Rect size = getBufferSize(s);
362 Rect crop = getCrop(s);
363 if (!crop.isEmpty() && size.isValid()) {
364 size.intersect(crop, &size);
365 } else if (!crop.isEmpty()) {
366 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700367 }
Vishnu Nair60356342018-11-13 13:00:45 -0800368 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800369}
370
Alec Mouri1f3bd182019-01-24 15:20:18 +0000371Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700372 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800373 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700374 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800375
376 // apply the projection's clipping to the window crop in
377 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700378 // if there are no window scaling involved, this operation will map to full
379 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700380
Vishnu Nair4351ad52019-02-11 14:13:02 -0800381 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000382 ui::Transform t = getTransform();
383 // Transform to screen space.
384 activeCropFloat = t.transform(activeCropFloat);
385 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
386 // Back to layer space to work with the content crop.
387 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800388 // This needs to be here as transform.transform(Rect) computes the
389 // transformed rect and then takes the bounding box of the result before
390 // returning. This means
391 // transform.inverse().transform(transform.transform(Rect)) != Rect
392 // in which case we need to make sure the final rect is clipped to the
393 // display bounds.
394 Rect activeCrop{activeCropFloat};
395 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000396 activeCrop.clear();
397 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700398 return activeCrop;
399}
400
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700401void Layer::setupRoundedCornersCropCoordinates(Rect win,
402 const FloatRect& roundedCornersCrop) const {
403 // Translate win by the rounded corners rect coordinates, to have all values in
404 // layer coordinate space.
405 win.left -= roundedCornersCrop.left;
406 win.right -= roundedCornersCrop.left;
407 win.top -= roundedCornersCrop.top;
408 win.bottom -= roundedCornersCrop.top;
409
Lloyd Pique0b785d82018-12-04 17:25:27 -0800410 renderengine::Mesh::VertexArray<vec2> cropCoords(
411 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700412 cropCoords[0] = vec2(win.left, win.top);
413 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
414 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
415 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700416}
417
Alec Mouri1f3bd182019-01-24 15:20:18 +0000418FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700419 // the content crop is the area of the content that gets scaled to the
420 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800421 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700422
423 // In addition there is a WM-specified crop we pull from our drawing state.
424 const State& s(getDrawingState());
425
Alec Mouri1f3bd182019-01-24 15:20:18 +0000426 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800427 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700428
Marissa Wall290ad082019-03-06 13:23:47 -0800429 int32_t winWidth = bufferSize.getWidth();
430 int32_t winHeight = bufferSize.getHeight();
431
432 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1]) if display frame
433 // hasn't been set and the parent is an unbounded layer.
434 if (winWidth < 0 && winHeight < 0) {
435 return crop;
436 }
437
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 // Transform the window crop to match the buffer coordinate system,
439 // which means using the inverse of the current transform set on the
440 // SurfaceFlingerConsumer.
441 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800442 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700444 * the code below applies the primary display's inverse transform to the
445 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 */
David Sodman41fdfc92017-11-06 16:09:56 -0800447 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 // calculate the inverse transform
449 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800450 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800451 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700453 invTransform = (ui::Transform(invTransformOrient) *
454 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800455 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000456
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
458 // If the activeCrop has been rotate the ends are rotated but not
459 // the space itself so when transforming ends back we can't rely on
460 // a modification of the axes of rotation. To account for this we
461 // need to reorient the inverse rotation in terms of the current
462 // axes of rotation.
463 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
464 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
465 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800466 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800468 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000469 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800470 const Rect winCrop =
471 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000472
473 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800474 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000475 float yScale = crop.getHeight() / float(winHeight);
476
David Sodman41fdfc92017-11-06 16:09:56 -0800477 float insetL = winCrop.left * xScale;
478 float insetT = winCrop.top * yScale;
479 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000480 float insetB = (winHeight - winCrop.bottom) * yScale;
481
David Sodman41fdfc92017-11-06 16:09:56 -0800482 crop.left += insetL;
483 crop.top += insetT;
484 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000485 crop.bottom -= insetB;
486
Mathias Agopian13127d82013-03-05 17:47:11 -0800487 return crop;
488}
489
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700490void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800491 const auto outputLayer = findOutputLayerForDisplay(display);
492 LOG_FATAL_IF(!outputLayer);
493 LOG_FATAL_IF(!outputLayer->getState().hwc);
494 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700495
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800496 if (!hasHwcLayer(display)) {
497 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
498 display->getDebugName().c_str());
499 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800500 }
501
Lloyd Pique0b785d82018-12-04 17:25:27 -0800502 LOG_FATAL_IF(!getCompositionLayer());
503 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800504 auto& compositionState = outputLayer->editState();
505
506 // enable this layer
507 compositionState.forceClientComposition = false;
508
509 if (isSecure() && !display->isSecure()) {
510 compositionState.forceClientComposition = true;
511 }
David Sodman15094112018-10-11 09:39:37 -0700512
Mathias Agopian13127d82013-03-05 17:47:11 -0800513 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700514 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800515 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500516 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800517 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800518 blendMode =
519 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800520 }
David Sodman15094112018-10-11 09:39:37 -0700521 auto error = hwcLayer->setBlendMode(blendMode);
522 ALOGE_IF(error != HWC2::Error::None,
523 "[%s] Failed to set blend mode %s:"
524 " %s (%d)",
525 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
526 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800527 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800528
529 // apply the layer's transform, followed by the display's global transform
530 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700531 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800532 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700533 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800534 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700535 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700536 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000537 activeCrop.clear();
538 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700539 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800540 // This needs to be here as transform.transform(Rect) computes the
541 // transformed rect and then takes the bounding box of the result before
542 // returning. This means
543 // transform.inverse().transform(transform.transform(Rect)) != Rect
544 // in which case we need to make sure the final rect is clipped to the
545 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800546 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000547 activeCrop.clear();
548 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700549 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800550 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
551 activeTransparentRegion.orSelf(
552 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800553 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
554 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800555 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700556 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700557
Vishnu Nair4351ad52019-02-11 14:13:02 -0800558 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800559 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800560 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700561 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000562 frame.clear();
563 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700564 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700566 error = hwcLayer->setDisplayFrame(transformedFrame);
567 if (error != HWC2::Error::None) {
568 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
569 transformedFrame.left, transformedFrame.top, transformedFrame.right,
570 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
571 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800572 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700573 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800574
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700575 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700576 error = hwcLayer->setSourceCrop(sourceCrop);
577 if (error != HWC2::Error::None) {
578 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
579 "%s (%d)",
580 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
581 to_string(error).c_str(), static_cast<int32_t>(error));
582 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800583 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700584 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800586 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700587 error = hwcLayer->setPlaneAlpha(alpha);
588 ALOGE_IF(error != HWC2::Error::None,
589 "[%s] Failed to set plane alpha %.3f: "
590 "%s (%d)",
591 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800592 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593
David Sodman15094112018-10-11 09:39:37 -0700594 error = hwcLayer->setZOrder(z);
595 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
596 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800597 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500598
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800599 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
600 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700601 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400602 if (parent.get()) {
603 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800604 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
605 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
606 if (parentType >= 0 || parentAppId >= 0) {
607 type = parentType;
608 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700609 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400610 }
611
David Sodman15094112018-10-11 09:39:37 -0700612 error = hwcLayer->setInfo(type, appId);
613 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
614 static_cast<int32_t>(error));
615
Lloyd Pique0b785d82018-12-04 17:25:27 -0800616 commonCompositionState.type = type;
617 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700618
Mathias Agopian29a367b2011-07-12 14:51:45 -0700619 /*
620 * Transformations are applied in this order:
621 * 1) buffer orientation/flip/mirror
622 * 2) state transformation (window manager)
623 * 3) layer orientation (screen orientation)
624 * (NOTE: the matrices are multiplied in reverse order)
625 */
626
Peiyong Linefefaac2018-08-17 12:27:51 -0700627 const ui::Transform bufferOrientation(mCurrentTransform);
628 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700629
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800630 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700631 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700632 * the code below applies the primary display's inverse transform to the
633 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700634 */
David Sodman41fdfc92017-11-06 16:09:56 -0800635 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636 // calculate the inverse transform
637 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800638 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700639 }
Robert Carrcae605c2017-03-29 12:10:31 -0700640
641 /*
642 * Here we cancel out the orientation component of the WM transform.
643 * The scaling and translate components are already included in our bounds
644 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000645 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700646 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700647 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700648 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700649
650 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800651 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700652 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800654 compositionState.forceClientComposition = true;
655 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656 } else {
657 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700658 auto error = hwcLayer->setTransform(transform);
659 ALOGE_IF(error != HWC2::Error::None,
660 "[%s] Failed to set transform %s: "
661 "%s (%d)",
662 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
663 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800664 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800665 }
666}
667
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800668void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
669 const auto outputLayer = findOutputLayerForDisplay(display);
670 LOG_FATAL_IF(!outputLayer);
671 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672}
Dan Stozaee44edd2015-03-23 15:50:23 -0700673
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800674bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
675 const auto outputLayer = findOutputLayerForDisplay(display);
676 LOG_FATAL_IF(!outputLayer);
677 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800678}
679
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700680void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800681 const auto outputLayer = findOutputLayerForDisplay(display);
682 LOG_FATAL_IF(!outputLayer);
683
684 if (!outputLayer->getState().hwc ||
685 (*outputLayer->getState().hwc).hwcCompositionType !=
686 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687 return;
688 }
689
690 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800691 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800692
693 // Apply the layer's transform, followed by the display's global transform
694 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800695 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800696 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700697 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800698 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700699 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700700 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800701 auto position = displayTransform.transform(frame);
702
Dominik Laskowski7e045462018-05-30 13:02:02 -0700703 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800704 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800705 ALOGE_IF(error != HWC2::Error::None,
706 "[%s] Failed to set cursor position "
707 "to (%d, %d): %s (%d)",
708 mName.string(), position.left, position.top, to_string(error).c_str(),
709 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710}
Riley Andrews03414a12014-07-01 14:22:59 -0700711
Mathias Agopian13127d82013-03-05 17:47:11 -0800712// ---------------------------------------------------------------------------
713// drawing...
714// ---------------------------------------------------------------------------
715
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000716bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800717 Region& clearRegion, const bool supportProtectedContent,
718 renderengine::LayerSettings& layer) {
719 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800720}
721
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000722bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800723 Region& clearRegion, const bool supportProtectedContent,
724 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000725 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800726 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000727}
728
729bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
730 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800731 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000732 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800733 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000734 half alpha = getAlpha();
735 layer.geometry.boundaries = bounds;
736 if (useIdentityTransform) {
737 layer.geometry.positionTransform = mat4();
738 } else {
739 const ui::Transform transform = getTransform();
740 mat4 m;
741 m[0][0] = transform[0][0];
742 m[0][1] = transform[0][1];
743 m[0][3] = transform[0][2];
744 m[1][0] = transform[1][0];
745 m[1][1] = transform[1][1];
746 m[1][3] = transform[1][2];
747 m[3][0] = transform[2][0];
748 m[3][1] = transform[2][1];
749 m[3][3] = transform[2][2];
750 layer.geometry.positionTransform = m;
751 }
752
753 if (hasColorTransform()) {
754 layer.colorTransform = getColorTransform();
755 }
756
757 const auto roundedCornerState = getRoundedCornerState();
758 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
759 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
760
761 layer.alpha = alpha;
762 layer.sourceDataspace = mCurrentDataSpace;
763 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800764}
765
David Sodman41fdfc92017-11-06 16:09:56 -0800766void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
767 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800768 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800769 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700770 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800771 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800772}
773
David Sodmanc1498e62018-09-12 14:36:26 -0700774void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
775 clearWithOpenGL(renderArea, 0, 0, 0, 0);
776}
777
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800778void Layer::setCompositionType(const sp<const DisplayDevice>& display,
779 Hwc2::IComposerClient::Composition type) {
780 const auto outputLayer = findOutputLayerForDisplay(display);
781 LOG_FATAL_IF(!outputLayer);
782 LOG_FATAL_IF(!outputLayer->getState().hwc);
783 auto& compositionState = outputLayer->editState();
784
785 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
786 toString(type).c_str(), 1);
787 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700788 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800789 (*compositionState.hwc).hwcCompositionType = type;
790
791 auto error = (*compositionState.hwc)
792 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
793 ALOGE_IF(error != HWC2::Error::None,
794 "[%s] Failed to set "
795 "composition type %s: %s (%d)",
796 mName.string(), toString(type).c_str(), to_string(error).c_str(),
797 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800798 }
799}
800
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800801Hwc2::IComposerClient::Composition Layer::getCompositionType(
802 const sp<const DisplayDevice>& display) const {
803 const auto outputLayer = findOutputLayerForDisplay(display);
804 LOG_FATAL_IF(!outputLayer);
805 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
806 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800807}
808
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800809bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
810 const auto outputLayer = findOutputLayerForDisplay(display);
811 LOG_FATAL_IF(!outputLayer);
812 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800813}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800814
Dan Stozacac35382016-01-27 12:21:06 -0800815bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
816 if (point->getFrameNumber() <= mCurrentFrameNumber) {
817 // Don't bother with a SyncPoint, since we've already latched the
818 // relevant frame
819 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700820 }
Robert Carr2e102c92018-10-23 12:11:15 -0700821 if (isRemovedFromCurrentState()) {
822 return false;
823 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700824
Dan Stozacac35382016-01-27 12:21:06 -0800825 Mutex::Autolock lock(mLocalSyncPointMutex);
826 mLocalSyncPoints.push_back(point);
827 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700828}
829
Mathias Agopian13127d82013-03-05 17:47:11 -0800830// ----------------------------------------------------------------------------
831// local state
832// ----------------------------------------------------------------------------
833
Peiyong Lin833074a2018-08-28 11:53:54 -0700834void Layer::computeGeometry(const RenderArea& renderArea,
835 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700836 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700837 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800838 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700839
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000840 vec2 lt = vec2(win.left, win.top);
841 vec2 lb = vec2(win.left, win.bottom);
842 vec2 rb = vec2(win.right, win.bottom);
843 vec2 rt = vec2(win.right, win.top);
844
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800845 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000846 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700847 lt = layerTransform.transform(lt);
848 lb = layerTransform.transform(lb);
849 rb = layerTransform.transform(rb);
850 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000851 }
852
Peiyong Lin833074a2018-08-28 11:53:54 -0700853 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700854 position[0] = renderAreaTransform.transform(lt);
855 position[1] = renderAreaTransform.transform(lb);
856 position[2] = renderAreaTransform.transform(rb);
857 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800858}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800859
David Sodman41fdfc92017-11-06 16:09:56 -0800860bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700862 return (s.flags & layer_state_t::eLayerSecure);
863}
864
Mathias Agopian13127d82013-03-05 17:47:11 -0800865void Layer::setVisibleRegion(const Region& visibleRegion) {
866 // always called from main thread
867 this->visibleRegion = visibleRegion;
868}
869
870void Layer::setCoveredRegion(const Region& coveredRegion) {
871 // always called from main thread
872 this->coveredRegion = coveredRegion;
873}
874
David Sodman41fdfc92017-11-06 16:09:56 -0800875void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800876 // always called from main thread
877 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
878}
879
Robert Carre5f4f692018-01-12 13:12:28 -0800880void Layer::clearVisibilityRegions() {
881 visibleRegion.clear();
882 visibleNonTransparentRegion.clear();
883 coveredRegion.clear();
884}
885
Mathias Agopian13127d82013-03-05 17:47:11 -0800886// ----------------------------------------------------------------------------
887// transaction
888// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800889
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800890void Layer::pushPendingState() {
891 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700892 return;
893 }
894
Dan Stoza7dde5992015-05-22 09:51:44 -0700895 // If this transaction is waiting on the receipt of a frame, generate a sync
896 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700897 // We don't allow installing sync points after we are removed from the current state
898 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800899 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
900 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800901 if (barrierLayer == nullptr) {
902 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700903 // If we can't promote the layer we are intended to wait on,
904 // then it is expired or otherwise invalid. Allow this transaction
905 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800906 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800907 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800908 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800909 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800910 mRemoteSyncPoints.push_back(std::move(syncPoint));
911 } else {
912 // We already missed the frame we're supposed to synchronize
913 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800915 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700916 }
917
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 // Wake us up to check if the frame has been received
919 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700920 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700921 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800922 mPendingStates.push_back(mCurrentState);
923 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700924}
925
Pablo Ceballos05289c22016-04-14 15:49:55 -0700926void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800927 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700928
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800929 mPendingStates.removeAt(0);
930 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700931}
932
Pablo Ceballos05289c22016-04-14 15:49:55 -0700933bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800935 while (!mPendingStates.empty()) {
936 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700937 if (mRemoteSyncPoints.empty()) {
938 // If we don't have a sync point for this, apply it anyway. It
939 // will be visually wrong, but it should keep us from getting
940 // into too much trouble.
941 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700942 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700943 stateUpdateAvailable = true;
944 continue;
945 }
946
Marissa Wallf58c14b2018-07-24 10:50:43 -0700947 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800948 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800949 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800950
951 // Signal our end of the sync point and then dispose of it
952 mRemoteSyncPoints.front()->setTransactionApplied();
953 mRemoteSyncPoints.pop_front();
954 continue;
955 }
956
Dan Stoza7dde5992015-05-22 09:51:44 -0700957 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
958 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700959 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 stateUpdateAvailable = true;
961
962 // Signal our end of the sync point and then dispose of it
963 mRemoteSyncPoints.front()->setTransactionApplied();
964 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800965 } else {
966 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700968 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700969 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700970 stateUpdateAvailable = true;
971 }
972 }
973
974 // If we still have pending updates, wake SurfaceFlinger back up and point
975 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800976 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700977 setTransactionFlags(eTransactionNeeded);
978 mFlinger->setTransactionFlags(eTraversalNeeded);
979 }
980
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800981 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700982 return stateUpdateAvailable;
983}
984
Marissa Wall61c58622018-07-18 10:12:20 -0700985uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000986 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987
Marissa Wall61c58622018-07-18 10:12:20 -0700988 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
989 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700990
David Sodmaneb085e02017-10-05 18:49:04 -0700991 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700992 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000993 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800994 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
995 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
996 " requested={ wh={%4u,%4u} }}\n"
997 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
998 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700999 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -07001000 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
1001 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
1002 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
1003 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
1004 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -07001005 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1006 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1007 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001009
Robert Carre392b552017-09-19 12:16:05 -07001010 // Don't let Layer::doTransaction update the drawing state
1011 // if we have a pending resize, unless we are in fixed-size mode.
1012 // the drawing state will be updated only once we receive a buffer
1013 // with the correct size.
1014 //
1015 // In particular, we want to make sure the clip (which is part
1016 // of the geometry state) is latched together with the size but is
1017 // latched immediately when no resizing is involved.
1018 //
1019 // If a sideband stream is attached, however, we want to skip this
1020 // optimization so that transactions aren't missed when a buffer
1021 // never arrives
1022 //
1023 // In the case that we don't have a buffer we ignore other factors
1024 // and avoid entering the resizePending state. At a high level the
1025 // resizePending state is to avoid applying the state of the new buffer
1026 // to the old buffer. However in the state where we don't have an old buffer
1027 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001028 const bool resizePending =
1029 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1030 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -08001031 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001032 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -08001033 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001034 flags |= eDontUpdateGeometryState;
1035 }
1036 }
1037
Robert Carr7bf247e2017-05-18 14:02:49 -07001038 // Here we apply various requested geometry states, depending on our
1039 // latching configuration. See Layer.h for a detailed discussion of
1040 // how geometry latching is controlled.
1041 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001042 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001043
1044 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1045 // mode, which causes attributes which normally latch regardless of scaling mode,
1046 // to be delayed. We copy the requested state to the active state making sure
1047 // to respect these rules (again see Layer.h for a detailed discussion).
1048 //
1049 // There is an awkward asymmetry in the handling of the crop states in the position
1050 // states, as can be seen below. Largely this arises from position and transform
1051 // being stored in the same data structure while having different latching rules.
1052 // b/38182305
1053 //
Marissa Wall61c58622018-07-18 10:12:20 -07001054 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001055 // applyPendingStates in the presence of deferred transactions.
1056 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001057 float tx = stateToCommit->active_legacy.transform.tx();
1058 float ty = stateToCommit->active_legacy.transform.ty();
1059 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1060 stateToCommit->active_legacy.transform.set(tx, ty);
1061 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001062 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001063 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001064 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001065 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001066 }
1067
Marissa Wall61c58622018-07-18 10:12:20 -07001068 return flags;
1069}
1070
1071uint32_t Layer::doTransaction(uint32_t flags) {
1072 ATRACE_CALL();
1073
chaviw5aedec92018-10-22 10:40:38 -07001074 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001075 return flags;
1076 }
1077
1078 if (mChildrenChanged) {
1079 flags |= eVisibleRegion;
1080 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001081 }
1082
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001084 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001085 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001086 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001087 }
1088
1089 flags = doTransactionResize(flags, &c);
1090
Alec Mourib416efd2018-09-06 21:01:59 +00001091 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001092
1093 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 // invalidate and recompute the visible regions if needed
1095 flags |= Layer::eVisibleRegion;
1096 }
1097
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001098 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001099 // invalidate and recompute the visible regions if needed
1100 flags |= eVisibleRegion;
1101 this->contentDirty = true;
1102
1103 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001104 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001105 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001106 }
1107
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001109 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001110 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001111 }
1112
Mathias Agopian13127d82013-03-05 17:47:11 -08001113 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001114 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001115 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001116 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117}
1118
Pablo Ceballos05289c22016-04-14 15:49:55 -07001119void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001120 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001121}
1122
Mathias Agopian13127d82013-03-05 17:47:11 -08001123uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001124 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001125}
1126
1127uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001128 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001129}
1130
Robert Carr82364e32016-05-15 11:27:47 -07001131bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001132 if (mCurrentState.requested_legacy.transform.tx() == x &&
1133 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001134 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001135 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001136
1137 // We update the requested and active position simultaneously because
1138 // we want to apply the position portion of the transform matrix immediately,
1139 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001140 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001141 if (immediate && !mFreezeGeometryUpdates) {
1142 // Here we directly update the active state
1143 // unlike other setters, because we store it within
1144 // the transform, but use different latching rules.
1145 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001146 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001147 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001148 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001149
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001150 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001151 setTransactionFlags(eTransactionNeeded);
1152 return true;
1153}
Robert Carr82364e32016-05-15 11:27:47 -07001154
Robert Carr1f0a16a2016-10-24 16:27:39 -07001155bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1156 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1157 if (idx < 0) {
1158 return false;
1159 }
1160 if (childLayer->setLayer(z)) {
1161 mCurrentChildren.removeAt(idx);
1162 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001163 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001164 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001165 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001166}
1167
Robert Carr503c7042017-09-27 15:06:08 -07001168bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1169 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1170 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1171 if (idx < 0) {
1172 return false;
1173 }
1174 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1175 mCurrentChildren.removeAt(idx);
1176 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001177 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001178 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001179 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001180}
1181
Robert Carrae060832016-11-28 10:51:00 -08001182bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001183 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1184 mCurrentState.sequence++;
1185 mCurrentState.z = z;
1186 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001187
1188 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001189 if (mCurrentState.zOrderRelativeOf != nullptr) {
1190 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001191 if (strongRelative != nullptr) {
1192 strongRelative->removeZOrderRelative(this);
1193 }
chaviw606e5cf2019-03-01 10:12:10 -08001194 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001195 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001196 setTransactionFlags(eTransactionNeeded);
1197 return true;
1198}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001199
Robert Carrdb66e622017-04-10 16:55:57 -07001200void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001201 mCurrentState.zOrderRelatives.remove(relative);
1202 mCurrentState.sequence++;
1203 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001204 setTransactionFlags(eTransactionNeeded);
1205}
1206
1207void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001208 mCurrentState.zOrderRelatives.add(relative);
1209 mCurrentState.modified = true;
1210 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001211 setTransactionFlags(eTransactionNeeded);
1212}
1213
chaviw606e5cf2019-03-01 10:12:10 -08001214void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1215 mCurrentState.zOrderRelativeOf = relativeOf;
1216 mCurrentState.sequence++;
1217 mCurrentState.modified = true;
1218 setTransactionFlags(eTransactionNeeded);
1219}
1220
Robert Carr503d2bd2017-12-04 15:49:47 -08001221bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001222 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1223 if (handle == nullptr) {
1224 return false;
1225 }
1226 sp<Layer> relative = handle->owner.promote();
1227 if (relative == nullptr) {
1228 return false;
1229 }
1230
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001231 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1232 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001233 return false;
1234 }
1235
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001236 mCurrentState.sequence++;
1237 mCurrentState.modified = true;
1238 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001239
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001240 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001241 if (oldZOrderRelativeOf != nullptr) {
1242 oldZOrderRelativeOf->removeZOrderRelative(this);
1243 }
chaviw606e5cf2019-03-01 10:12:10 -08001244 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001245 relative->addZOrderRelative(this);
1246
1247 setTransactionFlags(eTransactionNeeded);
1248
1249 return true;
1250}
1251
Mathias Agopian13127d82013-03-05 17:47:11 -08001252bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001253 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001254 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001255 mCurrentState.requested_legacy.w = w;
1256 mCurrentState.requested_legacy.h = h;
1257 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001258 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001259
1260 // record the new size, from this point on, when the client request
1261 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001262 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 return true;
1264}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001265bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001266 if (mCurrentState.color.a == alpha) return false;
1267 mCurrentState.sequence++;
1268 mCurrentState.color.a = alpha;
1269 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001270 setTransactionFlags(eTransactionNeeded);
1271 return true;
1272}
chaviw13fdc492017-06-27 12:40:18 -07001273
Valerie Haudd0b7572019-01-29 14:59:27 -08001274bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1275 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001276 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001277 }
1278 mCurrentState.sequence++;
1279 mCurrentState.modified = true;
1280 setTransactionFlags(eTransactionNeeded);
1281
1282 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001283 // create background color layer if one does not yet exist
1284 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1285 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001286 mCurrentState.bgColorLayer = new ColorLayer(
1287 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001288
Valerie Haudd0b7572019-01-29 14:59:27 -08001289 // add to child list
1290 addChild(mCurrentState.bgColorLayer);
1291 mFlinger->mLayersAdded = true;
1292 // set up SF to handle added color layer
1293 if (isRemovedFromCurrentState()) {
1294 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1295 }
1296 mFlinger->setTransactionFlags(eTransactionNeeded);
1297 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1298 mCurrentState.bgColorLayer->reparent(nullptr);
1299 mCurrentState.bgColorLayer = nullptr;
1300 return true;
1301 }
1302
1303 mCurrentState.bgColorLayer->setColor(color);
1304 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1305 mCurrentState.bgColorLayer->setAlpha(alpha);
1306 mCurrentState.bgColorLayer->setDataspace(dataspace);
1307
chaviw13fdc492017-06-27 12:40:18 -07001308 return true;
1309}
1310
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001311bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001312 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001313
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001314 mCurrentState.sequence++;
1315 mCurrentState.cornerRadius = cornerRadius;
1316 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001317 setTransactionFlags(eTransactionNeeded);
1318 return true;
1319}
1320
Robert Carrd4ae7f32018-06-07 16:10:57 -07001321bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1322 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001323 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001324 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1325
1326 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1327 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1328 return false;
1329 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001330 mCurrentState.sequence++;
1331 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1332 matrix.dsdy);
1333 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001334 setTransactionFlags(eTransactionNeeded);
1335 return true;
1336}
Marissa Wall61c58622018-07-18 10:12:20 -07001337
Mathias Agopian13127d82013-03-05 17:47:11 -08001338bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001339 mCurrentState.requestedTransparentRegion_legacy = transparent;
1340 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001341 setTransactionFlags(eTransactionNeeded);
1342 return true;
1343}
1344bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001345 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1346 if (mCurrentState.flags == newFlags) return false;
1347 mCurrentState.sequence++;
1348 mCurrentState.flags = newFlags;
1349 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001350 setTransactionFlags(eTransactionNeeded);
1351 return true;
1352}
Robert Carr99e27f02016-06-16 15:18:02 -07001353
Marissa Wallf58c14b2018-07-24 10:50:43 -07001354bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001355 if (mCurrentState.requestedCrop_legacy == crop) return false;
1356 mCurrentState.sequence++;
1357 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001358 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001359 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001360 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001361 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1362
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001363 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001364 setTransactionFlags(eTransactionNeeded);
1365 return true;
1366}
Robert Carr8d5227b2017-03-16 15:41:03 -07001367
Robert Carrc3574f72016-03-24 12:19:32 -07001368bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001369 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001370 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001371 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001372 return true;
1373}
1374
Evan Roskyef876c92019-01-25 17:46:06 -08001375bool Layer::setMetadata(const LayerMetadata& data) {
1376 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001377 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001378 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001379 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001380 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001381}
1382
Mathias Agopian13127d82013-03-05 17:47:11 -08001383bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001384 if (mCurrentState.layerStack == layerStack) return false;
1385 mCurrentState.sequence++;
1386 mCurrentState.layerStack = layerStack;
1387 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001388 setTransactionFlags(eTransactionNeeded);
1389 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001390}
1391
Peiyong Linc502cb72019-03-01 15:00:23 -08001392bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1393 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1394 return false;
1395 }
1396 mCurrentState.sequence++;
1397 mCurrentState.colorSpaceAgnostic = agnostic;
1398 mCurrentState.modified = true;
1399 setTransactionFlags(eTransactionNeeded);
1400 return true;
1401}
1402
Robert Carr1f0a16a2016-10-24 16:27:39 -07001403uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001404 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001405 if (p == nullptr) {
1406 return getDrawingState().layerStack;
1407 }
1408 return p->getLayerStack();
1409}
1410
Marissa Wallf58c14b2018-07-24 10:50:43 -07001411void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001412 mCurrentState.barrierLayer_legacy = barrierLayer;
1413 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001414 // We don't set eTransactionNeeded, because just receiving a deferral
1415 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001416 mCurrentState.modified = true;
1417 pushPendingState();
1418 mCurrentState.barrierLayer_legacy = nullptr;
1419 mCurrentState.frameNumber_legacy = 0;
1420 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001421}
1422
Marissa Wallf58c14b2018-07-24 10:50:43 -07001423void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001424 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001425 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001426}
1427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428// ----------------------------------------------------------------------------
1429// pageflip handling...
1430// ----------------------------------------------------------------------------
1431
Robert Carr1f0a16a2016-10-24 16:27:39 -07001432bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001433 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001434 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001435 if (parent != nullptr && parent->isHiddenByPolicy()) {
1436 return true;
1437 }
1438 return s.flags & layer_state_t::eLayerHidden;
1439}
1440
David Sodman41fdfc92017-11-06 16:09:56 -08001441uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001442 // TODO: should we do something special if mSecure is set?
1443 if (mProtectedByApp) {
1444 // need a hardware-protected path to external video sink
1445 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001446 }
Riley Andrews03414a12014-07-01 14:22:59 -07001447 if (mPotentialCursor) {
1448 usage |= GraphicBuffer::USAGE_CURSOR;
1449 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001450 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001451 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001452}
1453
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001454void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001455 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001456 // Disable setting transform hint if the debug flag is set or if the
1457 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1458 // in one orientation.
1459 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001460 // The transform hint is used to improve performance, but we can
1461 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001462 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001463 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001464 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001465 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001466 orientation = 0;
1467 }
1468 }
David Sodmaneb085e02017-10-05 18:49:04 -07001469 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001470}
1471
Mathias Agopian13127d82013-03-05 17:47:11 -08001472// ----------------------------------------------------------------------------
1473// debugging
1474// ----------------------------------------------------------------------------
1475
Marissa Wall61c58622018-07-18 10:12:20 -07001476// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001477LayerDebugInfo Layer::getLayerDebugInfo() const {
1478 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001479 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001480 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001481 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001482 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001483 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001484 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001485 info.mVisibleRegion = visibleRegion;
1486 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001487 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001488 info.mX = ds.active_legacy.transform.tx();
1489 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001490 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001491 info.mWidth = ds.active_legacy.w;
1492 info.mHeight = ds.active_legacy.h;
1493 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001494 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001495 info.mFlags = ds.flags;
1496 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001497 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001498 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1499 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1500 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1501 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001502 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001503 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001504 if (buffer != 0) {
1505 info.mActiveBufferWidth = buffer->getWidth();
1506 info.mActiveBufferHeight = buffer->getHeight();
1507 info.mActiveBufferStride = buffer->getStride();
1508 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001509 } else {
1510 info.mActiveBufferWidth = 0;
1511 info.mActiveBufferHeight = 0;
1512 info.mActiveBufferStride = 0;
1513 info.mActiveBufferFormat = 0;
1514 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001515 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001516 info.mNumQueuedFrames = getQueuedFrameCount();
1517 info.mRefreshPending = isBufferLatched();
1518 info.mIsOpaque = isOpaque(ds);
1519 info.mContentDirty = contentDirty;
1520 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001521}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001522
Yiwei Zhang5434a782018-12-05 18:06:32 -08001523void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001524 result.append("-------------------------------");
1525 result.append("-------------------------------");
1526 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001527 result.append(" Layer name\n");
1528 result.append(" Z | ");
1529 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001530 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001531 result.append(" Disp Frame (LTRB) | ");
1532 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001533 result.append("-------------------------------");
1534 result.append("-------------------------------");
1535 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001536}
1537
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001538void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1539 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1540 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001541 return;
1542 }
1543
Yiwei Zhang5434a782018-12-05 18:06:32 -08001544 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001545 if (mName.length() > 77) {
1546 std::string shortened;
1547 shortened.append(mName.string(), 36);
1548 shortened.append("[...]");
1549 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001550 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001551 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001552 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001553 }
1554
Yiwei Zhang5434a782018-12-05 18:06:32 -08001555 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001556
Alec Mourib416efd2018-09-06 21:01:59 +00001557 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001558 const auto& compositionState = outputLayer->getState();
1559
Chia-I Wu1e043612018-03-01 09:45:09 -08001560 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001561 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001562 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001563 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001564 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001565 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1566 StringAppendF(&result, "%10s | ",
1567 toString(getCompositionLayer() ? compositionState.bufferTransform
1568 : static_cast<Hwc2::Transform>(0))
1569 .c_str());
1570 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001571 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001572 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001573 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1574 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001575
Yichi Chen6ca35192018-05-29 12:20:43 +08001576 result.append("- - - - - - - - - - - - - - - -");
1577 result.append("- - - - - - - - - - - - - - - -");
1578 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001579}
Dan Stozae22aec72016-08-01 13:20:59 -07001580
Yiwei Zhang5434a782018-12-05 18:06:32 -08001581void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001582 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001583}
1584
Svetoslavd85084b2014-03-20 10:28:31 -07001585void Layer::clearFrameStats() {
1586 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001587}
1588
Jamie Gennis6547ff42013-07-16 20:12:42 -07001589void Layer::logFrameStats() {
1590 mFrameTracker.logAndResetStats(mName);
1591}
1592
Svetoslavd85084b2014-03-20 10:28:31 -07001593void Layer::getFrameStats(FrameStats* outStats) const {
1594 mFrameTracker.getStats(outStats);
1595}
1596
Yiwei Zhang5434a782018-12-05 18:06:32 -08001597void Layer::dumpFrameEvents(std::string& result) {
1598 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001599 Mutex::Autolock lock(mFrameEventHistoryMutex);
1600 mFrameEventHistory.checkFencesForCompletion();
1601 mFrameEventHistory.dump(result);
1602}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001603
Brian Anderson5ea5e592016-12-01 16:54:33 -08001604void Layer::onDisconnect() {
1605 Mutex::Autolock lock(mFrameEventHistoryMutex);
1606 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001607 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001608}
1609
Brian Anderson3890c392016-07-25 12:48:08 -07001610void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001611 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001612 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001613 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1614 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001615 }
1616
Brian Andersond6927fb2016-07-23 23:37:30 -07001617 Mutex::Autolock lock(mFrameEventHistoryMutex);
1618 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001619 // If there are any unsignaled fences in the aquire timeline at this
1620 // point, the previously queued frame hasn't been latched yet. Go ahead
1621 // and try to get the signal time here so the syscall is taken out of
1622 // the main thread's critical path.
1623 mAcquireTimeline.updateSignalTimes();
1624 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001625 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001626 mFrameEventHistory.addQueue(*newTimestamps);
1627 }
1628
Brian Anderson3890c392016-07-25 12:48:08 -07001629 if (outDelta) {
1630 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001631 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001632}
Dan Stozae77c7662016-05-13 11:37:28 -07001633
Chia-I Wu98f1c102017-05-30 14:54:08 -07001634size_t Layer::getChildrenCount() const {
1635 size_t count = 0;
1636 for (const sp<Layer>& child : mCurrentChildren) {
1637 count += 1 + child->getChildrenCount();
1638 }
1639 return count;
1640}
1641
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001643 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001644 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001645
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646 mCurrentChildren.add(layer);
1647 layer->setParent(this);
1648}
1649
1650ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001651 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001652 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001653
Robert Carr1323c952019-01-28 18:13:27 -08001654 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001655 return mCurrentChildren.remove(layer);
1656}
1657
Robert Carr1db73f62016-12-21 12:58:51 -08001658bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1659 sp<Handle> handle = nullptr;
1660 sp<Layer> newParent = nullptr;
1661 if (newParentHandle == nullptr) {
1662 return false;
1663 }
1664 handle = static_cast<Handle*>(newParentHandle.get());
1665 newParent = handle->owner.promote();
1666 if (newParent == nullptr) {
1667 ALOGE("Unable to promote Layer handle");
1668 return false;
1669 }
1670
chaviw5aedec92018-10-22 10:40:38 -07001671 if (attachChildren()) {
1672 setTransactionFlags(eTransactionNeeded);
1673 }
Robert Carr1db73f62016-12-21 12:58:51 -08001674 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001675 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001676 }
1677 mCurrentChildren.clear();
1678
1679 return true;
1680}
1681
Robert Carr15eae092018-03-23 13:43:53 -07001682void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001683 for (const sp<Layer>& child : mDrawingChildren) {
1684 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001685 child->computeBounds(newParent->mBounds,
1686 newParent->getTransformWithScale(
1687 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001688 }
1689}
1690
chaviwf1961f72017-09-18 16:41:07 -07001691bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001692 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001693
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001694 // While layers are detached, we allow most operations
1695 // and simply halt performing the actual transaction. However
1696 // for reparent != null we would enter the mRemovedFromCurrentState
1697 // state, regardless of whether doTransaction was called, and
1698 // so we need to prevent the update here.
1699 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001700 return false;
1701 }
1702
Robert Carr54cf5b12019-01-25 14:02:28 -08001703 sp<Layer> newParent;
1704 if (newParentHandle != nullptr) {
1705 auto handle = static_cast<Handle*>(newParentHandle.get());
1706 newParent = handle->owner.promote();
1707 if (newParent == nullptr) {
1708 ALOGE("Unable to promote Layer handle");
1709 return false;
1710 }
1711 if (newParent == this) {
1712 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1713 return false;
1714 }
1715 }
1716
chaviwf1961f72017-09-18 16:41:07 -07001717 sp<Layer> parent = getParent();
1718 if (parent != nullptr) {
1719 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001720 }
1721
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001722 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001723 newParent->addChild(this);
1724 if (!newParent->isRemovedFromCurrentState()) {
1725 addToCurrentState();
1726 } else {
1727 onRemovedFromCurrentState();
1728 }
1729
1730 if (mLayerDetached) {
1731 mLayerDetached = false;
1732 callSetTransactionFlags = true;
1733 }
1734 } else {
1735 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001736 }
1737
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001738 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001739 setTransactionFlags(eTransactionNeeded);
1740 }
chaviw06178942017-07-27 10:25:59 -07001741 return true;
1742}
1743
Robert Carr9524cb32017-02-13 11:32:32 -08001744bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001745 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001746 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001747 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001748 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001749 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001750 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001751 }
Robert Carr7f619b22017-11-06 12:56:35 -08001752 }
Robert Carr9524cb32017-02-13 11:32:32 -08001753
1754 return true;
1755}
1756
chaviw5aedec92018-10-22 10:40:38 -07001757bool Layer::attachChildren() {
1758 bool changed = false;
1759 for (const sp<Layer>& child : mCurrentChildren) {
1760 sp<Client> parentClient = mClientRef.promote();
1761 sp<Client> client(child->mClientRef.promote());
1762 if (client != nullptr && parentClient != client) {
1763 if (child->mLayerDetached) {
1764 child->mLayerDetached = false;
1765 changed = true;
1766 }
1767 changed |= child->attachChildren();
1768 }
1769 }
1770
1771 return changed;
1772}
1773
Peiyong Lind3788632018-09-18 16:01:31 -07001774bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001775 static const mat4 identityMatrix = mat4();
1776
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001777 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001778 return false;
1779 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001780 ++mCurrentState.sequence;
1781 mCurrentState.colorTransform = matrix;
1782 mCurrentState.hasColorTransform = matrix != identityMatrix;
1783 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001784 setTransactionFlags(eTransactionNeeded);
1785 return true;
1786}
1787
chaviwf66724d2018-11-28 16:35:21 -08001788mat4 Layer::getColorTransform() const {
1789 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1790 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1791 colorTransform = parent->getColorTransform() * colorTransform;
1792 }
1793 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001794}
1795
1796bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001797 bool hasColorTransform = getDrawingState().hasColorTransform;
1798 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1799 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1800 }
1801 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001802}
1803
Chia-I Wu11481472018-05-04 10:43:19 -07001804bool Layer::isLegacyDataSpace() const {
1805 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001806 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001807 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001808}
1809
Robert Carr1f0a16a2016-10-24 16:27:39 -07001810void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001811 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001812}
1813
Robert Carr1f0a16a2016-10-24 16:27:39 -07001814int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001815 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816}
1817
Robert Carr29abff82017-12-04 13:51:20 -08001818bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1819 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001820 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001821 return state.zOrderRelativeOf != nullptr;
1822}
1823
David Sodman41fdfc92017-11-06 16:09:56 -08001824__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001825 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001826 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1827 "makeTraversalList received invalid stateSet");
1828 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1829 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001830 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001831
Robert Carr29abff82017-12-04 13:51:20 -08001832 if (state.zOrderRelatives.size() == 0) {
1833 *outSkipRelativeZUsers = true;
1834 return children;
1835 }
1836
chaviwfd462612018-05-31 16:11:27 -07001837 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001838 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001839 sp<Layer> strongRelative = weakRelative.promote();
1840 if (strongRelative != nullptr) {
1841 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001842 }
1843 }
1844
Dan Stoza412903f2017-04-27 13:42:17 -07001845 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001846 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001847 if (childState.zOrderRelativeOf != nullptr) {
1848 continue;
1849 }
Robert Carrdb66e622017-04-10 16:55:57 -07001850 traverse.add(child);
1851 }
1852
1853 return traverse;
1854}
1855
Robert Carr1f0a16a2016-10-24 16:27:39 -07001856/**
Robert Carrdb66e622017-04-10 16:55:57 -07001857 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001858 */
Dan Stoza412903f2017-04-27 13:42:17 -07001859void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001860 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1861 // produce a new list for traversing, including our relatives, and not including our children
1862 // who are relatives of another surface. In the case that there are no relative Z,
1863 // makeTraversalList returns our children directly to avoid significant overhead.
1864 // However in this case we need to take the responsibility for filtering children which
1865 // are relatives of another surface here.
1866 bool skipRelativeZUsers = false;
1867 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001868
Robert Carr1f0a16a2016-10-24 16:27:39 -07001869 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001870 for (; i < list.size(); i++) {
1871 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001872 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1873 continue;
1874 }
1875
Robert Carrdb66e622017-04-10 16:55:57 -07001876 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001877 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001878 }
Dan Stoza412903f2017-04-27 13:42:17 -07001879 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001880 }
Robert Carr29abff82017-12-04 13:51:20 -08001881
Dan Stoza412903f2017-04-27 13:42:17 -07001882 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001883 for (; i < list.size(); i++) {
1884 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001885
Robert Carr29abff82017-12-04 13:51:20 -08001886 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1887 continue;
1888 }
Dan Stoza412903f2017-04-27 13:42:17 -07001889 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001890 }
1891}
1892
1893/**
Robert Carrdb66e622017-04-10 16:55:57 -07001894 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001895 */
Dan Stoza412903f2017-04-27 13:42:17 -07001896void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1897 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001898 // See traverseInZOrder for documentation.
1899 bool skipRelativeZUsers = false;
1900 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001901
Robert Carr1f0a16a2016-10-24 16:27:39 -07001902 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001903 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001904 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001905
1906 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1907 continue;
1908 }
1909
Robert Carrdb66e622017-04-10 16:55:57 -07001910 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001911 break;
1912 }
Dan Stoza412903f2017-04-27 13:42:17 -07001913 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001914 }
Dan Stoza412903f2017-04-27 13:42:17 -07001915 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001916 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001917 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001918
1919 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1920 continue;
1921 }
1922
Dan Stoza412903f2017-04-27 13:42:17 -07001923 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001924 }
1925}
1926
chaviw4b129c22018-04-09 16:19:43 -07001927LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1928 const std::vector<Layer*>& layersInTree) {
1929 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1930 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001931 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1932 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001933 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001934
chaviwfd462612018-05-31 16:11:27 -07001935 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001936 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1937 sp<Layer> strongRelative = weakRelative.promote();
1938 // Only add relative layers that are also descendents of the top most parent of the tree.
1939 // If a relative layer is not a descendent, then it should be ignored.
1940 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1941 traverse.add(strongRelative);
1942 }
1943 }
1944
1945 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001946 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001947 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1948 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1949 // the child here since it won't be added later as a relative.
1950 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1951 childState.zOrderRelativeOf.promote().get())) {
1952 continue;
1953 }
1954 traverse.add(child);
1955 }
1956
1957 return traverse;
1958}
1959
1960void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1961 LayerVector::StateSet stateSet,
1962 const LayerVector::Visitor& visitor) {
1963 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001964
1965 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001966 for (; i < list.size(); i++) {
1967 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001968 if (relative->getZ() >= 0) {
1969 break;
1970 }
chaviw4b129c22018-04-09 16:19:43 -07001971 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001972 }
chaviw4b129c22018-04-09 16:19:43 -07001973
chaviwa76b2712017-09-20 12:02:26 -07001974 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001975 for (; i < list.size(); i++) {
1976 const auto& relative = list[i];
1977 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001978 }
1979}
1980
chaviw4b129c22018-04-09 16:19:43 -07001981std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1982 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1983 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1984
1985 std::vector<Layer*> layersInTree = {this};
1986 for (size_t i = 0; i < children.size(); i++) {
1987 const auto& child = children[i];
1988 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1989 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1990 }
1991
1992 return layersInTree;
1993}
1994
1995void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1996 const LayerVector::Visitor& visitor) {
1997 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1998 std::sort(layersInTree.begin(), layersInTree.end());
1999 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2000}
2001
Peiyong Linefefaac2018-08-17 12:27:51 -07002002ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002003 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002004}
2005
chaviw13fdc492017-06-27 12:40:18 -07002006half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002007 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002008
chaviw13fdc492017-06-27 12:40:18 -07002009 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2010 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002011}
Robert Carr6452f122017-03-21 10:41:29 -07002012
chaviw13fdc492017-06-27 12:40:18 -07002013half4 Layer::getColor() const {
2014 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002015 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002016}
Robert Carr6452f122017-03-21 10:41:29 -07002017
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002018Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2019 const auto& p = mDrawingParent.promote();
2020 if (p != nullptr) {
2021 RoundedCornerState parentState = p->getRoundedCornerState();
2022 if (parentState.radius > 0) {
2023 ui::Transform t = getActiveTransform(getDrawingState());
2024 t = t.inverse();
2025 parentState.cropRect = t.transform(parentState.cropRect);
2026 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2027 // but a transform matrix can define horizontal and vertical scales.
2028 // Let's take the average between both of them and pass into the shader, practically we
2029 // never do this type of transformation on windows anyway.
2030 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2031 return parentState;
2032 }
2033 }
2034 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002035 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002036}
2037
Robert Carr1f0a16a2016-10-24 16:27:39 -07002038void Layer::commitChildList() {
2039 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2040 const auto& child = mCurrentChildren[i];
2041 child->commitChildList();
2042 }
2043 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002044 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002045}
2046
Robert Carr720e5062018-07-30 17:45:14 -07002047void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002048 mCurrentState.inputInfo = info;
2049 mCurrentState.modified = true;
2050 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002051 setTransactionFlags(eTransactionNeeded);
2052}
2053
chaviw1d044282017-09-27 12:19:28 -07002054void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2055 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2056 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002057 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002058
Peiyong Linefefaac2018-08-17 12:27:51 -07002059 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002060 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002061
2062 layerInfo->set_id(sequence);
2063 layerInfo->set_name(getName().c_str());
2064 layerInfo->set_type(String8(getTypeId()));
2065
2066 for (const auto& child : children) {
2067 layerInfo->add_children(child->sequence);
2068 }
2069
2070 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2071 sp<Layer> strongRelative = weakRelative.promote();
2072 if (strongRelative != nullptr) {
2073 layerInfo->add_relatives(strongRelative->sequence);
2074 }
2075 }
2076
Marissa Wallf58c14b2018-07-24 10:50:43 -07002077 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08002078 [&]() { return layerInfo->mutable_transparent_region(); });
2079 LayerProtoHelper::writeToProto(visibleRegion,
2080 [&]() { return layerInfo->mutable_visible_region(); });
2081 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2082 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07002083
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002084 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002085 layerInfo->set_z(state.z);
2086
Nataniel Borges797b0e42019-02-15 14:11:58 -08002087 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2088 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07002089
Nataniel Borges797b0e42019-02-15 14:11:58 -08002090 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2091 return layerInfo->mutable_requested_position();
2092 });
chaviw1d044282017-09-27 12:19:28 -07002093
Nataniel Borges797b0e42019-02-15 14:11:58 -08002094 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2095 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07002096
Nataniel Borges797b0e42019-02-15 14:11:58 -08002097 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002098 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002099
2100 layerInfo->set_is_opaque(isOpaque(state));
2101 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002102
2103 // XXX (b/79210409) mCurrentDataSpace is not protected
2104 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2105
chaviw1d044282017-09-27 12:19:28 -07002106 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08002107 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2108 LayerProtoHelper::writeToProto(state.color,
2109 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07002110 layerInfo->set_flags(state.flags);
2111
2112 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2113 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2114
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002115 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002116 if (parent != nullptr) {
2117 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002118 } else {
2119 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002120 }
2121
2122 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2123 if (zOrderRelativeOf != nullptr) {
2124 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002125 } else {
2126 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002127 }
2128
Lloyd Pique0b785d82018-12-04 17:25:27 -08002129 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002130 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08002131 LayerProtoHelper::writeToProto(buffer,
2132 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07002133 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002134 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002135 }
2136
2137 layerInfo->set_queued_frames(getQueuedFrameCount());
2138 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002139 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002140 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002141
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002142 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002143 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002144 if (barrierLayer != nullptr) {
2145 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2146 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002147 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002148 }
2149 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002150
2151 auto protoMap = layerInfo->mutable_metadata();
2152 for (const auto& entry : state.metadata.mMap) {
2153 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2154 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002155 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08002156 LayerProtoHelper::writeToProto(mSourceBounds,
2157 [&]() { return layerInfo->mutable_source_bounds(); });
2158 LayerProtoHelper::writeToProto(mScreenBounds,
2159 [&]() { return layerInfo->mutable_screen_bounds(); });
2160 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07002161}
2162
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002163void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2164 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2165 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002166 return;
2167 }
2168
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002169 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2170
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002171 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002172
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002173 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002174 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002175
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002176 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002177 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002178
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002179 const int32_t transform =
2180 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002181 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002182
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002183 const int32_t compositionType =
2184 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2185 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002186 layerInfo->set_hwc_composition_type(compositionType);
2187
2188 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2189 static_cast<BufferLayer*>(this)->isProtected()) {
2190 layerInfo->set_is_protected(true);
2191 } else {
2192 layerInfo->set_is_protected(false);
2193 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002194}
2195
Robert Carr2e102c92018-10-23 12:11:15 -07002196bool Layer::isRemovedFromCurrentState() const {
2197 return mRemovedFromCurrentState;
2198}
2199
Arthur Hungd20b2702019-01-14 18:16:16 +08002200InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002201 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002202
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002203 if (info.displayId == ADISPLAY_ID_NONE) {
2204 info.displayId = mDrawingState.layerStack;
2205 }
2206
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002207 ui::Transform t = getTransform();
2208 const float xScale = t.sx();
2209 const float yScale = t.sy();
2210 if (xScale != 1.0f || yScale != 1.0f) {
2211 info.windowXScale *= 1.0f / xScale;
2212 info.windowYScale *= 1.0f / yScale;
2213 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002214 }
Robert Carre07e1032018-11-26 12:55:53 -08002215
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002216 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002217 // If this is a portal window, set the touchableRegion to the layerBounds.
2218 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2219 ? getBufferSize(getDrawingState())
2220 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002221 if (!layerBounds.isValid()) {
2222 layerBounds = getCroppedBufferSize(getDrawingState());
2223 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002224 layerBounds = t.transform(layerBounds);
2225 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2226
Arthur Hungd20b2702019-01-14 18:16:16 +08002227 // Input coordinate should match the layer bounds.
2228 info.frameLeft = layerBounds.left;
2229 info.frameTop = layerBounds.top;
2230 info.frameRight = layerBounds.right;
2231 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002232
2233 // Position the touchable region relative to frame screen location and restrict it to frame
2234 // bounds.
2235 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002236 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002237 return info;
2238}
2239
2240bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002241 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002242}
2243
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002244std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2245 return nullptr;
2246}
2247
chaviw3e727cd2019-01-31 13:41:05 -08002248bool Layer::canReceiveInput() const {
2249 return isVisible();
2250}
2251
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002252compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2253 const sp<const DisplayDevice>& display) const {
2254 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2255}
2256
Mathias Agopian13127d82013-03-05 17:47:11 -08002257// ---------------------------------------------------------------------------
2258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002259}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002260
2261#if defined(__gl_h_)
2262#error "don't include gl/gl.h in this file"
2263#endif
2264
2265#if defined(__gl2_h_)
2266#error "don't include gl2/gl2.h in this file"
2267#endif