blob: 69ec638836ce2a8d247f5c27f0e02b35d9e3fa5a [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>
31#include <compositionengine/OutputLayer.h>
32#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080036#include <gui/BufferItem.h>
37#include <gui/LayerDebugInfo.h>
38#include <gui/Surface.h>
39#include <renderengine/RenderEngine.h>
40#include <ui/DebugUtils.h>
41#include <ui/GraphicBuffer.h>
42#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <utils/Errors.h>
44#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080045#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080047#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Yiwei Zhang60d1a192018-03-07 14:52:28 -080049#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080050#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020051#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070052#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080053#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080055#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070056#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070057#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080059#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Yiwei Zhang5434a782018-12-05 18:06:32 -080065using base::StringAppendF;
66
Lloyd Piquef1c675b2018-09-12 20:45:39 -070067std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080068
Lloyd Pique42ab75e2018-09-12 20:46:03 -070069Layer::Layer(const LayerCreationArgs& args)
70 : mFlinger(args.flinger),
71 mName(args.name),
72 mClientRef(args.client),
73 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070075
76 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070077 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
78 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
79 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Dan Stozaf7ba41a2017-05-10 15:11:11 -070081 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082
Lloyd Pique0449b0f2018-12-20 16:23:45 -080083 mCurrentState.active_legacy.w = args.w;
84 mCurrentState.active_legacy.h = args.h;
85 mCurrentState.flags = layerFlags;
86 mCurrentState.active_legacy.transform.set(0, 0);
87 mCurrentState.crop_legacy.makeInvalid();
88 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
89 mCurrentState.z = 0;
90 mCurrentState.color.a = 1.0f;
91 mCurrentState.layerStack = 0;
92 mCurrentState.sequence = 0;
93 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080094 mCurrentState.active.w = UINT32_MAX;
95 mCurrentState.active.h = UINT32_MAX;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.cornerRadius = 0.0f;
105 mCurrentState.api = -1;
106 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
108 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800109 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700110
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800111 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700112 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800113 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200114 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700115
116 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700117}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700118
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800120 sp<Client> c(mClientRef.promote());
121 if (c != 0) {
122 c->detachLayer(this);
123 }
124
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000125 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700126
chaviw61626f22018-11-15 16:26:27 -0800127 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700128
129 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700130}
131
Mathias Agopian13127d82013-03-05 17:47:11 -0800132// ---------------------------------------------------------------------------
133// callbacks
134// ---------------------------------------------------------------------------
135
David Sodmaneb085e02017-10-05 18:49:04 -0700136/*
137 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
138 * Layer. So, the implementation is done in BufferLayer. When called on a
139 * ColorLayer object, it's essentially a NOP.
140 */
David Sodmaneb085e02017-10-05 18:49:04 -0700141void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800142
Chia-I Wuc6657022017-08-15 11:18:17 -0700143void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700144 mRemovedFromCurrentState = true;
145
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800146 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
147 if (mCurrentState.zOrderRelativeOf != nullptr) {
148 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
149 if (strongRelative != nullptr) {
150 strongRelative->removeZOrderRelative(this);
151 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700152 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800153 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carr5edb1ad2017-04-25 10:54:24 -0700154 }
Rob Carr4bba3702018-10-08 21:53:30 +0000155
Robert Carr2e102c92018-10-23 12:11:15 -0700156 // Since we are no longer reachable from CurrentState SurfaceFlinger
157 // will no longer invoke doTransaction for us, and so we will
158 // never finish applying transactions. We signal the sync point
159 // now so that another layer will not become indefinitely
160 // blocked.
161 for (auto& point: mRemoteSyncPoints) {
162 point->setTransactionApplied();
163 }
164 mRemoteSyncPoints.clear();
165
166 {
167 Mutex::Autolock syncLock(mLocalSyncPointMutex);
168 for (auto& point : mLocalSyncPoints) {
169 point->setFrameAvailable();
170 }
171 mLocalSyncPoints.clear();
172 }
173
Chia-I Wuc6657022017-08-15 11:18:17 -0700174 for (const auto& child : mCurrentChildren) {
175 child->onRemovedFromCurrentState();
176 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800177
178 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700179}
Chia-I Wu38512252017-05-17 14:36:16 -0700180
chaviw61626f22018-11-15 16:26:27 -0800181void Layer::addToCurrentState() {
182 mRemovedFromCurrentState = false;
183
184 for (const auto& child : mCurrentChildren) {
185 child->addToCurrentState();
186 }
187}
188
Mathias Agopian13127d82013-03-05 17:47:11 -0800189// ---------------------------------------------------------------------------
190// set-up
191// ---------------------------------------------------------------------------
192
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700193const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800194 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
chaviw13fdc492017-06-27 12:40:18 -0700197bool Layer::getPremultipledAlpha() const {
198 return mPremultipliedAlpha;
199}
200
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700201sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700203 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800204}
205
206// ---------------------------------------------------------------------------
207// h/w composer set-up
208// ---------------------------------------------------------------------------
209
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800210bool Layer::createHwcLayer(HWComposer* hwc, const sp<DisplayDevice>& displayDevice) {
211 LOG_ALWAYS_FATAL_IF(!displayDevice->getId());
212 auto displayId = *displayDevice->getId();
213 auto outputLayer = findOutputLayerForDisplay(displayDevice);
214 LOG_ALWAYS_FATAL_IF(!outputLayer);
215
216 LOG_ALWAYS_FATAL_IF(outputLayer->getState().hwc.has_value(),
217 "Already have a layer for display %s",
218 displayDevice->getDisplayName().c_str());
219
David Sodmanb8aaea12017-12-14 15:54:51 -0800220 auto layer = std::shared_ptr<HWC2::Layer>(
221 hwc->createLayer(displayId),
222 [hwc, displayId](HWC2::Layer* layer) {
223 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700224 if (!layer) {
225 return false;
226 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800227 auto& state = outputLayer->editState();
228 state.hwc.emplace(layer);
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 return true;
230}
231
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800232bool Layer::destroyHwcLayer(const sp<DisplayDevice>& displayDevice) {
233 auto outputLayer = findOutputLayerForDisplay(displayDevice);
234 if (outputLayer == nullptr) {
Chia-I Wu83806892017-11-16 10:50:20 -0800235 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700236 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800237 auto& state = outputLayer->editState();
238 bool result = state.hwc.has_value();
239 state.hwc.reset();
240 return result;
Steven Thomasb02664d2017-07-26 18:48:28 -0700241}
242
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800243bool Layer::destroyHwcLayersForAllDisplays() {
244 bool destroyedAnyLayers = false;
245
246 for (const auto& [token, displayDevice] : mFlinger->mDisplays) {
247 if (destroyHwcLayer(displayDevice)) {
248 destroyedAnyLayers = true;
249 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700250 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800251
252 return destroyedAnyLayers;
chaviw61626f22018-11-15 16:26:27 -0800253}
254
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800255bool Layer::destroyAllHwcLayersPlusChildren() {
256 bool result = destroyHwcLayersForAllDisplays();
Robert Carr2e102c92018-10-23 12:11:15 -0700257
258 for (const sp<Layer>& child : mDrawingChildren) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800259 result |= child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700260 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800261
262 return result;
263}
264
265bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
266 auto outputLayer = findOutputLayerForDisplay(displayDevice);
267 LOG_FATAL_IF(!outputLayer);
268 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
269}
270
271HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
272 auto outputLayer = findOutputLayerForDisplay(displayDevice);
273 if (!outputLayer || !outputLayer->getState().hwc) {
274 return nullptr;
275 }
276 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700277}
Steven Thomasb02664d2017-07-26 18:48:28 -0700278
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800279Rect Layer::getContentCrop() const {
280 // this is the crop rectangle that applies to the buffer
281 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700282 Rect crop;
283 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800284 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700285 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800286 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800287 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800288 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700289 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800290 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700291 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700292 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700293 return crop;
294}
295
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700296static Rect reduce(const Rect& win, const Region& exclude) {
297 if (CC_LIKELY(exclude.isEmpty())) {
298 return win;
299 }
300 if (exclude.isRect()) {
301 return win.reduce(exclude.getBounds());
302 }
303 return Region(win).subtract(exclude).getBounds();
304}
305
Dan Stoza80d61162017-12-20 15:57:52 -0800306static FloatRect reduce(const FloatRect& win, const Region& exclude) {
307 if (CC_LIKELY(exclude.isEmpty())) {
308 return win;
309 }
310 // Convert through Rect (by rounding) for lack of FloatRegion
311 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
312}
313
Vishnu Nair4351ad52019-02-11 14:13:02 -0800314Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800315 if (!reduceTransparentRegion) {
316 return Rect{mScreenBounds};
317 }
318
319 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800320 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800321 // Transform to screen space.
322 bounds = t.transform(bounds);
323 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700324}
325
Vishnu Nair4351ad52019-02-11 14:13:02 -0800326FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000327 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800328 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700329}
330
Vishnu Nairf0c28512019-02-08 12:40:28 -0800331FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
332 // Subtract the transparent region and snap to the bounds.
333 return reduce(mBounds, activeTransparentRegion);
334}
335
Vishnu Nair4351ad52019-02-11 14:13:02 -0800336ui::Transform Layer::getTransformWithScale() const {
337 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
338 // it isFixedSize) then there may be additional scaling not accounted
339 // for in the transform. We need to mirror this scaling to child surfaces
340 // or we will break the contract where WM can treat child surfaces as
341 // pixels in the parent surface.
342 if (!isFixedSize() || !getBE().compositionInfo.mBuffer) {
343 return mEffectiveTransform;
344 }
345
346 int bufferWidth;
347 int bufferHeight;
348 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
349 bufferWidth = getBE().compositionInfo.mBuffer->getWidth();
350 bufferHeight = getBE().compositionInfo.mBuffer->getHeight();
351 } else {
352 bufferHeight = getBE().compositionInfo.mBuffer->getWidth();
353 bufferWidth = getBE().compositionInfo.mBuffer->getHeight();
354 }
355 float sx = getActiveWidth(getDrawingState()) / static_cast<float>(bufferWidth);
356 float sy = getActiveHeight(getDrawingState()) / static_cast<float>(bufferHeight);
357 ui::Transform extraParentScaling;
358 extraParentScaling.set(sx, 0, 0, sy);
359 return mEffectiveTransform * extraParentScaling;
360}
361
362void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
363 const State& s(getDrawingState());
364
365 // Calculate effective layer transform
366 mEffectiveTransform = parentTransform * getActiveTransform(s);
367
368 // Transform parent bounds to layer space
369 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
370
371 // Calculate display frame
372 mSourceBounds = computeSourceBounds(parentBounds);
373
374 // Calculate bounds by croping diplay frame with layer crop and parent bounds
375 FloatRect bounds = mSourceBounds;
376 const Rect layerCrop = getCrop(s);
377 if (!layerCrop.isEmpty()) {
378 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
379 }
380 bounds = bounds.intersect(parentBounds);
381
382 mBounds = bounds;
383 mScreenBounds = mEffectiveTransform.transform(mBounds);
384 for (const sp<Layer>& child : mDrawingChildren) {
385 child->computeBounds(mBounds, getTransformWithScale());
386 }
387}
388
Robert Carrb5d3d262016-03-25 15:08:13 -0700389
Robert Carr1f0a16a2016-10-24 16:27:39 -0700390
Vishnu Nair60356342018-11-13 13:00:45 -0800391Rect Layer::getCroppedBufferSize(const State& s) const {
392 Rect size = getBufferSize(s);
393 Rect crop = getCrop(s);
394 if (!crop.isEmpty() && size.isValid()) {
395 size.intersect(crop, &size);
396 } else if (!crop.isEmpty()) {
397 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700398 }
Vishnu Nair60356342018-11-13 13:00:45 -0800399 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800400}
401
Alec Mouri1f3bd182019-01-24 15:20:18 +0000402Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700403 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800404 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700405 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800406
407 // apply the projection's clipping to the window crop in
408 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700409 // if there are no window scaling involved, this operation will map to full
410 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700411
Vishnu Nair4351ad52019-02-11 14:13:02 -0800412 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000413 ui::Transform t = getTransform();
414 // Transform to screen space.
415 activeCropFloat = t.transform(activeCropFloat);
416 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
417 // Back to layer space to work with the content crop.
418 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800419 // This needs to be here as transform.transform(Rect) computes the
420 // transformed rect and then takes the bounding box of the result before
421 // returning. This means
422 // transform.inverse().transform(transform.transform(Rect)) != Rect
423 // in which case we need to make sure the final rect is clipped to the
424 // display bounds.
425 Rect activeCrop{activeCropFloat};
426 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000427 activeCrop.clear();
428 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700429 return activeCrop;
430}
431
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700432void Layer::setupRoundedCornersCropCoordinates(Rect win,
433 const FloatRect& roundedCornersCrop) const {
434 // Translate win by the rounded corners rect coordinates, to have all values in
435 // layer coordinate space.
436 win.left -= roundedCornersCrop.left;
437 win.right -= roundedCornersCrop.left;
438 win.top -= roundedCornersCrop.top;
439 win.bottom -= roundedCornersCrop.top;
440
441 renderengine::Mesh::VertexArray<vec2> cropCoords(getBE().mMesh.getCropCoordArray<vec2>());
442 cropCoords[0] = vec2(win.left, win.top);
443 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
444 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
445 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700446}
447
Alec Mouri1f3bd182019-01-24 15:20:18 +0000448FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700449 // the content crop is the area of the content that gets scaled to the
450 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800451 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700452
453 // In addition there is a WM-specified crop we pull from our drawing state.
454 const State& s(getDrawingState());
455
Alec Mouri1f3bd182019-01-24 15:20:18 +0000456 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800457 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700458
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000459 // Transform the window crop to match the buffer coordinate system,
460 // which means using the inverse of the current transform set on the
461 // SurfaceFlingerConsumer.
462 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800463 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700465 * the code below applies the primary display's inverse transform to the
466 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 */
David Sodman41fdfc92017-11-06 16:09:56 -0800468 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000469 // calculate the inverse transform
470 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800471 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800472 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000473 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700474 invTransform = (ui::Transform(invTransformOrient) *
475 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800476 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000477
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800478 int winWidth = bufferSize.getWidth();
479 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000480 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
481 // If the activeCrop has been rotate the ends are rotated but not
482 // the space itself so when transforming ends back we can't rely on
483 // a modification of the axes of rotation. To account for this we
484 // need to reorient the inverse rotation in terms of the current
485 // axes of rotation.
486 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
487 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
488 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800489 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000490 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800491 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000492 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800493 const Rect winCrop =
494 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000495
496 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800497 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000498 float yScale = crop.getHeight() / float(winHeight);
499
David Sodman41fdfc92017-11-06 16:09:56 -0800500 float insetL = winCrop.left * xScale;
501 float insetT = winCrop.top * yScale;
502 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000503 float insetB = (winHeight - winCrop.bottom) * yScale;
504
David Sodman41fdfc92017-11-06 16:09:56 -0800505 crop.left += insetL;
506 crop.top += insetT;
507 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000508 crop.bottom -= insetB;
509
Mathias Agopian13127d82013-03-05 17:47:11 -0800510 return crop;
511}
512
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700513void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800514 const auto outputLayer = findOutputLayerForDisplay(display);
515 LOG_FATAL_IF(!outputLayer);
516 LOG_FATAL_IF(!outputLayer->getState().hwc);
517 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700518
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800519 if (!hasHwcLayer(display)) {
520 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
521 display->getDebugName().c_str());
522 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800523 }
524
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800525 auto& compositionState = outputLayer->editState();
526
527 // enable this layer
528 compositionState.forceClientComposition = false;
529
530 if (isSecure() && !display->isSecure()) {
531 compositionState.forceClientComposition = true;
532 }
David Sodman15094112018-10-11 09:39:37 -0700533
Mathias Agopian13127d82013-03-05 17:47:11 -0800534 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700535 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800536 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500537 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800538 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800539 blendMode =
540 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541 }
David Sodman15094112018-10-11 09:39:37 -0700542 auto error = hwcLayer->setBlendMode(blendMode);
543 ALOGE_IF(error != HWC2::Error::None,
544 "[%s] Failed to set blend mode %s:"
545 " %s (%d)",
546 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
547 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700548 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800549
550 // apply the layer's transform, followed by the display's global transform
551 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700552 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800553 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700554 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800555 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700556 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700557 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000558 activeCrop.clear();
559 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700560 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800561 // This needs to be here as transform.transform(Rect) computes the
562 // transformed rect and then takes the bounding box of the result before
563 // returning. This means
564 // transform.inverse().transform(transform.transform(Rect)) != Rect
565 // in which case we need to make sure the final rect is clipped to the
566 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800567 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000568 activeCrop.clear();
569 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700570 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800571 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
572 activeTransparentRegion.orSelf(
573 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800574 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
575 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800576 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700577 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700578
Vishnu Nair4351ad52019-02-11 14:13:02 -0800579 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800580 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800581 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700582 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000583 frame.clear();
584 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700585 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700587 error = hwcLayer->setDisplayFrame(transformedFrame);
588 if (error != HWC2::Error::None) {
589 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
590 transformedFrame.left, transformedFrame.top, transformedFrame.right,
591 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
592 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800593 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700594 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700596 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700597 error = hwcLayer->setSourceCrop(sourceCrop);
598 if (error != HWC2::Error::None) {
599 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
600 "%s (%d)",
601 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
602 to_string(error).c_str(), static_cast<int32_t>(error));
603 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800604 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700605 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800606
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800607 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700608 error = hwcLayer->setPlaneAlpha(alpha);
609 ALOGE_IF(error != HWC2::Error::None,
610 "[%s] Failed to set plane alpha %.3f: "
611 "%s (%d)",
612 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700613 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800614
David Sodman15094112018-10-11 09:39:37 -0700615 error = hwcLayer->setZOrder(z);
616 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
617 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800618 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500619
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800620 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
621 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700622 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400623 if (parent.get()) {
624 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800625 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
626 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
627 if (parentType >= 0 || parentAppId >= 0) {
628 type = parentType;
629 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700630 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400631 }
632
David Sodman15094112018-10-11 09:39:37 -0700633 error = hwcLayer->setInfo(type, appId);
634 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
635 static_cast<int32_t>(error));
636
David Sodmanba340492018-08-05 21:51:33 -0700637 getBE().compositionInfo.hwc.type = type;
638 getBE().compositionInfo.hwc.appId = appId;
639
Mathias Agopian29a367b2011-07-12 14:51:45 -0700640 /*
641 * Transformations are applied in this order:
642 * 1) buffer orientation/flip/mirror
643 * 2) state transformation (window manager)
644 * 3) layer orientation (screen orientation)
645 * (NOTE: the matrices are multiplied in reverse order)
646 */
647
Peiyong Linefefaac2018-08-17 12:27:51 -0700648 const ui::Transform bufferOrientation(mCurrentTransform);
649 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700650
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800651 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700652 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700653 * the code below applies the primary display's inverse transform to the
654 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700655 */
David Sodman41fdfc92017-11-06 16:09:56 -0800656 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700657 // calculate the inverse transform
658 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800659 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700660 }
Robert Carrcae605c2017-03-29 12:10:31 -0700661
662 /*
663 * Here we cancel out the orientation component of the WM transform.
664 * The scaling and translate components are already included in our bounds
665 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000666 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700667 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700668 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700669 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700670
671 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800672 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700673 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800675 compositionState.forceClientComposition = true;
676 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677 } else {
678 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700679 auto error = hwcLayer->setTransform(transform);
680 ALOGE_IF(error != HWC2::Error::None,
681 "[%s] Failed to set transform %s: "
682 "%s (%d)",
683 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
684 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800685 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800686 }
687}
688
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800689void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
690 const auto outputLayer = findOutputLayerForDisplay(display);
691 LOG_FATAL_IF(!outputLayer);
692 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800693}
Dan Stozaee44edd2015-03-23 15:50:23 -0700694
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800695bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
696 const auto outputLayer = findOutputLayerForDisplay(display);
697 LOG_FATAL_IF(!outputLayer);
698 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800699}
700
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700701void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800702 const auto outputLayer = findOutputLayerForDisplay(display);
703 LOG_FATAL_IF(!outputLayer);
704
705 if (!outputLayer->getState().hwc ||
706 (*outputLayer->getState().hwc).hwcCompositionType !=
707 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708 return;
709 }
710
711 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800712 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800713
714 // Apply the layer's transform, followed by the display's global transform
715 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800716 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800717 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700718 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800719 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700720 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700721 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722 auto position = displayTransform.transform(frame);
723
Dominik Laskowski7e045462018-05-30 13:02:02 -0700724 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800725 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800726 ALOGE_IF(error != HWC2::Error::None,
727 "[%s] Failed to set cursor position "
728 "to (%d, %d): %s (%d)",
729 mName.string(), position.left, position.top, to_string(error).c_str(),
730 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800731}
Riley Andrews03414a12014-07-01 14:22:59 -0700732
Mathias Agopian13127d82013-03-05 17:47:11 -0800733// ---------------------------------------------------------------------------
734// drawing...
735// ---------------------------------------------------------------------------
736
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000737bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
738 Region& clearRegion, renderengine::LayerSettings& layer) {
739 return prepareClientLayer(renderArea, clip, false, clearRegion, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800740}
741
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000742bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
743 Region& clearRegion, renderengine::LayerSettings& layer) {
744 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
745 clearRegion, layer);
746}
747
748bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
749 bool useIdentityTransform, Region& /*clearRegion*/,
750 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800751 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000752 half alpha = getAlpha();
753 layer.geometry.boundaries = bounds;
754 if (useIdentityTransform) {
755 layer.geometry.positionTransform = mat4();
756 } else {
757 const ui::Transform transform = getTransform();
758 mat4 m;
759 m[0][0] = transform[0][0];
760 m[0][1] = transform[0][1];
761 m[0][3] = transform[0][2];
762 m[1][0] = transform[1][0];
763 m[1][1] = transform[1][1];
764 m[1][3] = transform[1][2];
765 m[3][0] = transform[2][0];
766 m[3][1] = transform[2][1];
767 m[3][3] = transform[2][2];
768 layer.geometry.positionTransform = m;
769 }
770
771 if (hasColorTransform()) {
772 layer.colorTransform = getColorTransform();
773 }
774
775 const auto roundedCornerState = getRoundedCornerState();
776 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
777 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
778
779 layer.alpha = alpha;
780 layer.sourceDataspace = mCurrentDataSpace;
781 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800782}
783
David Sodman41fdfc92017-11-06 16:09:56 -0800784void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
785 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800786 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700787 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700788 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700789 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800790}
791
David Sodmanc1498e62018-09-12 14:36:26 -0700792void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
793 clearWithOpenGL(renderArea, 0, 0, 0, 0);
794}
795
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800796void Layer::setCompositionType(const sp<const DisplayDevice>& display,
797 Hwc2::IComposerClient::Composition type) {
798 const auto outputLayer = findOutputLayerForDisplay(display);
799 LOG_FATAL_IF(!outputLayer);
800 LOG_FATAL_IF(!outputLayer->getState().hwc);
801 auto& compositionState = outputLayer->editState();
802
803 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
804 toString(type).c_str(), 1);
805 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700806 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800807 (*compositionState.hwc).hwcCompositionType = type;
808
809 auto error = (*compositionState.hwc)
810 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
811 ALOGE_IF(error != HWC2::Error::None,
812 "[%s] Failed to set "
813 "composition type %s: %s (%d)",
814 mName.string(), toString(type).c_str(), to_string(error).c_str(),
815 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800816 }
817}
818
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800819Hwc2::IComposerClient::Composition Layer::getCompositionType(
820 const sp<const DisplayDevice>& display) const {
821 const auto outputLayer = findOutputLayerForDisplay(display);
822 LOG_FATAL_IF(!outputLayer);
823 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
824 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800825}
826
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800827bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
828 const auto outputLayer = findOutputLayerForDisplay(display);
829 LOG_FATAL_IF(!outputLayer);
830 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800831}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800832
Dan Stozacac35382016-01-27 12:21:06 -0800833bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
834 if (point->getFrameNumber() <= mCurrentFrameNumber) {
835 // Don't bother with a SyncPoint, since we've already latched the
836 // relevant frame
837 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700838 }
Robert Carr2e102c92018-10-23 12:11:15 -0700839 if (isRemovedFromCurrentState()) {
840 return false;
841 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700842
Dan Stozacac35382016-01-27 12:21:06 -0800843 Mutex::Autolock lock(mLocalSyncPointMutex);
844 mLocalSyncPoints.push_back(point);
845 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700846}
847
Mathias Agopian13127d82013-03-05 17:47:11 -0800848// ----------------------------------------------------------------------------
849// local state
850// ----------------------------------------------------------------------------
851
Peiyong Lin833074a2018-08-28 11:53:54 -0700852void Layer::computeGeometry(const RenderArea& renderArea,
853 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700854 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700855 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800856 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700857
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000858 vec2 lt = vec2(win.left, win.top);
859 vec2 lb = vec2(win.left, win.bottom);
860 vec2 rb = vec2(win.right, win.bottom);
861 vec2 rt = vec2(win.right, win.top);
862
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800863 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000864 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700865 lt = layerTransform.transform(lt);
866 lb = layerTransform.transform(lb);
867 rb = layerTransform.transform(rb);
868 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000869 }
870
Peiyong Lin833074a2018-08-28 11:53:54 -0700871 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700872 position[0] = renderAreaTransform.transform(lt);
873 position[1] = renderAreaTransform.transform(lb);
874 position[2] = renderAreaTransform.transform(rb);
875 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800876}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800877
David Sodman41fdfc92017-11-06 16:09:56 -0800878bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800879 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700880 return (s.flags & layer_state_t::eLayerSecure);
881}
882
Mathias Agopian13127d82013-03-05 17:47:11 -0800883void Layer::setVisibleRegion(const Region& visibleRegion) {
884 // always called from main thread
885 this->visibleRegion = visibleRegion;
886}
887
888void Layer::setCoveredRegion(const Region& coveredRegion) {
889 // always called from main thread
890 this->coveredRegion = coveredRegion;
891}
892
David Sodman41fdfc92017-11-06 16:09:56 -0800893void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800894 // always called from main thread
895 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
896}
897
Robert Carre5f4f692018-01-12 13:12:28 -0800898void Layer::clearVisibilityRegions() {
899 visibleRegion.clear();
900 visibleNonTransparentRegion.clear();
901 coveredRegion.clear();
902}
903
Mathias Agopian13127d82013-03-05 17:47:11 -0800904// ----------------------------------------------------------------------------
905// transaction
906// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800907
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800908void Layer::pushPendingState() {
909 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 return;
911 }
912
Dan Stoza7dde5992015-05-22 09:51:44 -0700913 // If this transaction is waiting on the receipt of a frame, generate a sync
914 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700915 // We don't allow installing sync points after we are removed from the current state
916 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800917 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
918 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800919 if (barrierLayer == nullptr) {
920 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700921 // If we can't promote the layer we are intended to wait on,
922 // then it is expired or otherwise invalid. Allow this transaction
923 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800924 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800925 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800926 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800927 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800928 mRemoteSyncPoints.push_back(std::move(syncPoint));
929 } else {
930 // We already missed the frame we're supposed to synchronize
931 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800932 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800933 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 }
935
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 // Wake us up to check if the frame has been received
937 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700938 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800940 mPendingStates.push_back(mCurrentState);
941 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700942}
943
Pablo Ceballos05289c22016-04-14 15:49:55 -0700944void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800945 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700946
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800947 mPendingStates.removeAt(0);
948 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700949}
950
Pablo Ceballos05289c22016-04-14 15:49:55 -0700951bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700952 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800953 while (!mPendingStates.empty()) {
954 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700955 if (mRemoteSyncPoints.empty()) {
956 // If we don't have a sync point for this, apply it anyway. It
957 // will be visually wrong, but it should keep us from getting
958 // into too much trouble.
959 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700960 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700961 stateUpdateAvailable = true;
962 continue;
963 }
964
Marissa Wallf58c14b2018-07-24 10:50:43 -0700965 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800967 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800968
969 // Signal our end of the sync point and then dispose of it
970 mRemoteSyncPoints.front()->setTransactionApplied();
971 mRemoteSyncPoints.pop_front();
972 continue;
973 }
974
Dan Stoza7dde5992015-05-22 09:51:44 -0700975 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
976 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700977 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700978 stateUpdateAvailable = true;
979
980 // Signal our end of the sync point and then dispose of it
981 mRemoteSyncPoints.front()->setTransactionApplied();
982 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800983 } else {
984 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700985 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700986 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700987 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700988 stateUpdateAvailable = true;
989 }
990 }
991
992 // If we still have pending updates, wake SurfaceFlinger back up and point
993 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800994 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700995 setTransactionFlags(eTransactionNeeded);
996 mFlinger->setTransactionFlags(eTraversalNeeded);
997 }
998
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800999 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001000 return stateUpdateAvailable;
1001}
1002
Marissa Wall61c58622018-07-18 10:12:20 -07001003uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +00001004 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005
Marissa Wall61c58622018-07-18 10:12:20 -07001006 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
1007 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001008
David Sodmaneb085e02017-10-05 18:49:04 -07001009 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001010 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001011 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001012 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1013 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1014 " requested={ wh={%4u,%4u} }}\n"
1015 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1016 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -07001017 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -07001018 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
1019 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
1020 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
1021 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
1022 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -07001023 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1024 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1025 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001027
Robert Carre392b552017-09-19 12:16:05 -07001028 // Don't let Layer::doTransaction update the drawing state
1029 // if we have a pending resize, unless we are in fixed-size mode.
1030 // the drawing state will be updated only once we receive a buffer
1031 // with the correct size.
1032 //
1033 // In particular, we want to make sure the clip (which is part
1034 // of the geometry state) is latched together with the size but is
1035 // latched immediately when no resizing is involved.
1036 //
1037 // If a sideband stream is attached, however, we want to skip this
1038 // optimization so that transactions aren't missed when a buffer
1039 // never arrives
1040 //
1041 // In the case that we don't have a buffer we ignore other factors
1042 // and avoid entering the resizePending state. At a high level the
1043 // resizePending state is to avoid applying the state of the new buffer
1044 // to the old buffer. However in the state where we don't have an old buffer
1045 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001046 const bool resizePending =
1047 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1048 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001049 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001050 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001051 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001052 flags |= eDontUpdateGeometryState;
1053 }
1054 }
1055
Robert Carr7bf247e2017-05-18 14:02:49 -07001056 // Here we apply various requested geometry states, depending on our
1057 // latching configuration. See Layer.h for a detailed discussion of
1058 // how geometry latching is controlled.
1059 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001060 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001061
1062 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1063 // mode, which causes attributes which normally latch regardless of scaling mode,
1064 // to be delayed. We copy the requested state to the active state making sure
1065 // to respect these rules (again see Layer.h for a detailed discussion).
1066 //
1067 // There is an awkward asymmetry in the handling of the crop states in the position
1068 // states, as can be seen below. Largely this arises from position and transform
1069 // being stored in the same data structure while having different latching rules.
1070 // b/38182305
1071 //
Marissa Wall61c58622018-07-18 10:12:20 -07001072 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001073 // applyPendingStates in the presence of deferred transactions.
1074 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001075 float tx = stateToCommit->active_legacy.transform.tx();
1076 float ty = stateToCommit->active_legacy.transform.ty();
1077 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1078 stateToCommit->active_legacy.transform.set(tx, ty);
1079 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001080 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001081 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001082 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001083 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001084 }
1085
Marissa Wall61c58622018-07-18 10:12:20 -07001086 return flags;
1087}
1088
1089uint32_t Layer::doTransaction(uint32_t flags) {
1090 ATRACE_CALL();
1091
chaviw5aedec92018-10-22 10:40:38 -07001092 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001093 return flags;
1094 }
1095
1096 if (mChildrenChanged) {
1097 flags |= eVisibleRegion;
1098 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001099 }
1100
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001101 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001102 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001103 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001104 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001105 }
1106
1107 flags = doTransactionResize(flags, &c);
1108
Alec Mourib416efd2018-09-06 21:01:59 +00001109 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001110
1111 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001112 // invalidate and recompute the visible regions if needed
1113 flags |= Layer::eVisibleRegion;
1114 }
1115
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001116 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 // invalidate and recompute the visible regions if needed
1118 flags |= eVisibleRegion;
1119 this->contentDirty = true;
1120
1121 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001122 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001123 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 }
1125
Dan Stozac8145172016-04-28 16:29:06 -07001126 // If the layer is hidden, signal and clear out all local sync points so
1127 // that transactions for layers depending on this layer's frames becoming
1128 // visible are not blocked
1129 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001130 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001131 }
1132
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001133 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001134 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001135 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001136 }
1137
Mathias Agopian13127d82013-03-05 17:47:11 -08001138 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001139 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001140 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001141 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142}
1143
Pablo Ceballos05289c22016-04-14 15:49:55 -07001144void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001145 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001146}
1147
Mathias Agopian13127d82013-03-05 17:47:11 -08001148uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001149 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001150}
1151
1152uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001153 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001154}
1155
Robert Carr82364e32016-05-15 11:27:47 -07001156bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001157 if (mCurrentState.requested_legacy.transform.tx() == x &&
1158 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001159 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001160 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001161
1162 // We update the requested and active position simultaneously because
1163 // we want to apply the position portion of the transform matrix immediately,
1164 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001165 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001166 if (immediate && !mFreezeGeometryUpdates) {
1167 // Here we directly update the active state
1168 // unlike other setters, because we store it within
1169 // the transform, but use different latching rules.
1170 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001171 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001172 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001173 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001174
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001175 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001176 setTransactionFlags(eTransactionNeeded);
1177 return true;
1178}
Robert Carr82364e32016-05-15 11:27:47 -07001179
Robert Carr1f0a16a2016-10-24 16:27:39 -07001180bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1181 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1182 if (idx < 0) {
1183 return false;
1184 }
1185 if (childLayer->setLayer(z)) {
1186 mCurrentChildren.removeAt(idx);
1187 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001188 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001189 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001190 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001191}
1192
Robert Carr503c7042017-09-27 15:06:08 -07001193bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1194 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1195 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1196 if (idx < 0) {
1197 return false;
1198 }
1199 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1200 mCurrentChildren.removeAt(idx);
1201 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001202 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001203 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001204 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001205}
1206
Robert Carrae060832016-11-28 10:51:00 -08001207bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001208 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1209 mCurrentState.sequence++;
1210 mCurrentState.z = z;
1211 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001212
1213 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001214 if (mCurrentState.zOrderRelativeOf != nullptr) {
1215 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001216 if (strongRelative != nullptr) {
1217 strongRelative->removeZOrderRelative(this);
1218 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001219 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001220 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001221 setTransactionFlags(eTransactionNeeded);
1222 return true;
1223}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001224
Robert Carrdb66e622017-04-10 16:55:57 -07001225void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001226 mCurrentState.zOrderRelatives.remove(relative);
1227 mCurrentState.sequence++;
1228 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001229 setTransactionFlags(eTransactionNeeded);
1230}
1231
1232void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001233 mCurrentState.zOrderRelatives.add(relative);
1234 mCurrentState.modified = true;
1235 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001236 setTransactionFlags(eTransactionNeeded);
1237}
1238
Robert Carr503d2bd2017-12-04 15:49:47 -08001239bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001240 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1241 if (handle == nullptr) {
1242 return false;
1243 }
1244 sp<Layer> relative = handle->owner.promote();
1245 if (relative == nullptr) {
1246 return false;
1247 }
1248
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001249 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1250 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001251 return false;
1252 }
1253
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001254 mCurrentState.sequence++;
1255 mCurrentState.modified = true;
1256 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001257
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001258 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001259 if (oldZOrderRelativeOf != nullptr) {
1260 oldZOrderRelativeOf->removeZOrderRelative(this);
1261 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001262 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001263 relative->addZOrderRelative(this);
1264
1265 setTransactionFlags(eTransactionNeeded);
1266
1267 return true;
1268}
1269
Mathias Agopian13127d82013-03-05 17:47:11 -08001270bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001271 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001272 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001273 mCurrentState.requested_legacy.w = w;
1274 mCurrentState.requested_legacy.h = h;
1275 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001276 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001277
1278 // record the new size, from this point on, when the client request
1279 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001280 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 return true;
1282}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001283bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001284 if (mCurrentState.color.a == alpha) return false;
1285 mCurrentState.sequence++;
1286 mCurrentState.color.a = alpha;
1287 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001288 setTransactionFlags(eTransactionNeeded);
1289 return true;
1290}
chaviw13fdc492017-06-27 12:40:18 -07001291
Valerie Haudd0b7572019-01-29 14:59:27 -08001292bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1293 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001294 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001295 }
1296 mCurrentState.sequence++;
1297 mCurrentState.modified = true;
1298 setTransactionFlags(eTransactionNeeded);
1299
1300 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001301 // create background color layer if one does not yet exist
1302 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1303 const String8& name = mName + "BackgroundColorLayer";
1304 mCurrentState.bgColorLayer =
1305 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001306
Valerie Haudd0b7572019-01-29 14:59:27 -08001307 // add to child list
1308 addChild(mCurrentState.bgColorLayer);
1309 mFlinger->mLayersAdded = true;
1310 // set up SF to handle added color layer
1311 if (isRemovedFromCurrentState()) {
1312 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1313 }
1314 mFlinger->setTransactionFlags(eTransactionNeeded);
1315 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1316 mCurrentState.bgColorLayer->reparent(nullptr);
1317 mCurrentState.bgColorLayer = nullptr;
1318 return true;
1319 }
1320
1321 mCurrentState.bgColorLayer->setColor(color);
1322 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1323 mCurrentState.bgColorLayer->setAlpha(alpha);
1324 mCurrentState.bgColorLayer->setDataspace(dataspace);
1325
chaviw13fdc492017-06-27 12:40:18 -07001326 return true;
1327}
1328
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001329bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001330 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001331
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001332 mCurrentState.sequence++;
1333 mCurrentState.cornerRadius = cornerRadius;
1334 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001335 setTransactionFlags(eTransactionNeeded);
1336 return true;
1337}
1338
Robert Carrd4ae7f32018-06-07 16:10:57 -07001339bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1340 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001341 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001342 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1343
1344 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1345 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1346 return false;
1347 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001348 mCurrentState.sequence++;
1349 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1350 matrix.dsdy);
1351 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001352 setTransactionFlags(eTransactionNeeded);
1353 return true;
1354}
Marissa Wall61c58622018-07-18 10:12:20 -07001355
Mathias Agopian13127d82013-03-05 17:47:11 -08001356bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001357 mCurrentState.requestedTransparentRegion_legacy = transparent;
1358 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001359 setTransactionFlags(eTransactionNeeded);
1360 return true;
1361}
1362bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001363 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1364 if (mCurrentState.flags == newFlags) return false;
1365 mCurrentState.sequence++;
1366 mCurrentState.flags = newFlags;
1367 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001368 setTransactionFlags(eTransactionNeeded);
1369 return true;
1370}
Robert Carr99e27f02016-06-16 15:18:02 -07001371
Marissa Wallf58c14b2018-07-24 10:50:43 -07001372bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001373 if (mCurrentState.requestedCrop_legacy == crop) return false;
1374 mCurrentState.sequence++;
1375 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001376 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001377 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001378 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001379 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1380
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001381 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001382 setTransactionFlags(eTransactionNeeded);
1383 return true;
1384}
Robert Carr8d5227b2017-03-16 15:41:03 -07001385
Robert Carrc3574f72016-03-24 12:19:32 -07001386bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001387 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001388 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001389 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001390 return true;
1391}
1392
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001393bool Layer::setMetadata(LayerMetadata data) {
1394 bool changed = data.mMap != mCurrentState.metadata.mMap;
1395 if (!changed) return false;
1396 mCurrentState.metadata = std::move(data);
1397 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001398 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001399 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001400 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001401}
1402
Mathias Agopian13127d82013-03-05 17:47:11 -08001403bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001404 if (mCurrentState.layerStack == layerStack) return false;
1405 mCurrentState.sequence++;
1406 mCurrentState.layerStack = layerStack;
1407 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001408 setTransactionFlags(eTransactionNeeded);
1409 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001410}
1411
Robert Carr1f0a16a2016-10-24 16:27:39 -07001412uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001413 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001414 if (p == nullptr) {
1415 return getDrawingState().layerStack;
1416 }
1417 return p->getLayerStack();
1418}
1419
Marissa Wallf58c14b2018-07-24 10:50:43 -07001420void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001421 mCurrentState.barrierLayer_legacy = barrierLayer;
1422 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001423 // We don't set eTransactionNeeded, because just receiving a deferral
1424 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001425 mCurrentState.modified = true;
1426 pushPendingState();
1427 mCurrentState.barrierLayer_legacy = nullptr;
1428 mCurrentState.frameNumber_legacy = 0;
1429 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001430}
1431
Marissa Wallf58c14b2018-07-24 10:50:43 -07001432void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001433 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001434 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001435}
1436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437// ----------------------------------------------------------------------------
1438// pageflip handling...
1439// ----------------------------------------------------------------------------
1440
Robert Carr1f0a16a2016-10-24 16:27:39 -07001441bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001442 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001443 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001444 if (parent != nullptr && parent->isHiddenByPolicy()) {
1445 return true;
1446 }
1447 return s.flags & layer_state_t::eLayerHidden;
1448}
1449
David Sodman41fdfc92017-11-06 16:09:56 -08001450uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001451 // TODO: should we do something special if mSecure is set?
1452 if (mProtectedByApp) {
1453 // need a hardware-protected path to external video sink
1454 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001455 }
Riley Andrews03414a12014-07-01 14:22:59 -07001456 if (mPotentialCursor) {
1457 usage |= GraphicBuffer::USAGE_CURSOR;
1458 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001459 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001460 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001461}
1462
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001463void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001464 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001465 // Disable setting transform hint if the debug flag is set or if the
1466 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1467 // in one orientation.
1468 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001469 // The transform hint is used to improve performance, but we can
1470 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001471 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001472 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001473 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001474 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001475 orientation = 0;
1476 }
1477 }
David Sodmaneb085e02017-10-05 18:49:04 -07001478 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001479}
1480
Mathias Agopian13127d82013-03-05 17:47:11 -08001481// ----------------------------------------------------------------------------
1482// debugging
1483// ----------------------------------------------------------------------------
1484
Marissa Wall61c58622018-07-18 10:12:20 -07001485// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001486LayerDebugInfo Layer::getLayerDebugInfo() const {
1487 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001488 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001489 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001490 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001491 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001492 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001493 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001494 info.mVisibleRegion = visibleRegion;
1495 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001496 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001497 info.mX = ds.active_legacy.transform.tx();
1498 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001499 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001500 info.mWidth = ds.active_legacy.w;
1501 info.mHeight = ds.active_legacy.h;
1502 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001503 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001504 info.mFlags = ds.flags;
1505 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001506 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001507 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1508 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1509 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1510 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001511 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001512 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001513 if (buffer != 0) {
1514 info.mActiveBufferWidth = buffer->getWidth();
1515 info.mActiveBufferHeight = buffer->getHeight();
1516 info.mActiveBufferStride = buffer->getStride();
1517 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001518 } else {
1519 info.mActiveBufferWidth = 0;
1520 info.mActiveBufferHeight = 0;
1521 info.mActiveBufferStride = 0;
1522 info.mActiveBufferFormat = 0;
1523 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001524 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001525 info.mNumQueuedFrames = getQueuedFrameCount();
1526 info.mRefreshPending = isBufferLatched();
1527 info.mIsOpaque = isOpaque(ds);
1528 info.mContentDirty = contentDirty;
1529 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001530}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001531
Yiwei Zhang5434a782018-12-05 18:06:32 -08001532void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001533 result.append("-------------------------------");
1534 result.append("-------------------------------");
1535 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001536 result.append(" Layer name\n");
1537 result.append(" Z | ");
1538 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001539 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001540 result.append(" Disp Frame (LTRB) | ");
1541 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001542 result.append("-------------------------------");
1543 result.append("-------------------------------");
1544 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001545}
1546
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001547void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1548 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1549 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001550 return;
1551 }
1552
Yiwei Zhang5434a782018-12-05 18:06:32 -08001553 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001554 if (mName.length() > 77) {
1555 std::string shortened;
1556 shortened.append(mName.string(), 36);
1557 shortened.append("[...]");
1558 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001559 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001560 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001561 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001562 }
1563
Yiwei Zhang5434a782018-12-05 18:06:32 -08001564 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001565
Alec Mourib416efd2018-09-06 21:01:59 +00001566 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001567 const auto& compositionState = outputLayer->getState();
1568
Chia-I Wu1e043612018-03-01 09:45:09 -08001569 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001570 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001571 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001572 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001573 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001574 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1575 StringAppendF(&result, "%10s | ",
1576 toString(getCompositionLayer() ? compositionState.bufferTransform
1577 : static_cast<Hwc2::Transform>(0))
1578 .c_str());
1579 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001580 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001581 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001582 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1583 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001584
David Sodman7e4ae112018-02-09 15:02:28 -08001585 result.append("- - - - - - - - - - - - - - - -\n");
1586
1587 std::string compositionInfoStr;
1588 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001589 result.append(compositionInfoStr);
David Sodman7e4ae112018-02-09 15:02:28 -08001590
Yichi Chen6ca35192018-05-29 12:20:43 +08001591 result.append("- - - - - - - - - - - - - - - -");
1592 result.append("- - - - - - - - - - - - - - - -");
1593 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001594}
Dan Stozae22aec72016-08-01 13:20:59 -07001595
Yiwei Zhang5434a782018-12-05 18:06:32 -08001596void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001597 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001598}
1599
Svetoslavd85084b2014-03-20 10:28:31 -07001600void Layer::clearFrameStats() {
1601 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001602}
1603
Jamie Gennis6547ff42013-07-16 20:12:42 -07001604void Layer::logFrameStats() {
1605 mFrameTracker.logAndResetStats(mName);
1606}
1607
Svetoslavd85084b2014-03-20 10:28:31 -07001608void Layer::getFrameStats(FrameStats* outStats) const {
1609 mFrameTracker.getStats(outStats);
1610}
1611
Yiwei Zhang5434a782018-12-05 18:06:32 -08001612void Layer::dumpFrameEvents(std::string& result) {
1613 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001614 Mutex::Autolock lock(mFrameEventHistoryMutex);
1615 mFrameEventHistory.checkFencesForCompletion();
1616 mFrameEventHistory.dump(result);
1617}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001618
Brian Anderson5ea5e592016-12-01 16:54:33 -08001619void Layer::onDisconnect() {
1620 Mutex::Autolock lock(mFrameEventHistoryMutex);
1621 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001622 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001623}
1624
Brian Anderson3890c392016-07-25 12:48:08 -07001625void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001626 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001627 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001628 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1629 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001630 }
1631
Brian Andersond6927fb2016-07-23 23:37:30 -07001632 Mutex::Autolock lock(mFrameEventHistoryMutex);
1633 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001634 // If there are any unsignaled fences in the aquire timeline at this
1635 // point, the previously queued frame hasn't been latched yet. Go ahead
1636 // and try to get the signal time here so the syscall is taken out of
1637 // the main thread's critical path.
1638 mAcquireTimeline.updateSignalTimes();
1639 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001640 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001641 mFrameEventHistory.addQueue(*newTimestamps);
1642 }
1643
Brian Anderson3890c392016-07-25 12:48:08 -07001644 if (outDelta) {
1645 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001646 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001647}
Dan Stozae77c7662016-05-13 11:37:28 -07001648
Chia-I Wu98f1c102017-05-30 14:54:08 -07001649size_t Layer::getChildrenCount() const {
1650 size_t count = 0;
1651 for (const sp<Layer>& child : mCurrentChildren) {
1652 count += 1 + child->getChildrenCount();
1653 }
1654 return count;
1655}
1656
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001658 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001659 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001660
Robert Carr1f0a16a2016-10-24 16:27:39 -07001661 mCurrentChildren.add(layer);
1662 layer->setParent(this);
1663}
1664
1665ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001666 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001667 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001668
Robert Carr1323c952019-01-28 18:13:27 -08001669 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001670 return mCurrentChildren.remove(layer);
1671}
1672
Robert Carr1db73f62016-12-21 12:58:51 -08001673bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1674 sp<Handle> handle = nullptr;
1675 sp<Layer> newParent = nullptr;
1676 if (newParentHandle == nullptr) {
1677 return false;
1678 }
1679 handle = static_cast<Handle*>(newParentHandle.get());
1680 newParent = handle->owner.promote();
1681 if (newParent == nullptr) {
1682 ALOGE("Unable to promote Layer handle");
1683 return false;
1684 }
1685
chaviw5aedec92018-10-22 10:40:38 -07001686 if (attachChildren()) {
1687 setTransactionFlags(eTransactionNeeded);
1688 }
Robert Carr1db73f62016-12-21 12:58:51 -08001689 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001690 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001691 }
1692 mCurrentChildren.clear();
1693
1694 return true;
1695}
1696
Robert Carr15eae092018-03-23 13:43:53 -07001697void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001698 for (const sp<Layer>& child : mDrawingChildren) {
1699 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001700 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001701 }
1702}
1703
chaviwf1961f72017-09-18 16:41:07 -07001704bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001705 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001706
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001707 // While layers are detached, we allow most operations
1708 // and simply halt performing the actual transaction. However
1709 // for reparent != null we would enter the mRemovedFromCurrentState
1710 // state, regardless of whether doTransaction was called, and
1711 // so we need to prevent the update here.
1712 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001713 return false;
1714 }
1715
Robert Carr54cf5b12019-01-25 14:02:28 -08001716 sp<Layer> newParent;
1717 if (newParentHandle != nullptr) {
1718 auto handle = static_cast<Handle*>(newParentHandle.get());
1719 newParent = handle->owner.promote();
1720 if (newParent == nullptr) {
1721 ALOGE("Unable to promote Layer handle");
1722 return false;
1723 }
1724 if (newParent == this) {
1725 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1726 return false;
1727 }
1728 }
1729
chaviwf1961f72017-09-18 16:41:07 -07001730 sp<Layer> parent = getParent();
1731 if (parent != nullptr) {
1732 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001733 }
1734
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001735 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001736 newParent->addChild(this);
1737 if (!newParent->isRemovedFromCurrentState()) {
1738 addToCurrentState();
1739 } else {
1740 onRemovedFromCurrentState();
1741 }
1742
1743 if (mLayerDetached) {
1744 mLayerDetached = false;
1745 callSetTransactionFlags = true;
1746 }
1747 } else {
1748 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001749 }
1750
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001751 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001752 setTransactionFlags(eTransactionNeeded);
1753 }
chaviw06178942017-07-27 10:25:59 -07001754 return true;
1755}
1756
Robert Carr9524cb32017-02-13 11:32:32 -08001757bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001758 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001759 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001760 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001761 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001762 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001763 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001764 }
Robert Carr7f619b22017-11-06 12:56:35 -08001765 }
Robert Carr9524cb32017-02-13 11:32:32 -08001766
1767 return true;
1768}
1769
chaviw5aedec92018-10-22 10:40:38 -07001770bool Layer::attachChildren() {
1771 bool changed = false;
1772 for (const sp<Layer>& child : mCurrentChildren) {
1773 sp<Client> parentClient = mClientRef.promote();
1774 sp<Client> client(child->mClientRef.promote());
1775 if (client != nullptr && parentClient != client) {
1776 if (child->mLayerDetached) {
1777 child->mLayerDetached = false;
1778 changed = true;
1779 }
1780 changed |= child->attachChildren();
1781 }
1782 }
1783
1784 return changed;
1785}
1786
Peiyong Lind3788632018-09-18 16:01:31 -07001787bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001788 static const mat4 identityMatrix = mat4();
1789
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001790 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001791 return false;
1792 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001793 ++mCurrentState.sequence;
1794 mCurrentState.colorTransform = matrix;
1795 mCurrentState.hasColorTransform = matrix != identityMatrix;
1796 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001797 setTransactionFlags(eTransactionNeeded);
1798 return true;
1799}
1800
chaviwf66724d2018-11-28 16:35:21 -08001801mat4 Layer::getColorTransform() const {
1802 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1803 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1804 colorTransform = parent->getColorTransform() * colorTransform;
1805 }
1806 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001807}
1808
1809bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001810 bool hasColorTransform = getDrawingState().hasColorTransform;
1811 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1812 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1813 }
1814 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001815}
1816
Chia-I Wu11481472018-05-04 10:43:19 -07001817bool Layer::isLegacyDataSpace() const {
1818 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001819 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001820 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001821}
1822
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001824 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001825}
1826
1827void Layer::clearSyncPoints() {
1828 for (const auto& child : mCurrentChildren) {
1829 child->clearSyncPoints();
1830 }
1831
1832 Mutex::Autolock lock(mLocalSyncPointMutex);
1833 for (auto& point : mLocalSyncPoints) {
1834 point->setFrameAvailable();
1835 }
1836 mLocalSyncPoints.clear();
1837}
1838
1839int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001840 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841}
1842
Robert Carr29abff82017-12-04 13:51:20 -08001843bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1844 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001845 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001846 return state.zOrderRelativeOf != nullptr;
1847}
1848
David Sodman41fdfc92017-11-06 16:09:56 -08001849__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001850 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001851 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1852 "makeTraversalList received invalid stateSet");
1853 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1854 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001855 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001856
Robert Carr29abff82017-12-04 13:51:20 -08001857 if (state.zOrderRelatives.size() == 0) {
1858 *outSkipRelativeZUsers = true;
1859 return children;
1860 }
1861
chaviwfd462612018-05-31 16:11:27 -07001862 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001863 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001864 sp<Layer> strongRelative = weakRelative.promote();
1865 if (strongRelative != nullptr) {
1866 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001867 }
1868 }
1869
Dan Stoza412903f2017-04-27 13:42:17 -07001870 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001871 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001872 if (childState.zOrderRelativeOf != nullptr) {
1873 continue;
1874 }
Robert Carrdb66e622017-04-10 16:55:57 -07001875 traverse.add(child);
1876 }
1877
1878 return traverse;
1879}
1880
Robert Carr1f0a16a2016-10-24 16:27:39 -07001881/**
Robert Carrdb66e622017-04-10 16:55:57 -07001882 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001883 */
Dan Stoza412903f2017-04-27 13:42:17 -07001884void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001885 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1886 // produce a new list for traversing, including our relatives, and not including our children
1887 // who are relatives of another surface. In the case that there are no relative Z,
1888 // makeTraversalList returns our children directly to avoid significant overhead.
1889 // However in this case we need to take the responsibility for filtering children which
1890 // are relatives of another surface here.
1891 bool skipRelativeZUsers = false;
1892 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001893
Robert Carr1f0a16a2016-10-24 16:27:39 -07001894 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001895 for (; i < list.size(); i++) {
1896 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001897 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1898 continue;
1899 }
1900
Robert Carrdb66e622017-04-10 16:55:57 -07001901 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001902 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001903 }
Dan Stoza412903f2017-04-27 13:42:17 -07001904 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001905 }
Robert Carr29abff82017-12-04 13:51:20 -08001906
Dan Stoza412903f2017-04-27 13:42:17 -07001907 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001908 for (; i < list.size(); i++) {
1909 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001910
Robert Carr29abff82017-12-04 13:51:20 -08001911 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1912 continue;
1913 }
Dan Stoza412903f2017-04-27 13:42:17 -07001914 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001915 }
1916}
1917
1918/**
Robert Carrdb66e622017-04-10 16:55:57 -07001919 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001920 */
Dan Stoza412903f2017-04-27 13:42:17 -07001921void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1922 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001923 // See traverseInZOrder for documentation.
1924 bool skipRelativeZUsers = false;
1925 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001926
Robert Carr1f0a16a2016-10-24 16:27:39 -07001927 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001928 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001929 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001930
1931 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1932 continue;
1933 }
1934
Robert Carrdb66e622017-04-10 16:55:57 -07001935 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001936 break;
1937 }
Dan Stoza412903f2017-04-27 13:42:17 -07001938 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001939 }
Dan Stoza412903f2017-04-27 13:42:17 -07001940 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001941 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001942 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001943
1944 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1945 continue;
1946 }
1947
Dan Stoza412903f2017-04-27 13:42:17 -07001948 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001949 }
1950}
1951
chaviw4b129c22018-04-09 16:19:43 -07001952LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1953 const std::vector<Layer*>& layersInTree) {
1954 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1955 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001956 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1957 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001958 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001959
chaviwfd462612018-05-31 16:11:27 -07001960 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001961 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1962 sp<Layer> strongRelative = weakRelative.promote();
1963 // Only add relative layers that are also descendents of the top most parent of the tree.
1964 // If a relative layer is not a descendent, then it should be ignored.
1965 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1966 traverse.add(strongRelative);
1967 }
1968 }
1969
1970 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001971 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001972 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1973 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1974 // the child here since it won't be added later as a relative.
1975 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1976 childState.zOrderRelativeOf.promote().get())) {
1977 continue;
1978 }
1979 traverse.add(child);
1980 }
1981
1982 return traverse;
1983}
1984
1985void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1986 LayerVector::StateSet stateSet,
1987 const LayerVector::Visitor& visitor) {
1988 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001989
1990 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001991 for (; i < list.size(); i++) {
1992 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001993 if (relative->getZ() >= 0) {
1994 break;
1995 }
chaviw4b129c22018-04-09 16:19:43 -07001996 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001997 }
chaviw4b129c22018-04-09 16:19:43 -07001998
chaviwa76b2712017-09-20 12:02:26 -07001999 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07002000 for (; i < list.size(); i++) {
2001 const auto& relative = list[i];
2002 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002003 }
2004}
2005
chaviw4b129c22018-04-09 16:19:43 -07002006std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
2007 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2008 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2009
2010 std::vector<Layer*> layersInTree = {this};
2011 for (size_t i = 0; i < children.size(); i++) {
2012 const auto& child = children[i];
2013 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
2014 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
2015 }
2016
2017 return layersInTree;
2018}
2019
2020void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2021 const LayerVector::Visitor& visitor) {
2022 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
2023 std::sort(layersInTree.begin(), layersInTree.end());
2024 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2025}
2026
Peiyong Linefefaac2018-08-17 12:27:51 -07002027ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002028 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002029}
2030
chaviw13fdc492017-06-27 12:40:18 -07002031half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002032 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002033
chaviw13fdc492017-06-27 12:40:18 -07002034 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2035 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002036}
Robert Carr6452f122017-03-21 10:41:29 -07002037
chaviw13fdc492017-06-27 12:40:18 -07002038half4 Layer::getColor() const {
2039 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002040 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002041}
Robert Carr6452f122017-03-21 10:41:29 -07002042
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002043Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2044 const auto& p = mDrawingParent.promote();
2045 if (p != nullptr) {
2046 RoundedCornerState parentState = p->getRoundedCornerState();
2047 if (parentState.radius > 0) {
2048 ui::Transform t = getActiveTransform(getDrawingState());
2049 t = t.inverse();
2050 parentState.cropRect = t.transform(parentState.cropRect);
2051 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2052 // but a transform matrix can define horizontal and vertical scales.
2053 // Let's take the average between both of them and pass into the shader, practically we
2054 // never do this type of transformation on windows anyway.
2055 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2056 return parentState;
2057 }
2058 }
2059 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002060 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002061}
2062
Robert Carr1f0a16a2016-10-24 16:27:39 -07002063void Layer::commitChildList() {
2064 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2065 const auto& child = mCurrentChildren[i];
2066 child->commitChildList();
2067 }
2068 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002069 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002070}
2071
Robert Carr720e5062018-07-30 17:45:14 -07002072void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002073 mCurrentState.inputInfo = info;
2074 mCurrentState.modified = true;
2075 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002076 setTransactionFlags(eTransactionNeeded);
2077}
2078
chaviw1d044282017-09-27 12:19:28 -07002079void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2080 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2081 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002082 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002083
Peiyong Linefefaac2018-08-17 12:27:51 -07002084 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002085 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002086
2087 layerInfo->set_id(sequence);
2088 layerInfo->set_name(getName().c_str());
2089 layerInfo->set_type(String8(getTypeId()));
2090
2091 for (const auto& child : children) {
2092 layerInfo->add_children(child->sequence);
2093 }
2094
2095 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2096 sp<Layer> strongRelative = weakRelative.promote();
2097 if (strongRelative != nullptr) {
2098 layerInfo->add_relatives(strongRelative->sequence);
2099 }
2100 }
2101
Marissa Wallf58c14b2018-07-24 10:50:43 -07002102 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002103 layerInfo->mutable_transparent_region());
2104 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2105 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2106
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002107 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002108 layerInfo->set_z(state.z);
2109
2110 PositionProto* position = layerInfo->mutable_position();
2111 position->set_x(transform.tx());
2112 position->set_y(transform.ty());
2113
2114 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2115 requestedPosition->set_x(requestedTransform.tx());
2116 requestedPosition->set_y(requestedTransform.ty());
2117
2118 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002119 size->set_w(state.active_legacy.w);
2120 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002121
Marissa Wallf58c14b2018-07-24 10:50:43 -07002122 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002123 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002124
2125 layerInfo->set_is_opaque(isOpaque(state));
2126 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002127
2128 // XXX (b/79210409) mCurrentDataSpace is not protected
2129 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2130
chaviw1d044282017-09-27 12:19:28 -07002131 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2132 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2133 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2134 layerInfo->set_flags(state.flags);
2135
2136 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2137 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2138
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002139 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002140 if (parent != nullptr) {
2141 layerInfo->set_parent(parent->sequence);
2142 }
2143
2144 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2145 if (zOrderRelativeOf != nullptr) {
2146 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2147 }
2148
Chia-I Wu01591c92018-05-22 12:03:00 -07002149 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002150 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002151 if (buffer != nullptr) {
2152 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002153 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002154 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002155 }
2156
2157 layerInfo->set_queued_frames(getQueuedFrameCount());
2158 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002159 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002160 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002161
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002162 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002163 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002164 if (barrierLayer != nullptr) {
2165 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2166 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002167 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002168 }
2169 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002170
2171 auto protoMap = layerInfo->mutable_metadata();
2172 for (const auto& entry : state.metadata.mMap) {
2173 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2174 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002175 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
2176 LayerProtoHelper::writeToProto(mSourceBounds, layerInfo->mutable_source_bounds());
2177 LayerProtoHelper::writeToProto(mScreenBounds, layerInfo->mutable_screen_bounds());
2178 LayerProtoHelper::writeToProto(mBounds, layerInfo->mutable_bounds());
chaviw1d044282017-09-27 12:19:28 -07002179}
2180
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002181void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2182 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2183 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002184 return;
2185 }
2186
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002187 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2188
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002189 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002190
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002191 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002192 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2193
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002194 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002195 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2196
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002197 const int32_t transform =
2198 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002199 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002200
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002201 const int32_t compositionType =
2202 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2203 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002204 layerInfo->set_hwc_composition_type(compositionType);
2205
2206 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2207 static_cast<BufferLayer*>(this)->isProtected()) {
2208 layerInfo->set_is_protected(true);
2209 } else {
2210 layerInfo->set_is_protected(false);
2211 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002212}
2213
Robert Carr2e102c92018-10-23 12:11:15 -07002214bool Layer::isRemovedFromCurrentState() const {
2215 return mRemovedFromCurrentState;
2216}
2217
Arthur Hungd20b2702019-01-14 18:16:16 +08002218InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002219 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002220
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002221 if (info.displayId == ADISPLAY_ID_NONE) {
2222 info.displayId = mDrawingState.layerStack;
2223 }
2224
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002225 ui::Transform t = getTransform();
2226 const float xScale = t.sx();
2227 const float yScale = t.sy();
2228 if (xScale != 1.0f || yScale != 1.0f) {
2229 info.windowXScale *= 1.0f / xScale;
2230 info.windowYScale *= 1.0f / yScale;
2231 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002232 }
Robert Carre07e1032018-11-26 12:55:53 -08002233
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002234 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002235 // If this is a portal window, set the touchableRegion to the layerBounds.
2236 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2237 ? getBufferSize(getDrawingState())
2238 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002239 if (!layerBounds.isValid()) {
2240 layerBounds = getCroppedBufferSize(getDrawingState());
2241 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002242 layerBounds = t.transform(layerBounds);
2243 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2244
Arthur Hungd20b2702019-01-14 18:16:16 +08002245 // Input coordinate should match the layer bounds.
2246 info.frameLeft = layerBounds.left;
2247 info.frameTop = layerBounds.top;
2248 info.frameRight = layerBounds.right;
2249 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002250
2251 // Position the touchable region relative to frame screen location and restrict it to frame
2252 // bounds.
2253 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002254 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002255 return info;
2256}
2257
2258bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002259 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002260}
2261
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002262std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2263 return nullptr;
2264}
2265
chaviw3e727cd2019-01-31 13:41:05 -08002266bool Layer::canReceiveInput() const {
2267 return isVisible();
2268}
2269
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002270compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2271 const sp<const DisplayDevice>& display) const {
2272 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2273}
2274
Mathias Agopian13127d82013-03-05 17:47:11 -08002275// ---------------------------------------------------------------------------
2276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002277}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002278
2279#if defined(__gl_h_)
2280#error "don't include gl/gl.h in this file"
2281#endif
2282
2283#if defined(__gl2_h_)
2284#error "don't include gl2/gl2.h in this file"
2285#endif