blob: a2bec9306bdb8134b56573c1945c42a189ec7fd8 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080033#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <gui/BufferItem.h>
39#include <gui/LayerDebugInfo.h>
40#include <gui/Surface.h>
41#include <renderengine/RenderEngine.h>
42#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Yiwei Zhang60d1a192018-03-07 14:52:28 -080051#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080052#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020053#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080055#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080057#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070058#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070059#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080061#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070062
David Sodman41fdfc92017-11-06 16:09:56 -080063#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065namespace android {
66
Yiwei Zhang5434a782018-12-05 18:06:32 -080067using base::StringAppendF;
68
Lloyd Piquef1c675b2018-09-12 20:45:39 -070069std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080070
Lloyd Pique42ab75e2018-09-12 20:46:03 -070071Layer::Layer(const LayerCreationArgs& args)
Lloyd Piquefa8fd6b2019-01-29 18:42:24 -080072 : mFlinger(args.flinger), mName(args.name), mClientRef(args.client) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070073 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070074
75 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070076 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
77 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
78 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079
Dan Stozaf7ba41a2017-05-10 15:11:11 -070080 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081
Lloyd Pique0449b0f2018-12-20 16:23:45 -080082 mCurrentState.active_legacy.w = args.w;
83 mCurrentState.active_legacy.h = args.h;
84 mCurrentState.flags = layerFlags;
85 mCurrentState.active_legacy.transform.set(0, 0);
86 mCurrentState.crop_legacy.makeInvalid();
87 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
88 mCurrentState.z = 0;
89 mCurrentState.color.a = 1.0f;
90 mCurrentState.layerStack = 0;
91 mCurrentState.sequence = 0;
92 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080093 mCurrentState.active.w = UINT32_MAX;
94 mCurrentState.active.h = UINT32_MAX;
95 mCurrentState.active.transform.set(0, 0);
96 mCurrentState.transform = 0;
97 mCurrentState.transformToDisplayInverse = false;
98 mCurrentState.crop.makeInvalid();
99 mCurrentState.acquireFence = new Fence(-1);
100 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
101 mCurrentState.hdrMetadata.validTypes = 0;
102 mCurrentState.surfaceDamageRegion.clear();
103 mCurrentState.cornerRadius = 0.0f;
104 mCurrentState.api = -1;
105 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800106 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800107 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
109 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700111
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700113 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800114 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200115 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700116
117 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700118}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800121 sp<Client> c(mClientRef.promote());
122 if (c != 0) {
123 c->detachLayer(this);
124 }
125
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000126 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700127
Robert Carr2e102c92018-10-23 12:11:15 -0700128 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian13127d82013-03-05 17:47:11 -0800131// ---------------------------------------------------------------------------
132// callbacks
133// ---------------------------------------------------------------------------
134
David Sodmaneb085e02017-10-05 18:49:04 -0700135/*
136 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
137 * Layer. So, the implementation is done in BufferLayer. When called on a
138 * ColorLayer object, it's essentially a NOP.
139 */
David Sodmaneb085e02017-10-05 18:49:04 -0700140void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800141
Chia-I Wuc6657022017-08-15 11:18:17 -0700142void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700143 mRemovedFromCurrentState = true;
144
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
146 if (mCurrentState.zOrderRelativeOf != nullptr) {
147 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
148 if (strongRelative != nullptr) {
149 strongRelative->removeZOrderRelative(this);
150 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
chaviw606e5cf2019-03-01 10:12:10 -0800152 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700153 }
Rob Carr4bba3702018-10-08 21:53:30 +0000154
Robert Carr2e102c92018-10-23 12:11:15 -0700155 // Since we are no longer reachable from CurrentState SurfaceFlinger
156 // will no longer invoke doTransaction for us, and so we will
157 // never finish applying transactions. We signal the sync point
158 // now so that another layer will not become indefinitely
159 // blocked.
160 for (auto& point: mRemoteSyncPoints) {
161 point->setTransactionApplied();
162 }
163 mRemoteSyncPoints.clear();
164
165 {
166 Mutex::Autolock syncLock(mLocalSyncPointMutex);
167 for (auto& point : mLocalSyncPoints) {
168 point->setFrameAvailable();
169 }
170 mLocalSyncPoints.clear();
171 }
172
Chia-I Wuc6657022017-08-15 11:18:17 -0700173 for (const auto& child : mCurrentChildren) {
174 child->onRemovedFromCurrentState();
175 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800176
177 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700178}
Chia-I Wu38512252017-05-17 14:36:16 -0700179
chaviw61626f22018-11-15 16:26:27 -0800180void Layer::addToCurrentState() {
181 mRemovedFromCurrentState = false;
182
183 for (const auto& child : mCurrentChildren) {
184 child->addToCurrentState();
185 }
186}
187
Mathias Agopian13127d82013-03-05 17:47:11 -0800188// ---------------------------------------------------------------------------
189// set-up
190// ---------------------------------------------------------------------------
191
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700192const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800193 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194}
195
chaviw13fdc492017-06-27 12:40:18 -0700196bool Layer::getPremultipledAlpha() const {
197 return mPremultipliedAlpha;
198}
199
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700200sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700202 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800203}
204
205// ---------------------------------------------------------------------------
206// h/w composer set-up
207// ---------------------------------------------------------------------------
208
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800209bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
210 auto outputLayer = findOutputLayerForDisplay(displayDevice);
211 LOG_FATAL_IF(!outputLayer);
212 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
213}
214
215HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
216 auto outputLayer = findOutputLayerForDisplay(displayDevice);
217 if (!outputLayer || !outputLayer->getState().hwc) {
218 return nullptr;
219 }
220 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700221}
Steven Thomasb02664d2017-07-26 18:48:28 -0700222
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800223Rect Layer::getContentCrop() const {
224 // this is the crop rectangle that applies to the buffer
225 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700226 Rect crop;
227 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800228 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700229 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800230 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800231 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800232 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700233 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800234 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700235 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700237 return crop;
238}
239
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700240static Rect reduce(const Rect& win, const Region& exclude) {
241 if (CC_LIKELY(exclude.isEmpty())) {
242 return win;
243 }
244 if (exclude.isRect()) {
245 return win.reduce(exclude.getBounds());
246 }
247 return Region(win).subtract(exclude).getBounds();
248}
249
Dan Stoza80d61162017-12-20 15:57:52 -0800250static FloatRect reduce(const FloatRect& win, const Region& exclude) {
251 if (CC_LIKELY(exclude.isEmpty())) {
252 return win;
253 }
254 // Convert through Rect (by rounding) for lack of FloatRegion
255 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
256}
257
Vishnu Nair4351ad52019-02-11 14:13:02 -0800258Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800259 if (!reduceTransparentRegion) {
260 return Rect{mScreenBounds};
261 }
262
263 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800264 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800265 // Transform to screen space.
266 bounds = t.transform(bounds);
267 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700268}
269
Vishnu Nair4351ad52019-02-11 14:13:02 -0800270FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000271 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800272 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700273}
274
Vishnu Nairf0c28512019-02-08 12:40:28 -0800275FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
276 // Subtract the transparent region and snap to the bounds.
277 return reduce(mBounds, activeTransparentRegion);
278}
279
Vishnu Nair4351ad52019-02-11 14:13:02 -0800280ui::Transform Layer::getTransformWithScale() const {
281 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
282 // it isFixedSize) then there may be additional scaling not accounted
283 // for in the transform. We need to mirror this scaling to child surfaces
284 // or we will break the contract where WM can treat child surfaces as
285 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800286 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800287 return mEffectiveTransform;
288 }
289
290 int bufferWidth;
291 int bufferHeight;
292 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800293 bufferWidth = mActiveBuffer->getWidth();
294 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800295 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800296 bufferHeight = mActiveBuffer->getWidth();
297 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800298 }
299 float sx = getActiveWidth(getDrawingState()) / static_cast<float>(bufferWidth);
300 float sy = getActiveHeight(getDrawingState()) / static_cast<float>(bufferHeight);
301 ui::Transform extraParentScaling;
302 extraParentScaling.set(sx, 0, 0, sy);
303 return mEffectiveTransform * extraParentScaling;
304}
305
306void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
307 const State& s(getDrawingState());
308
309 // Calculate effective layer transform
310 mEffectiveTransform = parentTransform * getActiveTransform(s);
311
312 // Transform parent bounds to layer space
313 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
314
315 // Calculate display frame
316 mSourceBounds = computeSourceBounds(parentBounds);
317
318 // Calculate bounds by croping diplay frame with layer crop and parent bounds
319 FloatRect bounds = mSourceBounds;
320 const Rect layerCrop = getCrop(s);
321 if (!layerCrop.isEmpty()) {
322 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
323 }
324 bounds = bounds.intersect(parentBounds);
325
326 mBounds = bounds;
327 mScreenBounds = mEffectiveTransform.transform(mBounds);
328 for (const sp<Layer>& child : mDrawingChildren) {
329 child->computeBounds(mBounds, getTransformWithScale());
330 }
331}
332
Robert Carrb5d3d262016-03-25 15:08:13 -0700333
Robert Carr1f0a16a2016-10-24 16:27:39 -0700334
Vishnu Nair60356342018-11-13 13:00:45 -0800335Rect Layer::getCroppedBufferSize(const State& s) const {
336 Rect size = getBufferSize(s);
337 Rect crop = getCrop(s);
338 if (!crop.isEmpty() && size.isValid()) {
339 size.intersect(crop, &size);
340 } else if (!crop.isEmpty()) {
341 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700342 }
Vishnu Nair60356342018-11-13 13:00:45 -0800343 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800344}
345
Alec Mouri1f3bd182019-01-24 15:20:18 +0000346Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700347 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800348 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700349 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800350
351 // apply the projection's clipping to the window crop in
352 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700353 // if there are no window scaling involved, this operation will map to full
354 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700355
Vishnu Nair4351ad52019-02-11 14:13:02 -0800356 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000357 ui::Transform t = getTransform();
358 // Transform to screen space.
359 activeCropFloat = t.transform(activeCropFloat);
360 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
361 // Back to layer space to work with the content crop.
362 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800363 // This needs to be here as transform.transform(Rect) computes the
364 // transformed rect and then takes the bounding box of the result before
365 // returning. This means
366 // transform.inverse().transform(transform.transform(Rect)) != Rect
367 // in which case we need to make sure the final rect is clipped to the
368 // display bounds.
369 Rect activeCrop{activeCropFloat};
370 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000371 activeCrop.clear();
372 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700373 return activeCrop;
374}
375
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700376void Layer::setupRoundedCornersCropCoordinates(Rect win,
377 const FloatRect& roundedCornersCrop) const {
378 // Translate win by the rounded corners rect coordinates, to have all values in
379 // layer coordinate space.
380 win.left -= roundedCornersCrop.left;
381 win.right -= roundedCornersCrop.left;
382 win.top -= roundedCornersCrop.top;
383 win.bottom -= roundedCornersCrop.top;
384
Lloyd Pique0b785d82018-12-04 17:25:27 -0800385 renderengine::Mesh::VertexArray<vec2> cropCoords(
386 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700387 cropCoords[0] = vec2(win.left, win.top);
388 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
389 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
390 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700391}
392
Alec Mouri1f3bd182019-01-24 15:20:18 +0000393FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700394 // the content crop is the area of the content that gets scaled to the
395 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800396 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700397
398 // In addition there is a WM-specified crop we pull from our drawing state.
399 const State& s(getDrawingState());
400
Alec Mouri1f3bd182019-01-24 15:20:18 +0000401 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800402 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700403
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000404 // Transform the window crop to match the buffer coordinate system,
405 // which means using the inverse of the current transform set on the
406 // SurfaceFlingerConsumer.
407 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800408 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000409 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700410 * the code below applies the primary display's inverse transform to the
411 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000412 */
David Sodman41fdfc92017-11-06 16:09:56 -0800413 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 // calculate the inverse transform
415 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800416 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800417 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000418 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700419 invTransform = (ui::Transform(invTransformOrient) *
420 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800421 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800423 int winWidth = bufferSize.getWidth();
424 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
426 // If the activeCrop has been rotate the ends are rotated but not
427 // the space itself so when transforming ends back we can't rely on
428 // a modification of the axes of rotation. To account for this we
429 // need to reorient the inverse rotation in terms of the current
430 // axes of rotation.
431 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
432 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
433 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800434 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800436 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800438 const Rect winCrop =
439 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440
441 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800442 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 float yScale = crop.getHeight() / float(winHeight);
444
David Sodman41fdfc92017-11-06 16:09:56 -0800445 float insetL = winCrop.left * xScale;
446 float insetT = winCrop.top * yScale;
447 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 float insetB = (winHeight - winCrop.bottom) * yScale;
449
David Sodman41fdfc92017-11-06 16:09:56 -0800450 crop.left += insetL;
451 crop.top += insetT;
452 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 crop.bottom -= insetB;
454
Mathias Agopian13127d82013-03-05 17:47:11 -0800455 return crop;
456}
457
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700458void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800459 const auto outputLayer = findOutputLayerForDisplay(display);
460 LOG_FATAL_IF(!outputLayer);
461 LOG_FATAL_IF(!outputLayer->getState().hwc);
462 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700463
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800464 if (!hasHwcLayer(display)) {
465 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
466 display->getDebugName().c_str());
467 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800468 }
469
Lloyd Pique0b785d82018-12-04 17:25:27 -0800470 LOG_FATAL_IF(!getCompositionLayer());
471 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800472 auto& compositionState = outputLayer->editState();
473
474 // enable this layer
475 compositionState.forceClientComposition = false;
476
477 if (isSecure() && !display->isSecure()) {
478 compositionState.forceClientComposition = true;
479 }
David Sodman15094112018-10-11 09:39:37 -0700480
Mathias Agopian13127d82013-03-05 17:47:11 -0800481 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700482 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800483 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500484 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800485 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800486 blendMode =
487 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800488 }
David Sodman15094112018-10-11 09:39:37 -0700489 auto error = hwcLayer->setBlendMode(blendMode);
490 ALOGE_IF(error != HWC2::Error::None,
491 "[%s] Failed to set blend mode %s:"
492 " %s (%d)",
493 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
494 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800495 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800496
497 // apply the layer's transform, followed by the display's global transform
498 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700499 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800500 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700501 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800502 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700503 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700504 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000505 activeCrop.clear();
506 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700507 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800508 // This needs to be here as transform.transform(Rect) computes the
509 // transformed rect and then takes the bounding box of the result before
510 // returning. This means
511 // transform.inverse().transform(transform.transform(Rect)) != Rect
512 // in which case we need to make sure the final rect is clipped to the
513 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800514 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000515 activeCrop.clear();
516 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700517 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800518 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
519 activeTransparentRegion.orSelf(
520 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800521 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
522 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800523 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700524 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700525
Vishnu Nair4351ad52019-02-11 14:13:02 -0800526 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800527 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800528 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700529 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000530 frame.clear();
531 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700532 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800533 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700534 error = hwcLayer->setDisplayFrame(transformedFrame);
535 if (error != HWC2::Error::None) {
536 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
537 transformedFrame.left, transformedFrame.top, transformedFrame.right,
538 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
539 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800540 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700541 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700543 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700544 error = hwcLayer->setSourceCrop(sourceCrop);
545 if (error != HWC2::Error::None) {
546 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
547 "%s (%d)",
548 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
549 to_string(error).c_str(), static_cast<int32_t>(error));
550 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800551 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700552 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800554 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700555 error = hwcLayer->setPlaneAlpha(alpha);
556 ALOGE_IF(error != HWC2::Error::None,
557 "[%s] Failed to set plane alpha %.3f: "
558 "%s (%d)",
559 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800560 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561
David Sodman15094112018-10-11 09:39:37 -0700562 error = hwcLayer->setZOrder(z);
563 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
564 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800565 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500566
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800567 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
568 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700569 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400570 if (parent.get()) {
571 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800572 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
573 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
574 if (parentType >= 0 || parentAppId >= 0) {
575 type = parentType;
576 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700577 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400578 }
579
David Sodman15094112018-10-11 09:39:37 -0700580 error = hwcLayer->setInfo(type, appId);
581 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
582 static_cast<int32_t>(error));
583
Lloyd Pique0b785d82018-12-04 17:25:27 -0800584 commonCompositionState.type = type;
585 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700586
Mathias Agopian29a367b2011-07-12 14:51:45 -0700587 /*
588 * Transformations are applied in this order:
589 * 1) buffer orientation/flip/mirror
590 * 2) state transformation (window manager)
591 * 3) layer orientation (screen orientation)
592 * (NOTE: the matrices are multiplied in reverse order)
593 */
594
Peiyong Linefefaac2018-08-17 12:27:51 -0700595 const ui::Transform bufferOrientation(mCurrentTransform);
596 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700597
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800598 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700599 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700600 * the code below applies the primary display's inverse transform to the
601 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700602 */
David Sodman41fdfc92017-11-06 16:09:56 -0800603 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700604 // calculate the inverse transform
605 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800606 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700607 }
Robert Carrcae605c2017-03-29 12:10:31 -0700608
609 /*
610 * Here we cancel out the orientation component of the WM transform.
611 * The scaling and translate components are already included in our bounds
612 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000613 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700614 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700615 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700616 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700617
618 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800619 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700620 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800622 compositionState.forceClientComposition = true;
623 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624 } else {
625 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700626 auto error = hwcLayer->setTransform(transform);
627 ALOGE_IF(error != HWC2::Error::None,
628 "[%s] Failed to set transform %s: "
629 "%s (%d)",
630 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
631 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800632 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800633 }
634}
635
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800636void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
637 const auto outputLayer = findOutputLayerForDisplay(display);
638 LOG_FATAL_IF(!outputLayer);
639 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640}
Dan Stozaee44edd2015-03-23 15:50:23 -0700641
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800642bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
643 const auto outputLayer = findOutputLayerForDisplay(display);
644 LOG_FATAL_IF(!outputLayer);
645 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800646}
647
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700648void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800649 const auto outputLayer = findOutputLayerForDisplay(display);
650 LOG_FATAL_IF(!outputLayer);
651
652 if (!outputLayer->getState().hwc ||
653 (*outputLayer->getState().hwc).hwcCompositionType !=
654 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800655 return;
656 }
657
658 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800659 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660
661 // Apply the layer's transform, followed by the display's global transform
662 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800663 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700665 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800666 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700667 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700668 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669 auto position = displayTransform.transform(frame);
670
Dominik Laskowski7e045462018-05-30 13:02:02 -0700671 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800672 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800673 ALOGE_IF(error != HWC2::Error::None,
674 "[%s] Failed to set cursor position "
675 "to (%d, %d): %s (%d)",
676 mName.string(), position.left, position.top, to_string(error).c_str(),
677 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678}
Riley Andrews03414a12014-07-01 14:22:59 -0700679
Mathias Agopian13127d82013-03-05 17:47:11 -0800680// ---------------------------------------------------------------------------
681// drawing...
682// ---------------------------------------------------------------------------
683
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000684bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800685 Region& clearRegion, const bool supportProtectedContent,
686 renderengine::LayerSettings& layer) {
687 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800688}
689
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000690bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800691 Region& clearRegion, const bool supportProtectedContent,
692 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000693 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800694 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000695}
696
697bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
698 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800699 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000700 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800701 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000702 half alpha = getAlpha();
703 layer.geometry.boundaries = bounds;
704 if (useIdentityTransform) {
705 layer.geometry.positionTransform = mat4();
706 } else {
707 const ui::Transform transform = getTransform();
708 mat4 m;
709 m[0][0] = transform[0][0];
710 m[0][1] = transform[0][1];
711 m[0][3] = transform[0][2];
712 m[1][0] = transform[1][0];
713 m[1][1] = transform[1][1];
714 m[1][3] = transform[1][2];
715 m[3][0] = transform[2][0];
716 m[3][1] = transform[2][1];
717 m[3][3] = transform[2][2];
718 layer.geometry.positionTransform = m;
719 }
720
721 if (hasColorTransform()) {
722 layer.colorTransform = getColorTransform();
723 }
724
725 const auto roundedCornerState = getRoundedCornerState();
726 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
727 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
728
729 layer.alpha = alpha;
730 layer.sourceDataspace = mCurrentDataSpace;
731 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800732}
733
David Sodman41fdfc92017-11-06 16:09:56 -0800734void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
735 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800736 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800737 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700738 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800739 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800740}
741
David Sodmanc1498e62018-09-12 14:36:26 -0700742void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
743 clearWithOpenGL(renderArea, 0, 0, 0, 0);
744}
745
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800746void Layer::setCompositionType(const sp<const DisplayDevice>& display,
747 Hwc2::IComposerClient::Composition type) {
748 const auto outputLayer = findOutputLayerForDisplay(display);
749 LOG_FATAL_IF(!outputLayer);
750 LOG_FATAL_IF(!outputLayer->getState().hwc);
751 auto& compositionState = outputLayer->editState();
752
753 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
754 toString(type).c_str(), 1);
755 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700756 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800757 (*compositionState.hwc).hwcCompositionType = type;
758
759 auto error = (*compositionState.hwc)
760 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
761 ALOGE_IF(error != HWC2::Error::None,
762 "[%s] Failed to set "
763 "composition type %s: %s (%d)",
764 mName.string(), toString(type).c_str(), to_string(error).c_str(),
765 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766 }
767}
768
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800769Hwc2::IComposerClient::Composition Layer::getCompositionType(
770 const sp<const DisplayDevice>& display) const {
771 const auto outputLayer = findOutputLayerForDisplay(display);
772 LOG_FATAL_IF(!outputLayer);
773 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
774 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775}
776
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800777bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
778 const auto outputLayer = findOutputLayerForDisplay(display);
779 LOG_FATAL_IF(!outputLayer);
780 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782
Dan Stozacac35382016-01-27 12:21:06 -0800783bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
784 if (point->getFrameNumber() <= mCurrentFrameNumber) {
785 // Don't bother with a SyncPoint, since we've already latched the
786 // relevant frame
787 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700788 }
Robert Carr2e102c92018-10-23 12:11:15 -0700789 if (isRemovedFromCurrentState()) {
790 return false;
791 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700792
Dan Stozacac35382016-01-27 12:21:06 -0800793 Mutex::Autolock lock(mLocalSyncPointMutex);
794 mLocalSyncPoints.push_back(point);
795 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700796}
797
Mathias Agopian13127d82013-03-05 17:47:11 -0800798// ----------------------------------------------------------------------------
799// local state
800// ----------------------------------------------------------------------------
801
Peiyong Lin833074a2018-08-28 11:53:54 -0700802void Layer::computeGeometry(const RenderArea& renderArea,
803 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700804 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700805 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800806 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700807
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000808 vec2 lt = vec2(win.left, win.top);
809 vec2 lb = vec2(win.left, win.bottom);
810 vec2 rb = vec2(win.right, win.bottom);
811 vec2 rt = vec2(win.right, win.top);
812
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800813 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000814 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700815 lt = layerTransform.transform(lt);
816 lb = layerTransform.transform(lb);
817 rb = layerTransform.transform(rb);
818 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000819 }
820
Peiyong Lin833074a2018-08-28 11:53:54 -0700821 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700822 position[0] = renderAreaTransform.transform(lt);
823 position[1] = renderAreaTransform.transform(lb);
824 position[2] = renderAreaTransform.transform(rb);
825 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800826}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800827
David Sodman41fdfc92017-11-06 16:09:56 -0800828bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800829 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700830 return (s.flags & layer_state_t::eLayerSecure);
831}
832
Mathias Agopian13127d82013-03-05 17:47:11 -0800833void Layer::setVisibleRegion(const Region& visibleRegion) {
834 // always called from main thread
835 this->visibleRegion = visibleRegion;
836}
837
838void Layer::setCoveredRegion(const Region& coveredRegion) {
839 // always called from main thread
840 this->coveredRegion = coveredRegion;
841}
842
David Sodman41fdfc92017-11-06 16:09:56 -0800843void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800844 // always called from main thread
845 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
846}
847
Robert Carre5f4f692018-01-12 13:12:28 -0800848void Layer::clearVisibilityRegions() {
849 visibleRegion.clear();
850 visibleNonTransparentRegion.clear();
851 coveredRegion.clear();
852}
853
Mathias Agopian13127d82013-03-05 17:47:11 -0800854// ----------------------------------------------------------------------------
855// transaction
856// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800857
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800858void Layer::pushPendingState() {
859 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700860 return;
861 }
862
Dan Stoza7dde5992015-05-22 09:51:44 -0700863 // If this transaction is waiting on the receipt of a frame, generate a sync
864 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700865 // We don't allow installing sync points after we are removed from the current state
866 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800867 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
868 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800869 if (barrierLayer == nullptr) {
870 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700871 // If we can't promote the layer we are intended to wait on,
872 // then it is expired or otherwise invalid. Allow this transaction
873 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800874 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800875 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800876 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800877 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800878 mRemoteSyncPoints.push_back(std::move(syncPoint));
879 } else {
880 // We already missed the frame we're supposed to synchronize
881 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800882 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800883 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700884 }
885
Dan Stoza7dde5992015-05-22 09:51:44 -0700886 // Wake us up to check if the frame has been received
887 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700888 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700889 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800890 mPendingStates.push_back(mCurrentState);
891 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700892}
893
Pablo Ceballos05289c22016-04-14 15:49:55 -0700894void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800895 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700896
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 mPendingStates.removeAt(0);
898 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700899}
900
Pablo Ceballos05289c22016-04-14 15:49:55 -0700901bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700902 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800903 while (!mPendingStates.empty()) {
904 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700905 if (mRemoteSyncPoints.empty()) {
906 // If we don't have a sync point for this, apply it anyway. It
907 // will be visually wrong, but it should keep us from getting
908 // into too much trouble.
909 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700910 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700911 stateUpdateAvailable = true;
912 continue;
913 }
914
Marissa Wallf58c14b2018-07-24 10:50:43 -0700915 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800916 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800917 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800918
919 // Signal our end of the sync point and then dispose of it
920 mRemoteSyncPoints.front()->setTransactionApplied();
921 mRemoteSyncPoints.pop_front();
922 continue;
923 }
924
Dan Stoza7dde5992015-05-22 09:51:44 -0700925 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
926 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700927 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700928 stateUpdateAvailable = true;
929
930 // Signal our end of the sync point and then dispose of it
931 mRemoteSyncPoints.front()->setTransactionApplied();
932 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800933 } else {
934 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700937 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700938 stateUpdateAvailable = true;
939 }
940 }
941
942 // If we still have pending updates, wake SurfaceFlinger back up and point
943 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800944 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700945 setTransactionFlags(eTransactionNeeded);
946 mFlinger->setTransactionFlags(eTraversalNeeded);
947 }
948
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800949 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700950 return stateUpdateAvailable;
951}
952
Marissa Wall61c58622018-07-18 10:12:20 -0700953uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000954 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955
Marissa Wall61c58622018-07-18 10:12:20 -0700956 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
957 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700958
David Sodmaneb085e02017-10-05 18:49:04 -0700959 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700960 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000961 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800962 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
963 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
964 " requested={ wh={%4u,%4u} }}\n"
965 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
966 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700967 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700968 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
969 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
970 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
971 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
972 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700973 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
974 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
975 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700977
Robert Carre392b552017-09-19 12:16:05 -0700978 // Don't let Layer::doTransaction update the drawing state
979 // if we have a pending resize, unless we are in fixed-size mode.
980 // the drawing state will be updated only once we receive a buffer
981 // with the correct size.
982 //
983 // In particular, we want to make sure the clip (which is part
984 // of the geometry state) is latched together with the size but is
985 // latched immediately when no resizing is involved.
986 //
987 // If a sideband stream is attached, however, we want to skip this
988 // optimization so that transactions aren't missed when a buffer
989 // never arrives
990 //
991 // In the case that we don't have a buffer we ignore other factors
992 // and avoid entering the resizePending state. At a high level the
993 // resizePending state is to avoid applying the state of the new buffer
994 // to the old buffer. However in the state where we don't have an old buffer
995 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700996 const bool resizePending =
997 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
998 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800999 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001000 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -08001001 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001002 flags |= eDontUpdateGeometryState;
1003 }
1004 }
1005
Robert Carr7bf247e2017-05-18 14:02:49 -07001006 // Here we apply various requested geometry states, depending on our
1007 // latching configuration. See Layer.h for a detailed discussion of
1008 // how geometry latching is controlled.
1009 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001010 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001011
1012 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1013 // mode, which causes attributes which normally latch regardless of scaling mode,
1014 // to be delayed. We copy the requested state to the active state making sure
1015 // to respect these rules (again see Layer.h for a detailed discussion).
1016 //
1017 // There is an awkward asymmetry in the handling of the crop states in the position
1018 // states, as can be seen below. Largely this arises from position and transform
1019 // being stored in the same data structure while having different latching rules.
1020 // b/38182305
1021 //
Marissa Wall61c58622018-07-18 10:12:20 -07001022 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001023 // applyPendingStates in the presence of deferred transactions.
1024 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001025 float tx = stateToCommit->active_legacy.transform.tx();
1026 float ty = stateToCommit->active_legacy.transform.ty();
1027 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1028 stateToCommit->active_legacy.transform.set(tx, ty);
1029 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001030 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001031 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001032 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001033 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001034 }
1035
Marissa Wall61c58622018-07-18 10:12:20 -07001036 return flags;
1037}
1038
1039uint32_t Layer::doTransaction(uint32_t flags) {
1040 ATRACE_CALL();
1041
chaviw5aedec92018-10-22 10:40:38 -07001042 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001043 return flags;
1044 }
1045
1046 if (mChildrenChanged) {
1047 flags |= eVisibleRegion;
1048 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001049 }
1050
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001051 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001052 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001053 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001054 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001055 }
1056
1057 flags = doTransactionResize(flags, &c);
1058
Alec Mourib416efd2018-09-06 21:01:59 +00001059 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001060
1061 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001062 // invalidate and recompute the visible regions if needed
1063 flags |= Layer::eVisibleRegion;
1064 }
1065
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001066 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001067 // invalidate and recompute the visible regions if needed
1068 flags |= eVisibleRegion;
1069 this->contentDirty = true;
1070
1071 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001072 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001073 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001074 }
1075
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001076 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001077 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001078 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001079 }
1080
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001082 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001084 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085}
1086
Pablo Ceballos05289c22016-04-14 15:49:55 -07001087void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001088 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001089}
1090
Mathias Agopian13127d82013-03-05 17:47:11 -08001091uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001093}
1094
1095uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001096 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001097}
1098
Robert Carr82364e32016-05-15 11:27:47 -07001099bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001100 if (mCurrentState.requested_legacy.transform.tx() == x &&
1101 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001102 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001103 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001104
1105 // We update the requested and active position simultaneously because
1106 // we want to apply the position portion of the transform matrix immediately,
1107 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001109 if (immediate && !mFreezeGeometryUpdates) {
1110 // Here we directly update the active state
1111 // unlike other setters, because we store it within
1112 // the transform, but use different latching rules.
1113 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001114 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001115 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001116 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001117
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001118 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001119 setTransactionFlags(eTransactionNeeded);
1120 return true;
1121}
Robert Carr82364e32016-05-15 11:27:47 -07001122
Robert Carr1f0a16a2016-10-24 16:27:39 -07001123bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1124 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1125 if (idx < 0) {
1126 return false;
1127 }
1128 if (childLayer->setLayer(z)) {
1129 mCurrentChildren.removeAt(idx);
1130 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001131 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001132 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001133 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001134}
1135
Robert Carr503c7042017-09-27 15:06:08 -07001136bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1137 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1138 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1139 if (idx < 0) {
1140 return false;
1141 }
1142 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1143 mCurrentChildren.removeAt(idx);
1144 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001145 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001146 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001147 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001148}
1149
Robert Carrae060832016-11-28 10:51:00 -08001150bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001151 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1152 mCurrentState.sequence++;
1153 mCurrentState.z = z;
1154 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001155
1156 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001157 if (mCurrentState.zOrderRelativeOf != nullptr) {
1158 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001159 if (strongRelative != nullptr) {
1160 strongRelative->removeZOrderRelative(this);
1161 }
chaviw606e5cf2019-03-01 10:12:10 -08001162 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001163 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001164 setTransactionFlags(eTransactionNeeded);
1165 return true;
1166}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001167
Robert Carrdb66e622017-04-10 16:55:57 -07001168void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001169 mCurrentState.zOrderRelatives.remove(relative);
1170 mCurrentState.sequence++;
1171 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001172 setTransactionFlags(eTransactionNeeded);
1173}
1174
1175void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001176 mCurrentState.zOrderRelatives.add(relative);
1177 mCurrentState.modified = true;
1178 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001179 setTransactionFlags(eTransactionNeeded);
1180}
1181
chaviw606e5cf2019-03-01 10:12:10 -08001182void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1183 mCurrentState.zOrderRelativeOf = relativeOf;
1184 mCurrentState.sequence++;
1185 mCurrentState.modified = true;
1186 setTransactionFlags(eTransactionNeeded);
1187}
1188
Robert Carr503d2bd2017-12-04 15:49:47 -08001189bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001190 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1191 if (handle == nullptr) {
1192 return false;
1193 }
1194 sp<Layer> relative = handle->owner.promote();
1195 if (relative == nullptr) {
1196 return false;
1197 }
1198
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001199 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1200 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001201 return false;
1202 }
1203
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001204 mCurrentState.sequence++;
1205 mCurrentState.modified = true;
1206 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001207
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001208 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001209 if (oldZOrderRelativeOf != nullptr) {
1210 oldZOrderRelativeOf->removeZOrderRelative(this);
1211 }
chaviw606e5cf2019-03-01 10:12:10 -08001212 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001213 relative->addZOrderRelative(this);
1214
1215 setTransactionFlags(eTransactionNeeded);
1216
1217 return true;
1218}
1219
Mathias Agopian13127d82013-03-05 17:47:11 -08001220bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001221 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001222 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001223 mCurrentState.requested_legacy.w = w;
1224 mCurrentState.requested_legacy.h = h;
1225 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001227
1228 // record the new size, from this point on, when the client request
1229 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001230 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 return true;
1232}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001233bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001234 if (mCurrentState.color.a == alpha) return false;
1235 mCurrentState.sequence++;
1236 mCurrentState.color.a = alpha;
1237 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001238 setTransactionFlags(eTransactionNeeded);
1239 return true;
1240}
chaviw13fdc492017-06-27 12:40:18 -07001241
Valerie Haudd0b7572019-01-29 14:59:27 -08001242bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1243 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001244 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001245 }
1246 mCurrentState.sequence++;
1247 mCurrentState.modified = true;
1248 setTransactionFlags(eTransactionNeeded);
1249
1250 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001251 // create background color layer if one does not yet exist
1252 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1253 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001254 mCurrentState.bgColorLayer = new ColorLayer(
1255 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001256
Valerie Haudd0b7572019-01-29 14:59:27 -08001257 // add to child list
1258 addChild(mCurrentState.bgColorLayer);
1259 mFlinger->mLayersAdded = true;
1260 // set up SF to handle added color layer
1261 if (isRemovedFromCurrentState()) {
1262 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1263 }
1264 mFlinger->setTransactionFlags(eTransactionNeeded);
1265 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1266 mCurrentState.bgColorLayer->reparent(nullptr);
1267 mCurrentState.bgColorLayer = nullptr;
1268 return true;
1269 }
1270
1271 mCurrentState.bgColorLayer->setColor(color);
1272 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1273 mCurrentState.bgColorLayer->setAlpha(alpha);
1274 mCurrentState.bgColorLayer->setDataspace(dataspace);
1275
chaviw13fdc492017-06-27 12:40:18 -07001276 return true;
1277}
1278
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001279bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001280 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001281
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001282 mCurrentState.sequence++;
1283 mCurrentState.cornerRadius = cornerRadius;
1284 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001285 setTransactionFlags(eTransactionNeeded);
1286 return true;
1287}
1288
Robert Carrd4ae7f32018-06-07 16:10:57 -07001289bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1290 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001291 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001292 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1293
1294 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1295 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1296 return false;
1297 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001298 mCurrentState.sequence++;
1299 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1300 matrix.dsdy);
1301 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001302 setTransactionFlags(eTransactionNeeded);
1303 return true;
1304}
Marissa Wall61c58622018-07-18 10:12:20 -07001305
Mathias Agopian13127d82013-03-05 17:47:11 -08001306bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001307 mCurrentState.requestedTransparentRegion_legacy = transparent;
1308 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001309 setTransactionFlags(eTransactionNeeded);
1310 return true;
1311}
1312bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001313 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1314 if (mCurrentState.flags == newFlags) return false;
1315 mCurrentState.sequence++;
1316 mCurrentState.flags = newFlags;
1317 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001318 setTransactionFlags(eTransactionNeeded);
1319 return true;
1320}
Robert Carr99e27f02016-06-16 15:18:02 -07001321
Marissa Wallf58c14b2018-07-24 10:50:43 -07001322bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001323 if (mCurrentState.requestedCrop_legacy == crop) return false;
1324 mCurrentState.sequence++;
1325 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001326 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001327 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001328 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001329 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1330
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001331 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001332 setTransactionFlags(eTransactionNeeded);
1333 return true;
1334}
Robert Carr8d5227b2017-03-16 15:41:03 -07001335
Robert Carrc3574f72016-03-24 12:19:32 -07001336bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001337 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001338 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001339 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001340 return true;
1341}
1342
Evan Roskyef876c92019-01-25 17:46:06 -08001343bool Layer::setMetadata(const LayerMetadata& data) {
1344 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001345 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001346 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001347 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001348 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001349}
1350
Mathias Agopian13127d82013-03-05 17:47:11 -08001351bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001352 if (mCurrentState.layerStack == layerStack) return false;
1353 mCurrentState.sequence++;
1354 mCurrentState.layerStack = layerStack;
1355 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001356 setTransactionFlags(eTransactionNeeded);
1357 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001358}
1359
Peiyong Linc502cb72019-03-01 15:00:23 -08001360bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1361 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1362 return false;
1363 }
1364 mCurrentState.sequence++;
1365 mCurrentState.colorSpaceAgnostic = agnostic;
1366 mCurrentState.modified = true;
1367 setTransactionFlags(eTransactionNeeded);
1368 return true;
1369}
1370
Robert Carr1f0a16a2016-10-24 16:27:39 -07001371uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001372 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001373 if (p == nullptr) {
1374 return getDrawingState().layerStack;
1375 }
1376 return p->getLayerStack();
1377}
1378
Marissa Wallf58c14b2018-07-24 10:50:43 -07001379void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001380 mCurrentState.barrierLayer_legacy = barrierLayer;
1381 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001382 // We don't set eTransactionNeeded, because just receiving a deferral
1383 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001384 mCurrentState.modified = true;
1385 pushPendingState();
1386 mCurrentState.barrierLayer_legacy = nullptr;
1387 mCurrentState.frameNumber_legacy = 0;
1388 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001389}
1390
Marissa Wallf58c14b2018-07-24 10:50:43 -07001391void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001392 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001393 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001394}
1395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396// ----------------------------------------------------------------------------
1397// pageflip handling...
1398// ----------------------------------------------------------------------------
1399
Robert Carr1f0a16a2016-10-24 16:27:39 -07001400bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001401 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001402 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001403 if (parent != nullptr && parent->isHiddenByPolicy()) {
1404 return true;
1405 }
1406 return s.flags & layer_state_t::eLayerHidden;
1407}
1408
David Sodman41fdfc92017-11-06 16:09:56 -08001409uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001410 // TODO: should we do something special if mSecure is set?
1411 if (mProtectedByApp) {
1412 // need a hardware-protected path to external video sink
1413 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001414 }
Riley Andrews03414a12014-07-01 14:22:59 -07001415 if (mPotentialCursor) {
1416 usage |= GraphicBuffer::USAGE_CURSOR;
1417 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001418 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001419 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001420}
1421
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001422void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001423 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001424 // Disable setting transform hint if the debug flag is set or if the
1425 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1426 // in one orientation.
1427 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001428 // The transform hint is used to improve performance, but we can
1429 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001430 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001431 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001432 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001433 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001434 orientation = 0;
1435 }
1436 }
David Sodmaneb085e02017-10-05 18:49:04 -07001437 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001438}
1439
Mathias Agopian13127d82013-03-05 17:47:11 -08001440// ----------------------------------------------------------------------------
1441// debugging
1442// ----------------------------------------------------------------------------
1443
Marissa Wall61c58622018-07-18 10:12:20 -07001444// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001445LayerDebugInfo Layer::getLayerDebugInfo() const {
1446 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001447 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001448 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001449 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001450 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001451 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001452 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001453 info.mVisibleRegion = visibleRegion;
1454 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001455 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001456 info.mX = ds.active_legacy.transform.tx();
1457 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001458 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001459 info.mWidth = ds.active_legacy.w;
1460 info.mHeight = ds.active_legacy.h;
1461 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001462 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001463 info.mFlags = ds.flags;
1464 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001465 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001466 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1467 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1468 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1469 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001470 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001471 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001472 if (buffer != 0) {
1473 info.mActiveBufferWidth = buffer->getWidth();
1474 info.mActiveBufferHeight = buffer->getHeight();
1475 info.mActiveBufferStride = buffer->getStride();
1476 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001477 } else {
1478 info.mActiveBufferWidth = 0;
1479 info.mActiveBufferHeight = 0;
1480 info.mActiveBufferStride = 0;
1481 info.mActiveBufferFormat = 0;
1482 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001483 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001484 info.mNumQueuedFrames = getQueuedFrameCount();
1485 info.mRefreshPending = isBufferLatched();
1486 info.mIsOpaque = isOpaque(ds);
1487 info.mContentDirty = contentDirty;
1488 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001489}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001490
Yiwei Zhang5434a782018-12-05 18:06:32 -08001491void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001492 result.append("-------------------------------");
1493 result.append("-------------------------------");
1494 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001495 result.append(" Layer name\n");
1496 result.append(" Z | ");
1497 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001498 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001499 result.append(" Disp Frame (LTRB) | ");
1500 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001501 result.append("-------------------------------");
1502 result.append("-------------------------------");
1503 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001504}
1505
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001506void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1507 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1508 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001509 return;
1510 }
1511
Yiwei Zhang5434a782018-12-05 18:06:32 -08001512 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001513 if (mName.length() > 77) {
1514 std::string shortened;
1515 shortened.append(mName.string(), 36);
1516 shortened.append("[...]");
1517 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001518 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001519 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001520 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001521 }
1522
Yiwei Zhang5434a782018-12-05 18:06:32 -08001523 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001524
Alec Mourib416efd2018-09-06 21:01:59 +00001525 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001526 const auto& compositionState = outputLayer->getState();
1527
Chia-I Wu1e043612018-03-01 09:45:09 -08001528 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001529 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001530 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001531 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001532 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001533 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1534 StringAppendF(&result, "%10s | ",
1535 toString(getCompositionLayer() ? compositionState.bufferTransform
1536 : static_cast<Hwc2::Transform>(0))
1537 .c_str());
1538 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001539 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001540 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001541 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1542 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001543
Yichi Chen6ca35192018-05-29 12:20:43 +08001544 result.append("- - - - - - - - - - - - - - - -");
1545 result.append("- - - - - - - - - - - - - - - -");
1546 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001547}
Dan Stozae22aec72016-08-01 13:20:59 -07001548
Yiwei Zhang5434a782018-12-05 18:06:32 -08001549void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001550 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001551}
1552
Svetoslavd85084b2014-03-20 10:28:31 -07001553void Layer::clearFrameStats() {
1554 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001555}
1556
Jamie Gennis6547ff42013-07-16 20:12:42 -07001557void Layer::logFrameStats() {
1558 mFrameTracker.logAndResetStats(mName);
1559}
1560
Svetoslavd85084b2014-03-20 10:28:31 -07001561void Layer::getFrameStats(FrameStats* outStats) const {
1562 mFrameTracker.getStats(outStats);
1563}
1564
Yiwei Zhang5434a782018-12-05 18:06:32 -08001565void Layer::dumpFrameEvents(std::string& result) {
1566 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001567 Mutex::Autolock lock(mFrameEventHistoryMutex);
1568 mFrameEventHistory.checkFencesForCompletion();
1569 mFrameEventHistory.dump(result);
1570}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001571
Brian Anderson5ea5e592016-12-01 16:54:33 -08001572void Layer::onDisconnect() {
1573 Mutex::Autolock lock(mFrameEventHistoryMutex);
1574 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001575 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001576}
1577
Brian Anderson3890c392016-07-25 12:48:08 -07001578void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001579 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001580 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001581 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1582 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001583 }
1584
Brian Andersond6927fb2016-07-23 23:37:30 -07001585 Mutex::Autolock lock(mFrameEventHistoryMutex);
1586 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001587 // If there are any unsignaled fences in the aquire timeline at this
1588 // point, the previously queued frame hasn't been latched yet. Go ahead
1589 // and try to get the signal time here so the syscall is taken out of
1590 // the main thread's critical path.
1591 mAcquireTimeline.updateSignalTimes();
1592 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001593 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001594 mFrameEventHistory.addQueue(*newTimestamps);
1595 }
1596
Brian Anderson3890c392016-07-25 12:48:08 -07001597 if (outDelta) {
1598 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001599 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001600}
Dan Stozae77c7662016-05-13 11:37:28 -07001601
Chia-I Wu98f1c102017-05-30 14:54:08 -07001602size_t Layer::getChildrenCount() const {
1603 size_t count = 0;
1604 for (const sp<Layer>& child : mCurrentChildren) {
1605 count += 1 + child->getChildrenCount();
1606 }
1607 return count;
1608}
1609
Robert Carr1f0a16a2016-10-24 16:27:39 -07001610void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001611 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001612 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001613
Robert Carr1f0a16a2016-10-24 16:27:39 -07001614 mCurrentChildren.add(layer);
1615 layer->setParent(this);
1616}
1617
1618ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001619 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001620 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001621
Robert Carr1323c952019-01-28 18:13:27 -08001622 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001623 return mCurrentChildren.remove(layer);
1624}
1625
Robert Carr1db73f62016-12-21 12:58:51 -08001626bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1627 sp<Handle> handle = nullptr;
1628 sp<Layer> newParent = nullptr;
1629 if (newParentHandle == nullptr) {
1630 return false;
1631 }
1632 handle = static_cast<Handle*>(newParentHandle.get());
1633 newParent = handle->owner.promote();
1634 if (newParent == nullptr) {
1635 ALOGE("Unable to promote Layer handle");
1636 return false;
1637 }
1638
chaviw5aedec92018-10-22 10:40:38 -07001639 if (attachChildren()) {
1640 setTransactionFlags(eTransactionNeeded);
1641 }
Robert Carr1db73f62016-12-21 12:58:51 -08001642 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001643 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001644 }
1645 mCurrentChildren.clear();
1646
1647 return true;
1648}
1649
Robert Carr15eae092018-03-23 13:43:53 -07001650void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001651 for (const sp<Layer>& child : mDrawingChildren) {
1652 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001653 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001654 }
1655}
1656
chaviwf1961f72017-09-18 16:41:07 -07001657bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001658 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001659
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001660 // While layers are detached, we allow most operations
1661 // and simply halt performing the actual transaction. However
1662 // for reparent != null we would enter the mRemovedFromCurrentState
1663 // state, regardless of whether doTransaction was called, and
1664 // so we need to prevent the update here.
1665 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001666 return false;
1667 }
1668
Robert Carr54cf5b12019-01-25 14:02:28 -08001669 sp<Layer> newParent;
1670 if (newParentHandle != nullptr) {
1671 auto handle = static_cast<Handle*>(newParentHandle.get());
1672 newParent = handle->owner.promote();
1673 if (newParent == nullptr) {
1674 ALOGE("Unable to promote Layer handle");
1675 return false;
1676 }
1677 if (newParent == this) {
1678 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1679 return false;
1680 }
1681 }
1682
chaviwf1961f72017-09-18 16:41:07 -07001683 sp<Layer> parent = getParent();
1684 if (parent != nullptr) {
1685 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001686 }
1687
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001688 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001689 newParent->addChild(this);
1690 if (!newParent->isRemovedFromCurrentState()) {
1691 addToCurrentState();
1692 } else {
1693 onRemovedFromCurrentState();
1694 }
1695
1696 if (mLayerDetached) {
1697 mLayerDetached = false;
1698 callSetTransactionFlags = true;
1699 }
1700 } else {
1701 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001702 }
1703
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001704 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001705 setTransactionFlags(eTransactionNeeded);
1706 }
chaviw06178942017-07-27 10:25:59 -07001707 return true;
1708}
1709
Robert Carr9524cb32017-02-13 11:32:32 -08001710bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001711 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001712 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001713 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001714 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001715 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001716 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001717 }
Robert Carr7f619b22017-11-06 12:56:35 -08001718 }
Robert Carr9524cb32017-02-13 11:32:32 -08001719
1720 return true;
1721}
1722
chaviw5aedec92018-10-22 10:40:38 -07001723bool Layer::attachChildren() {
1724 bool changed = false;
1725 for (const sp<Layer>& child : mCurrentChildren) {
1726 sp<Client> parentClient = mClientRef.promote();
1727 sp<Client> client(child->mClientRef.promote());
1728 if (client != nullptr && parentClient != client) {
1729 if (child->mLayerDetached) {
1730 child->mLayerDetached = false;
1731 changed = true;
1732 }
1733 changed |= child->attachChildren();
1734 }
1735 }
1736
1737 return changed;
1738}
1739
Peiyong Lind3788632018-09-18 16:01:31 -07001740bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001741 static const mat4 identityMatrix = mat4();
1742
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001743 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001744 return false;
1745 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001746 ++mCurrentState.sequence;
1747 mCurrentState.colorTransform = matrix;
1748 mCurrentState.hasColorTransform = matrix != identityMatrix;
1749 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001750 setTransactionFlags(eTransactionNeeded);
1751 return true;
1752}
1753
chaviwf66724d2018-11-28 16:35:21 -08001754mat4 Layer::getColorTransform() const {
1755 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1756 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1757 colorTransform = parent->getColorTransform() * colorTransform;
1758 }
1759 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001760}
1761
1762bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001763 bool hasColorTransform = getDrawingState().hasColorTransform;
1764 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1765 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1766 }
1767 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001768}
1769
Chia-I Wu11481472018-05-04 10:43:19 -07001770bool Layer::isLegacyDataSpace() const {
1771 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001772 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001773 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001774}
1775
Robert Carr1f0a16a2016-10-24 16:27:39 -07001776void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001777 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778}
1779
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001781 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001782}
1783
Robert Carr29abff82017-12-04 13:51:20 -08001784bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1785 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001786 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001787 return state.zOrderRelativeOf != nullptr;
1788}
1789
David Sodman41fdfc92017-11-06 16:09:56 -08001790__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001791 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001792 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1793 "makeTraversalList received invalid stateSet");
1794 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1795 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001796 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001797
Robert Carr29abff82017-12-04 13:51:20 -08001798 if (state.zOrderRelatives.size() == 0) {
1799 *outSkipRelativeZUsers = true;
1800 return children;
1801 }
1802
chaviwfd462612018-05-31 16:11:27 -07001803 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001804 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001805 sp<Layer> strongRelative = weakRelative.promote();
1806 if (strongRelative != nullptr) {
1807 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001808 }
1809 }
1810
Dan Stoza412903f2017-04-27 13:42:17 -07001811 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001812 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001813 if (childState.zOrderRelativeOf != nullptr) {
1814 continue;
1815 }
Robert Carrdb66e622017-04-10 16:55:57 -07001816 traverse.add(child);
1817 }
1818
1819 return traverse;
1820}
1821
Robert Carr1f0a16a2016-10-24 16:27:39 -07001822/**
Robert Carrdb66e622017-04-10 16:55:57 -07001823 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001824 */
Dan Stoza412903f2017-04-27 13:42:17 -07001825void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001826 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1827 // produce a new list for traversing, including our relatives, and not including our children
1828 // who are relatives of another surface. In the case that there are no relative Z,
1829 // makeTraversalList returns our children directly to avoid significant overhead.
1830 // However in this case we need to take the responsibility for filtering children which
1831 // are relatives of another surface here.
1832 bool skipRelativeZUsers = false;
1833 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001834
Robert Carr1f0a16a2016-10-24 16:27:39 -07001835 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001836 for (; i < list.size(); i++) {
1837 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001838 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1839 continue;
1840 }
1841
Robert Carrdb66e622017-04-10 16:55:57 -07001842 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001843 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001844 }
Dan Stoza412903f2017-04-27 13:42:17 -07001845 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846 }
Robert Carr29abff82017-12-04 13:51:20 -08001847
Dan Stoza412903f2017-04-27 13:42:17 -07001848 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001849 for (; i < list.size(); i++) {
1850 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001851
Robert Carr29abff82017-12-04 13:51:20 -08001852 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1853 continue;
1854 }
Dan Stoza412903f2017-04-27 13:42:17 -07001855 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001856 }
1857}
1858
1859/**
Robert Carrdb66e622017-04-10 16:55:57 -07001860 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001861 */
Dan Stoza412903f2017-04-27 13:42:17 -07001862void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1863 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001864 // See traverseInZOrder for documentation.
1865 bool skipRelativeZUsers = false;
1866 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001867
Robert Carr1f0a16a2016-10-24 16:27:39 -07001868 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001869 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001870 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001871
1872 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;
1878 }
Dan Stoza412903f2017-04-27 13:42:17 -07001879 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001880 }
Dan Stoza412903f2017-04-27 13:42:17 -07001881 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001882 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001883 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001884
1885 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1886 continue;
1887 }
1888
Dan Stoza412903f2017-04-27 13:42:17 -07001889 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001890 }
1891}
1892
chaviw4b129c22018-04-09 16:19:43 -07001893LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1894 const std::vector<Layer*>& layersInTree) {
1895 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1896 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001897 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1898 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001899 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001900
chaviwfd462612018-05-31 16:11:27 -07001901 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001902 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1903 sp<Layer> strongRelative = weakRelative.promote();
1904 // Only add relative layers that are also descendents of the top most parent of the tree.
1905 // If a relative layer is not a descendent, then it should be ignored.
1906 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1907 traverse.add(strongRelative);
1908 }
1909 }
1910
1911 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001912 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001913 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1914 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1915 // the child here since it won't be added later as a relative.
1916 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1917 childState.zOrderRelativeOf.promote().get())) {
1918 continue;
1919 }
1920 traverse.add(child);
1921 }
1922
1923 return traverse;
1924}
1925
1926void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1927 LayerVector::StateSet stateSet,
1928 const LayerVector::Visitor& visitor) {
1929 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001930
1931 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001932 for (; i < list.size(); i++) {
1933 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001934 if (relative->getZ() >= 0) {
1935 break;
1936 }
chaviw4b129c22018-04-09 16:19:43 -07001937 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001938 }
chaviw4b129c22018-04-09 16:19:43 -07001939
chaviwa76b2712017-09-20 12:02:26 -07001940 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001941 for (; i < list.size(); i++) {
1942 const auto& relative = list[i];
1943 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001944 }
1945}
1946
chaviw4b129c22018-04-09 16:19:43 -07001947std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1948 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1949 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1950
1951 std::vector<Layer*> layersInTree = {this};
1952 for (size_t i = 0; i < children.size(); i++) {
1953 const auto& child = children[i];
1954 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1955 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1956 }
1957
1958 return layersInTree;
1959}
1960
1961void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1962 const LayerVector::Visitor& visitor) {
1963 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1964 std::sort(layersInTree.begin(), layersInTree.end());
1965 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1966}
1967
Peiyong Linefefaac2018-08-17 12:27:51 -07001968ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001969 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001970}
1971
chaviw13fdc492017-06-27 12:40:18 -07001972half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001973 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001974
chaviw13fdc492017-06-27 12:40:18 -07001975 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1976 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001977}
Robert Carr6452f122017-03-21 10:41:29 -07001978
chaviw13fdc492017-06-27 12:40:18 -07001979half4 Layer::getColor() const {
1980 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001981 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001982}
Robert Carr6452f122017-03-21 10:41:29 -07001983
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001984Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1985 const auto& p = mDrawingParent.promote();
1986 if (p != nullptr) {
1987 RoundedCornerState parentState = p->getRoundedCornerState();
1988 if (parentState.radius > 0) {
1989 ui::Transform t = getActiveTransform(getDrawingState());
1990 t = t.inverse();
1991 parentState.cropRect = t.transform(parentState.cropRect);
1992 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1993 // but a transform matrix can define horizontal and vertical scales.
1994 // Let's take the average between both of them and pass into the shader, practically we
1995 // never do this type of transformation on windows anyway.
1996 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1997 return parentState;
1998 }
1999 }
2000 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002001 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002002}
2003
Robert Carr1f0a16a2016-10-24 16:27:39 -07002004void Layer::commitChildList() {
2005 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2006 const auto& child = mCurrentChildren[i];
2007 child->commitChildList();
2008 }
2009 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002010 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002011}
2012
Robert Carr720e5062018-07-30 17:45:14 -07002013void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002014 mCurrentState.inputInfo = info;
2015 mCurrentState.modified = true;
2016 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002017 setTransactionFlags(eTransactionNeeded);
2018}
2019
chaviw1d044282017-09-27 12:19:28 -07002020void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2021 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2022 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002023 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002024
Peiyong Linefefaac2018-08-17 12:27:51 -07002025 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002026 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002027
2028 layerInfo->set_id(sequence);
2029 layerInfo->set_name(getName().c_str());
2030 layerInfo->set_type(String8(getTypeId()));
2031
2032 for (const auto& child : children) {
2033 layerInfo->add_children(child->sequence);
2034 }
2035
2036 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2037 sp<Layer> strongRelative = weakRelative.promote();
2038 if (strongRelative != nullptr) {
2039 layerInfo->add_relatives(strongRelative->sequence);
2040 }
2041 }
2042
Marissa Wallf58c14b2018-07-24 10:50:43 -07002043 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08002044 [&]() { return layerInfo->mutable_transparent_region(); });
2045 LayerProtoHelper::writeToProto(visibleRegion,
2046 [&]() { return layerInfo->mutable_visible_region(); });
2047 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2048 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07002049
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002050 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002051 layerInfo->set_z(state.z);
2052
Nataniel Borges797b0e42019-02-15 14:11:58 -08002053 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2054 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07002055
Nataniel Borges797b0e42019-02-15 14:11:58 -08002056 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2057 return layerInfo->mutable_requested_position();
2058 });
chaviw1d044282017-09-27 12:19:28 -07002059
Nataniel Borges797b0e42019-02-15 14:11:58 -08002060 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2061 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07002062
Nataniel Borges797b0e42019-02-15 14:11:58 -08002063 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002064 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002065
2066 layerInfo->set_is_opaque(isOpaque(state));
2067 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002068
2069 // XXX (b/79210409) mCurrentDataSpace is not protected
2070 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2071
chaviw1d044282017-09-27 12:19:28 -07002072 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08002073 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2074 LayerProtoHelper::writeToProto(state.color,
2075 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07002076 layerInfo->set_flags(state.flags);
2077
2078 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2079 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2080
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002081 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002082 if (parent != nullptr) {
2083 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002084 } else {
2085 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002086 }
2087
2088 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2089 if (zOrderRelativeOf != nullptr) {
2090 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002091 } else {
2092 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002093 }
2094
Lloyd Pique0b785d82018-12-04 17:25:27 -08002095 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002096 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08002097 LayerProtoHelper::writeToProto(buffer,
2098 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07002099 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002100 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002101 }
2102
2103 layerInfo->set_queued_frames(getQueuedFrameCount());
2104 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002105 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002106 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002107
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002108 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002109 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002110 if (barrierLayer != nullptr) {
2111 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2112 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002113 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002114 }
2115 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002116
2117 auto protoMap = layerInfo->mutable_metadata();
2118 for (const auto& entry : state.metadata.mMap) {
2119 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2120 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002121 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08002122 LayerProtoHelper::writeToProto(mSourceBounds,
2123 [&]() { return layerInfo->mutable_source_bounds(); });
2124 LayerProtoHelper::writeToProto(mScreenBounds,
2125 [&]() { return layerInfo->mutable_screen_bounds(); });
2126 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07002127}
2128
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002129void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2130 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2131 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002132 return;
2133 }
2134
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002135 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2136
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002137 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002138
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002139 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002140 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002141
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002142 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002143 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002144
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002145 const int32_t transform =
2146 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002147 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002148
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002149 const int32_t compositionType =
2150 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2151 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002152 layerInfo->set_hwc_composition_type(compositionType);
2153
2154 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2155 static_cast<BufferLayer*>(this)->isProtected()) {
2156 layerInfo->set_is_protected(true);
2157 } else {
2158 layerInfo->set_is_protected(false);
2159 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002160}
2161
Robert Carr2e102c92018-10-23 12:11:15 -07002162bool Layer::isRemovedFromCurrentState() const {
2163 return mRemovedFromCurrentState;
2164}
2165
Arthur Hungd20b2702019-01-14 18:16:16 +08002166InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002167 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002168
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002169 if (info.displayId == ADISPLAY_ID_NONE) {
2170 info.displayId = mDrawingState.layerStack;
2171 }
2172
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002173 ui::Transform t = getTransform();
2174 const float xScale = t.sx();
2175 const float yScale = t.sy();
2176 if (xScale != 1.0f || yScale != 1.0f) {
2177 info.windowXScale *= 1.0f / xScale;
2178 info.windowYScale *= 1.0f / yScale;
2179 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002180 }
Robert Carre07e1032018-11-26 12:55:53 -08002181
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002182 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002183 // If this is a portal window, set the touchableRegion to the layerBounds.
2184 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2185 ? getBufferSize(getDrawingState())
2186 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002187 if (!layerBounds.isValid()) {
2188 layerBounds = getCroppedBufferSize(getDrawingState());
2189 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002190 layerBounds = t.transform(layerBounds);
2191 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2192
Arthur Hungd20b2702019-01-14 18:16:16 +08002193 // Input coordinate should match the layer bounds.
2194 info.frameLeft = layerBounds.left;
2195 info.frameTop = layerBounds.top;
2196 info.frameRight = layerBounds.right;
2197 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002198
2199 // Position the touchable region relative to frame screen location and restrict it to frame
2200 // bounds.
2201 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002202 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002203 return info;
2204}
2205
2206bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002207 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002208}
2209
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002210std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2211 return nullptr;
2212}
2213
chaviw3e727cd2019-01-31 13:41:05 -08002214bool Layer::canReceiveInput() const {
2215 return isVisible();
2216}
2217
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002218compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2219 const sp<const DisplayDevice>& display) const {
2220 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2221}
2222
Mathias Agopian13127d82013-03-05 17:47:11 -08002223// ---------------------------------------------------------------------------
2224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002225}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002226
2227#if defined(__gl_h_)
2228#error "don't include gl/gl.h in this file"
2229#endif
2230
2231#if defined(__gl2_h_)
2232#error "don't include gl2/gl2.h in this file"
2233#endif