blob: 5734ccf38f93ad54f822432dbe1c5606079a52d8 [file] [log] [blame]
Vishnu Nairdc4d31b2022-11-17 03:20:58 +00001/*
2 * Copyright 2022 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 */
Vishnu Naira02943f2023-06-03 13:44:46 -070016// #define LOG_NDEBUG 0
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000017
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19#undef LOG_TAG
Vishnu Naira02943f2023-06-03 13:44:46 -070020#define LOG_TAG "SurfaceFlinger"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000021
Vishnu Nairbe0ad902024-06-27 23:38:43 +000022#include <common/trace.h>
Vishnu Naircfb2d252023-01-19 04:44:02 +000023#include <log/log.h>
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000024#include <private/android_filesystem_config.h>
25#include <sys/types.h>
26
Rachel Leece6e0042023-06-27 11:22:54 -070027#include <scheduler/Fps.h>
28
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000029#include "Layer.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000030#include "LayerCreationArgs.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000031#include "LayerLog.h"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000032#include "RequestedLayerState.h"
33
34namespace android::surfaceflinger::frontend {
35using ftl::Flags;
36using namespace ftl::flag_operators;
37
38namespace {
Vishnu Naira9c43762023-01-27 19:10:25 +000039std::string layerIdsToString(const std::vector<uint32_t>& layerIds) {
40 std::stringstream stream;
41 stream << "{";
42 for (auto layerId : layerIds) {
43 stream << layerId << ",";
44 }
45 stream << "}";
46 return stream.str();
47}
48
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000049} // namespace
50
51RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args)
52 : id(args.sequence),
Vishnu Naircfb2d252023-01-19 04:44:02 +000053 name(args.name + "#" + std::to_string(args.sequence)),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000054 canBeRoot(args.addToRoot),
55 layerCreationFlags(args.flags),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000056 ownerUid(args.ownerUid),
Vishnu Nair1391de22023-03-05 19:56:14 -080057 ownerPid(args.ownerPid),
58 parentId(args.parentId),
59 layerIdToMirror(args.layerIdToMirror) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000060 layerId = static_cast<int32_t>(args.sequence);
61 changes |= RequestedLayerState::Changes::Created;
62 metadata.merge(args.metadata);
63 changes |= RequestedLayerState::Changes::Metadata;
64 handleAlive = true;
Wenhui Yangab89d812024-09-11 23:21:38 +000065 // TODO: b/305254099 remove once we don't pass invisible windows to input
66 windowInfoHandle = nullptr;
Vishnu Nair1391de22023-03-05 19:56:14 -080067 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000068 canBeRoot = false;
69 }
Vishnu Naira9c43762023-01-27 19:10:25 +000070 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
71 changes |= RequestedLayerState::Changes::Mirror;
72 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
73 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000074 changes |= RequestedLayerState::Changes::Mirror;
75 }
76
77 flags = 0;
78 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
79 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
80 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
81 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
82 flags |= layer_state_t::eLayerSkipScreenshot;
83 }
84 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
85 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
86 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
87 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
88 // Set an invalid color so there is no color fill.
89 // (b/259981098) use an explicit flag instead of relying on invalid values.
90 color.r = -1.0_hf;
91 color.g = -1.0_hf;
92 color.b = -1.0_hf;
93 } else {
94 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
95 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000096 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000097 color.a = 1.0f;
98
99 crop.makeInvalid();
100 z = 0;
101 layerStack = ui::DEFAULT_LAYER_STACK;
102 transformToDisplayInverse = false;
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000103 desiredHdrSdrRatio = -1.f;
Sally Qi963049b2023-03-23 14:06:21 -0700104 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000105 dataspaceRequested = false;
106 hdrMetadata.validTypes = 0;
107 surfaceDamageRegion = Region::INVALID_REGION;
108 cornerRadius = 0.0f;
109 backgroundBlurRadius = 0;
110 api = -1;
111 hasColorTransform = false;
112 bufferTransform = 0;
113 requestedTransform.reset();
114 bufferData = std::make_shared<BufferData>();
115 bufferData->frameNumber = 0;
116 bufferData->acquireFence = sp<Fence>::make(-1);
117 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
118 colorSpaceAgnostic = false;
119 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
120 shadowRadius = 0.f;
121 fixedTransformHint = ui::Transform::ROT_INVALID;
122 destinationFrame.makeInvalid();
Vishnu Nair9e0017e2024-05-22 19:02:44 +0000123 trustedOverlay = gui::TrustedOverlay::UNSET;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000124 dropInputMode = gui::DropInputMode::NONE;
125 dimmingEnabled = true;
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700126 defaultFrameRateCompatibility = static_cast<int8_t>(scheduler::FrameRateCompatibility::Default);
Rachel Leece6e0042023-06-27 11:22:54 -0700127 frameRateCategory = static_cast<int8_t>(FrameRateCategory::Default);
Rachel Lee67afbea2023-09-28 15:35:07 -0700128 frameRateCategorySmoothSwitchOnly = false;
Rachel Lee58cc90d2023-09-05 18:50:20 -0700129 frameRateSelectionStrategy =
Rachel Lee70f7b692023-11-22 11:24:02 -0800130 static_cast<int8_t>(scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000131 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000132 gameMode = gui::GameMode::Unsupported;
133 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000134 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000135
136 if (name.length() > 77) {
137 std::string shortened;
138 shortened.append(name, 0, 36);
139 shortened.append("[...]");
140 shortened.append(name, name.length() - 36);
141 debugName = std::move(shortened);
142 } else {
143 debugName = name;
144 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000145}
146
147void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000148 const uint32_t oldFlags = flags;
149 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700150 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700151 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700152 const ui::Size oldBufferSize = hadBuffer
153 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
154 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700155 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700156 const bool oldBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
157 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
Vishnu Nair0808ae62023-08-07 21:42:42 -0700158
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700159 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000160 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700161 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000162 uint64_t clientChanges = what | layer_state_t::diff(clientState);
163 layer_state_t::merge(clientState);
164 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700165 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000166
167 if (clientState.what & layer_state_t::eFlagsChanged) {
Patrick Williamsd8edec02024-03-11 18:14:48 -0500168 if ((oldFlags ^ flags) &
169 (layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque |
170 layer_state_t::eLayerSecure)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000171 changes |= RequestedLayerState::Changes::Visibility |
172 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000173 }
174 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
175 changes |= RequestedLayerState::Changes::Geometry;
176 }
Vishnu Nair59a6be32024-01-29 10:26:21 -0800177 if ((oldFlags ^ flags) & layer_state_t::eCanOccludePresentation) {
178 changes |= RequestedLayerState::Changes::Input;
179 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000180 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700181
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700182 if (clientState.what & layer_state_t::eBufferChanged) {
183 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700184 const bool hasBuffer = externalTexture != nullptr;
185 if (hasBuffer || hasBuffer != hadBuffer) {
186 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700187 const ui::Size newBufferSize = hasBuffer
188 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
189 : ui::Size();
190 if (oldBufferSize != newBufferSize) {
191 changes |= RequestedLayerState::Changes::BufferSize;
192 changes |= RequestedLayerState::Changes::Geometry;
193 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700194 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
195 if (oldUsageFlags != usageFlags) {
196 changes |= RequestedLayerState::Changes::BufferUsageFlags;
197 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700198 }
199
200 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000201 changes |= RequestedLayerState::Changes::Geometry |
202 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000203 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
204 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700205
206 if (hasBuffer) {
207 const bool frameNumberChanged =
208 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
209 const uint64_t frameNumber =
210 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
211 bufferData->frameNumber = frameNumber;
212
213 if ((barrierProducerId > bufferData->producerId) ||
214 ((barrierProducerId == bufferData->producerId) &&
215 (barrierFrameNumber > bufferData->frameNumber))) {
216 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
217 " -> producedId=%d frameNumber=%" PRIu64,
Patrick Williams0db92f12023-10-18 12:02:06 -0500218 getDebugString().c_str(), barrierProducerId, barrierFrameNumber,
Vishnu Naird1f74982023-06-15 20:16:51 -0700219 bufferData->producerId, frameNumber);
220 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
221 /*overwrite=*/false);
222 }
223
224 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
225 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
226 }
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700227
228 const bool newBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
229 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
230 if (newBufferFormatOpaque != oldBufferFormatOpaque) {
231 changes |= RequestedLayerState::Changes::Visibility |
232 RequestedLayerState::Changes::VisibleRegion;
233 }
Vishnu Nair63221212023-04-06 15:17:37 -0700234 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700235
Vishnu Nair63221212023-04-06 15:17:37 -0700236 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
237 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700238 const bool hasSideStream = sidebandStream != nullptr;
239 if (hasSideStream != hadSideStream) {
240 changes |= RequestedLayerState::Changes::Geometry |
241 RequestedLayerState::Changes::VisibleRegion |
242 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
243 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000244 }
245 if (what & (layer_state_t::eAlphaChanged)) {
246 if (oldAlpha == 0 || color.a == 0) {
Vishnu Nair93e26b92023-10-16 21:36:51 -0700247 changes |= RequestedLayerState::Changes::Visibility;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000248 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000249 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700250
251 if (hadSomethingToDraw != hasSomethingToDraw()) {
252 changes |= RequestedLayerState::Changes::Visibility |
253 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000254 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000255 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
256 changes |= RequestedLayerState::Changes::Hierarchy;
257 if (clientChanges & layer_state_t::CONTENT_CHANGES)
258 changes |= RequestedLayerState::Changes::Content;
259 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
260 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000261 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
262 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000263 if (clientChanges & layer_state_t::INPUT_CHANGES)
264 changes |= RequestedLayerState::Changes::Input;
265 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
266 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000267 if (clientState.what & layer_state_t::eColorTransformChanged) {
268 static const mat4 identityMatrix = mat4();
269 hasColorTransform = colorTransform != identityMatrix;
270 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000271 if (clientState.what &
272 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
273 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000274 changes |= RequestedLayerState::Changes::Z;
275 }
276 if (clientState.what & layer_state_t::eReparent) {
277 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800278 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000279 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000280 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
281 // but thats the existing logic and until we make this behavior more explicit, we need
282 // to maintain this logic.
283 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000284 }
285 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
286 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800287 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000288 isRelativeOf = true;
289 relativeLayerSurfaceControl = nullptr;
290 }
291 if ((clientState.what & layer_state_t::eLayerChanged ||
292 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
293 isRelativeOf) {
294 // clear out relz data
295 relativeParentId = UNASSIGNED_LAYER_ID;
296 isRelativeOf = false;
297 changes |= RequestedLayerState::Changes::RelativeParent;
298 }
299 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
300 // provide a hint that we are are now a direct child and not a relative child.
301 changes |= RequestedLayerState::Changes::RelativeParent;
302 }
303 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800304 touchCropId = resolvedComposerState.touchCropId;
305 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000306 }
307 if (clientState.what & layer_state_t::eStretchChanged) {
308 stretchEffect.sanitize();
309 }
310
311 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
312 // TODO(b/238781169) handle callbacks
313 }
314
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000315 if (clientState.what & layer_state_t::ePositionChanged) {
316 requestedTransform.set(x, y);
317 }
318
319 if (clientState.what & layer_state_t::eMatrixChanged) {
320 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
321 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000322 if (clientState.what & layer_state_t::eMetadataChanged) {
323 const int32_t requestedGameMode =
324 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
325 if (requestedGameMode != -1) {
326 // The transaction will be received on the Task layer and needs to be applied to all
327 // child layers.
328 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
329 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700330 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000331 }
332 }
Nergi Rahardi27613c32024-05-23 06:57:02 +0000333 changes |= RequestedLayerState::Changes::Metadata;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000334 }
335 if (clientState.what & layer_state_t::eFrameRateChanged) {
336 const auto compatibility =
337 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
338 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
339 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700340 requestedFrameRate.vote =
341 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
342 compatibility, strategy);
343 changes |= RequestedLayerState::Changes::FrameRate;
344 }
345 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
346 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
347 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000348 changes |= RequestedLayerState::Changes::FrameRate;
349 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000350}
351
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000352ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
353 uint32_t bufferWidth = externalTexture->getWidth();
354 uint32_t bufferHeight = externalTexture->getHeight();
355 // Undo any transformations on the buffer.
356 if (bufferTransform & ui::Transform::ROT_90) {
357 std::swap(bufferWidth, bufferHeight);
358 }
359 if (transformToDisplayInverse) {
360 if (displayRotationFlags & ui::Transform::ROT_90) {
361 std::swap(bufferWidth, bufferHeight);
362 }
363 }
364 return {bufferWidth, bufferHeight};
365}
366
367ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000368 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
369 // If destination frame is not set, use the requested transform set via
370 // Transaction::setPosition and Transaction::setMatrix.
371 return requestedTransform;
372 }
373
374 Rect destRect = destinationFrame;
375 int32_t destW = destRect.width();
376 int32_t destH = destRect.height();
377 if (destRect.left < 0) {
378 destRect.left = 0;
379 destRect.right = destW;
380 }
381 if (destRect.top < 0) {
382 destRect.top = 0;
383 destRect.bottom = destH;
384 }
385
386 if (!externalTexture) {
387 ui::Transform transform;
388 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
389 return transform;
390 }
391
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000392 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000393
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000394 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
395 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000396 ui::Transform transform;
397 transform.set(sx, 0, 0, sy);
398 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
399 return transform;
400}
401
402std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000403 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700404 debug << "RequestedLayerState{" << name;
405 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
406 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
407 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
408 if (!handleAlive) debug << " !handle";
409 if (z != 0) debug << " z=" << z;
410 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Patrick Williams0db92f12023-10-18 12:02:06 -0500411 debug << "}";
Vishnu Nair80a5a702023-02-11 01:21:51 +0000412 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000413}
414
Ady Abraham4cb525b2023-09-22 17:34:45 -0700415std::ostream& operator<<(std::ostream& out, const scheduler::LayerInfo::FrameRate& obj) {
416 out << obj.vote.rate;
417 out << " " << ftl::enum_string_full(obj.vote.type);
418 out << " " << ftl::enum_string_full(obj.category);
419 return out;
420}
421
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000422std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
423 out << obj.debugName;
424 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
425 if (!obj.handleAlive) out << " handleNotAlive";
Ady Abraham4cb525b2023-09-22 17:34:45 -0700426 if (obj.requestedFrameRate.isValid())
427 out << " requestedFrameRate: {" << obj.requestedFrameRate << "}";
Vishnu Nairf13c8982023-12-02 11:26:09 -0800428 if (obj.dropInputMode != gui::DropInputMode::NONE)
429 out << " dropInputMode=" << static_cast<uint32_t>(obj.dropInputMode);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000430 return out;
431}
432
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000433std::string RequestedLayerState::getDebugStringShort() const {
434 return "[" + std::to_string(id) + "]" + name;
435}
436
437bool RequestedLayerState::canBeDestroyed() const {
438 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
439}
440bool RequestedLayerState::isRoot() const {
441 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
442}
443bool RequestedLayerState::isHiddenByPolicy() const {
444 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
445};
446half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700447 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000448 return {0._hf, 0._hf, 0._hf, color.a};
449 }
450 return color;
451}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000452Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000453 // for buffer state layers we use the display frame size as the buffer size.
454 if (!externalTexture) {
455 return Rect::INVALID_RECT;
456 }
457
458 uint32_t bufWidth = externalTexture->getWidth();
459 uint32_t bufHeight = externalTexture->getHeight();
460
461 // Undo any transformations on the buffer and return the result.
462 if (bufferTransform & ui::Transform::ROT_90) {
463 std::swap(bufWidth, bufHeight);
464 }
465
466 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000467 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000468 if (invTransform & ui::Transform::ROT_90) {
469 std::swap(bufWidth, bufHeight);
470 }
471 }
472
473 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
474}
475
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000476Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
477 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000478 if (!crop.isEmpty() && size.isValid()) {
479 size.intersect(crop, &size);
480 } else if (!crop.isEmpty()) {
481 size = crop;
482 }
483 return size;
484}
485
486Rect RequestedLayerState::getBufferCrop() const {
487 // this is the crop rectangle that applies to the buffer
488 // itself (as opposed to the window)
Chavi Weingarten07597342023-09-14 21:10:59 +0000489 if (!bufferCrop.isEmpty() && externalTexture != nullptr) {
490 // if the buffer crop is defined and there's a valid buffer, intersect buffer size and crop
491 // since the crop should never exceed the size of the buffer.
492 Rect sizeAndCrop;
493 externalTexture->getBounds().intersect(bufferCrop, &sizeAndCrop);
494 return sizeAndCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000495 } else if (externalTexture != nullptr) {
496 // otherwise we use the whole buffer
497 return externalTexture->getBounds();
Chavi Weingarten07597342023-09-14 21:10:59 +0000498 } else if (!bufferCrop.isEmpty()) {
499 // if the buffer crop is defined, we use that
500 return bufferCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000501 } else {
502 // if we don't have a buffer yet, we use an empty/invalid crop
503 return Rect();
504 }
505}
506
507aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
508 const {
509 using aidl::android::hardware::graphics::composer3::Composition;
510 // TODO(b/238781169) check about sidestream ready flag
511 if (sidebandStream.get()) {
512 return Composition::SIDEBAND;
513 }
514 if (!externalTexture) {
515 return Composition::SOLID_COLOR;
516 }
517 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
518 return Composition::DISPLAY_DECORATION;
519 }
Vishnu Nairbd51f952023-08-31 22:50:14 -0700520 if (flags & layer_state_t::eLayerIsRefreshRateIndicator) {
521 return Composition::REFRESH_RATE_INDICATOR;
522 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000523 if (potentialCursor) {
524 return Composition::CURSOR;
525 }
526 return Composition::DEVICE;
527}
528
529Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
530 if (CC_LIKELY(exclude.isEmpty())) {
531 return win;
532 }
533 if (exclude.isRect()) {
534 return win.reduce(exclude.getBounds());
535 }
536 return Region(win).subtract(exclude).getBounds();
537}
538
Vishnu Nair04f89692022-11-16 23:21:05 +0000539// Returns true if the layer has a relative parent that is not its own parent. This is an input
540// error from the client, and this check allows us to handle it gracefully. If both parentId and
541// relativeParentId is unassigned then the layer does not have a valid relative parent.
542// If the relative parentid is unassigned, the layer will be considered relative but won't be
543// reachable.
544bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000545 return isRelativeOf &&
546 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000547}
548
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000549bool RequestedLayerState::hasInputInfo() const {
550 if (!windowInfoHandle) {
551 return false;
552 }
553 const auto windowInfo = windowInfoHandle->getInfo();
554 return windowInfo->token != nullptr ||
555 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
556}
557
Wenhui Yangab89d812024-09-11 23:21:38 +0000558bool RequestedLayerState::needsInputInfo() const {
559 if (potentialCursor) {
560 return false;
561 }
562
563 if ((sidebandStream != nullptr) || (externalTexture != nullptr)) {
564 return true;
565 }
566
567 if (!windowInfoHandle) {
568 return false;
569 }
570
571 const auto windowInfo = windowInfoHandle->getInfo();
572 return windowInfo->token != nullptr ||
573 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
574}
575
Vishnu Nair80a5a702023-02-11 01:21:51 +0000576bool RequestedLayerState::hasBlur() const {
577 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
578}
579
Vishnu Naird47bcee2023-02-24 18:08:51 +0000580bool RequestedLayerState::hasFrameUpdate() const {
581 return what & layer_state_t::CONTENT_DIRTY &&
582 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
583}
584
585bool RequestedLayerState::hasReadyFrame() const {
586 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
587}
588
589bool RequestedLayerState::hasSidebandStreamFrame() const {
590 return hasFrameUpdate() && sidebandStream.get();
591}
592
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700593bool RequestedLayerState::willReleaseBufferOnLatch() const {
594 return changes.test(Changes::Buffer) && !externalTexture;
595}
596
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000597bool RequestedLayerState::backpressureEnabled() const {
598 return flags & layer_state_t::eEnableBackpressure;
599}
600
601bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
602 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
603 if ((s.what & requiredFlags) != requiredFlags) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000604 SFTRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
605 (s.what | requiredFlags) & ~s.what);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000606 return false;
607 }
608
Ady Abrahambb1ad762024-03-27 18:31:28 -0700609 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
610 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
Vishnu Nairc5545d52024-05-22 17:28:49 -0700611 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
612 layer_state_t::eReparent |
Ady Abrahambb1ad762024-03-27 18:31:28 -0700613 (FlagManager::getInstance().latch_unsignaled_with_auto_refresh_changed()
614 ? 0
Vishnu Nairc5545d52024-05-22 17:28:49 -0700615 : (layer_state_t::eAutoRefreshChanged | layer_state_t::eFlagsChanged));
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000616 if (s.what & deniedFlags) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000617 SFTRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
618 s.what & deniedFlags);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000619 return false;
620 }
621
Vishnu Nairc5545d52024-05-22 17:28:49 -0700622 const uint64_t changedFlags = diff(s);
623 const uint64_t deniedChanges = layer_state_t::ePositionChanged | layer_state_t::eAlphaChanged |
624 layer_state_t::eColorTransformChanged | layer_state_t::eBackgroundColorChanged |
625 layer_state_t::eMatrixChanged | layer_state_t::eCornerRadiusChanged |
626 layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBufferTransformChanged |
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000627 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
628 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
629 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
630 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
631 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
Marzia Favarodcc9d9b2024-01-10 10:17:00 +0000632 layer_state_t::eEdgeExtensionChanged | layer_state_t::eBufferCropChanged |
633 layer_state_t::eDestinationFrameChanged | layer_state_t::eDimmingEnabledChanged |
634 layer_state_t::eExtendedRangeBrightnessChanged |
Vishnu Nairc5545d52024-05-22 17:28:49 -0700635 layer_state_t::eDesiredHdrHeadroomChanged |
636 (FlagManager::getInstance().latch_unsignaled_with_auto_refresh_changed()
637 ? layer_state_t::eFlagsChanged
638 : 0);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000639 if (changedFlags & deniedChanges) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000640 SFTRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
641 changedFlags & deniedChanges);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000642 return false;
643 }
644
645 return true;
646}
647
Vishnu Nair0808ae62023-08-07 21:42:42 -0700648bool RequestedLayerState::isProtected() const {
649 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
650}
651
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700652bool RequestedLayerState::hasSomethingToDraw() const {
653 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
654 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
655 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
656}
657
Vishnu Naird47bcee2023-02-24 18:08:51 +0000658void RequestedLayerState::clearChanges() {
659 what = 0;
660 changes.clear();
661}
662
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000663} // namespace android::surfaceflinger::frontend