blob: 3cf45a7fbbea6f083a11c41d51b96896f0d35abe [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 Nair4d9cef92023-06-24 22:34:41 +000022#include <gui/TraceUtils.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);
Vishnu Nairdf59f472024-05-17 16:51:33 +000061 changes |= RequestedLayerState::Changes::Created |
62 RequestedLayerState::Changes::RequiresComposition;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000063 metadata.merge(args.metadata);
64 changes |= RequestedLayerState::Changes::Metadata;
65 handleAlive = true;
Vishnu Nair1391de22023-03-05 19:56:14 -080066 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000067 canBeRoot = false;
68 }
Vishnu Naira9c43762023-01-27 19:10:25 +000069 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
70 changes |= RequestedLayerState::Changes::Mirror;
71 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
72 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000073 changes |= RequestedLayerState::Changes::Mirror;
74 }
75
76 flags = 0;
77 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
78 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
79 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
80 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
81 flags |= layer_state_t::eLayerSkipScreenshot;
82 }
83 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
84 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
85 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
86 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
87 // Set an invalid color so there is no color fill.
88 // (b/259981098) use an explicit flag instead of relying on invalid values.
89 color.r = -1.0_hf;
90 color.g = -1.0_hf;
91 color.b = -1.0_hf;
92 } else {
93 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
94 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000095 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000096 color.a = 1.0f;
97
98 crop.makeInvalid();
99 z = 0;
100 layerStack = ui::DEFAULT_LAYER_STACK;
101 transformToDisplayInverse = false;
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000102 desiredHdrSdrRatio = -1.f;
Sally Qi963049b2023-03-23 14:06:21 -0700103 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000104 dataspaceRequested = false;
105 hdrMetadata.validTypes = 0;
106 surfaceDamageRegion = Region::INVALID_REGION;
107 cornerRadius = 0.0f;
108 backgroundBlurRadius = 0;
109 api = -1;
110 hasColorTransform = false;
111 bufferTransform = 0;
112 requestedTransform.reset();
113 bufferData = std::make_shared<BufferData>();
114 bufferData->frameNumber = 0;
115 bufferData->acquireFence = sp<Fence>::make(-1);
116 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
117 colorSpaceAgnostic = false;
118 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
119 shadowRadius = 0.f;
120 fixedTransformHint = ui::Transform::ROT_INVALID;
121 destinationFrame.makeInvalid();
Vishnu Nair9e0017e2024-05-22 19:02:44 +0000122 trustedOverlay = gui::TrustedOverlay::UNSET;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000123 dropInputMode = gui::DropInputMode::NONE;
124 dimmingEnabled = true;
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700125 defaultFrameRateCompatibility = static_cast<int8_t>(scheduler::FrameRateCompatibility::Default);
Rachel Leece6e0042023-06-27 11:22:54 -0700126 frameRateCategory = static_cast<int8_t>(FrameRateCategory::Default);
Rachel Lee67afbea2023-09-28 15:35:07 -0700127 frameRateCategorySmoothSwitchOnly = false;
Rachel Lee58cc90d2023-09-05 18:50:20 -0700128 frameRateSelectionStrategy =
Rachel Lee70f7b692023-11-22 11:24:02 -0800129 static_cast<int8_t>(scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000130 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000131 gameMode = gui::GameMode::Unsupported;
132 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000133 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000134
135 if (name.length() > 77) {
136 std::string shortened;
137 shortened.append(name, 0, 36);
138 shortened.append("[...]");
139 shortened.append(name, name.length() - 36);
140 debugName = std::move(shortened);
141 } else {
142 debugName = name;
143 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000144}
145
146void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000147 const uint32_t oldFlags = flags;
148 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700149 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700150 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700151 const ui::Size oldBufferSize = hadBuffer
152 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
153 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700154 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700155 const bool oldBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
156 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
Vishnu Nair0808ae62023-08-07 21:42:42 -0700157
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700158 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000159 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700160 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000161 uint64_t clientChanges = what | layer_state_t::diff(clientState);
162 layer_state_t::merge(clientState);
163 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700164 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000165
166 if (clientState.what & layer_state_t::eFlagsChanged) {
Patrick Williamsd8edec02024-03-11 18:14:48 -0500167 if ((oldFlags ^ flags) &
168 (layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque |
169 layer_state_t::eLayerSecure)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000170 changes |= RequestedLayerState::Changes::Visibility |
171 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000172 }
173 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
174 changes |= RequestedLayerState::Changes::Geometry;
175 }
Vishnu Nair59a6be32024-01-29 10:26:21 -0800176 if ((oldFlags ^ flags) & layer_state_t::eCanOccludePresentation) {
177 changes |= RequestedLayerState::Changes::Input;
178 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000179 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700180
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700181 if (clientState.what & layer_state_t::eBufferChanged) {
182 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700183 const bool hasBuffer = externalTexture != nullptr;
184 if (hasBuffer || hasBuffer != hadBuffer) {
185 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700186 const ui::Size newBufferSize = hasBuffer
187 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
188 : ui::Size();
189 if (oldBufferSize != newBufferSize) {
190 changes |= RequestedLayerState::Changes::BufferSize;
191 changes |= RequestedLayerState::Changes::Geometry;
192 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700193 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
194 if (oldUsageFlags != usageFlags) {
195 changes |= RequestedLayerState::Changes::BufferUsageFlags;
196 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700197 }
198
199 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000200 changes |= RequestedLayerState::Changes::Geometry |
201 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000202 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
203 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700204
205 if (hasBuffer) {
206 const bool frameNumberChanged =
207 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
208 const uint64_t frameNumber =
209 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
210 bufferData->frameNumber = frameNumber;
211
212 if ((barrierProducerId > bufferData->producerId) ||
213 ((barrierProducerId == bufferData->producerId) &&
214 (barrierFrameNumber > bufferData->frameNumber))) {
215 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
216 " -> producedId=%d frameNumber=%" PRIu64,
Patrick Williams0db92f12023-10-18 12:02:06 -0500217 getDebugString().c_str(), barrierProducerId, barrierFrameNumber,
Vishnu Naird1f74982023-06-15 20:16:51 -0700218 bufferData->producerId, frameNumber);
219 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
220 /*overwrite=*/false);
221 }
222
223 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
224 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
225 }
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700226
227 const bool newBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
228 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
229 if (newBufferFormatOpaque != oldBufferFormatOpaque) {
230 changes |= RequestedLayerState::Changes::Visibility |
231 RequestedLayerState::Changes::VisibleRegion;
232 }
Vishnu Nair63221212023-04-06 15:17:37 -0700233 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700234
Vishnu Nair63221212023-04-06 15:17:37 -0700235 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
236 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700237 const bool hasSideStream = sidebandStream != nullptr;
238 if (hasSideStream != hadSideStream) {
239 changes |= RequestedLayerState::Changes::Geometry |
240 RequestedLayerState::Changes::VisibleRegion |
241 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
242 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000243 }
244 if (what & (layer_state_t::eAlphaChanged)) {
245 if (oldAlpha == 0 || color.a == 0) {
Vishnu Nair93e26b92023-10-16 21:36:51 -0700246 changes |= RequestedLayerState::Changes::Visibility;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000247 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000248 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700249
250 if (hadSomethingToDraw != hasSomethingToDraw()) {
251 changes |= RequestedLayerState::Changes::Visibility |
Vishnu Nairdf59f472024-05-17 16:51:33 +0000252 RequestedLayerState::Changes::VisibleRegion |
253 RequestedLayerState::Changes::RequiresComposition;
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 Nairdf59f472024-05-17 16:51:33 +0000263 if (clientChanges & layer_state_t::REQUIRES_COMPOSITION)
264 changes |= RequestedLayerState::Changes::RequiresComposition;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000265 if (clientChanges & layer_state_t::INPUT_CHANGES)
266 changes |= RequestedLayerState::Changes::Input;
267 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
268 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000269 if (clientState.what & layer_state_t::eColorTransformChanged) {
270 static const mat4 identityMatrix = mat4();
271 hasColorTransform = colorTransform != identityMatrix;
272 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000273 if (clientState.what &
274 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
275 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000276 changes |= RequestedLayerState::Changes::Z;
277 }
278 if (clientState.what & layer_state_t::eReparent) {
279 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800280 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000281 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000282 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
283 // but thats the existing logic and until we make this behavior more explicit, we need
284 // to maintain this logic.
285 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000286 }
287 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
288 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800289 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000290 isRelativeOf = true;
291 relativeLayerSurfaceControl = nullptr;
292 }
293 if ((clientState.what & layer_state_t::eLayerChanged ||
294 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
295 isRelativeOf) {
296 // clear out relz data
297 relativeParentId = UNASSIGNED_LAYER_ID;
298 isRelativeOf = false;
299 changes |= RequestedLayerState::Changes::RelativeParent;
300 }
301 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
302 // provide a hint that we are are now a direct child and not a relative child.
303 changes |= RequestedLayerState::Changes::RelativeParent;
304 }
305 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800306 touchCropId = resolvedComposerState.touchCropId;
307 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000308 }
309 if (clientState.what & layer_state_t::eStretchChanged) {
310 stretchEffect.sanitize();
311 }
312
313 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
314 // TODO(b/238781169) handle callbacks
315 }
316
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000317 if (clientState.what & layer_state_t::ePositionChanged) {
318 requestedTransform.set(x, y);
319 }
320
321 if (clientState.what & layer_state_t::eMatrixChanged) {
322 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
323 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000324 if (clientState.what & layer_state_t::eMetadataChanged) {
325 const int32_t requestedGameMode =
326 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
327 if (requestedGameMode != -1) {
328 // The transaction will be received on the Task layer and needs to be applied to all
329 // child layers.
330 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
331 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700332 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000333 }
334 }
Nergi Rahardi6431d4a2024-05-15 18:42:48 +0900335 changes |= RequestedLayerState::Changes::Metadata;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000336 }
337 if (clientState.what & layer_state_t::eFrameRateChanged) {
338 const auto compatibility =
339 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
340 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
341 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700342 requestedFrameRate.vote =
343 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
344 compatibility, strategy);
345 changes |= RequestedLayerState::Changes::FrameRate;
346 }
347 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
348 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
349 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000350 changes |= RequestedLayerState::Changes::FrameRate;
351 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000352}
353
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000354ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
355 uint32_t bufferWidth = externalTexture->getWidth();
356 uint32_t bufferHeight = externalTexture->getHeight();
357 // Undo any transformations on the buffer.
358 if (bufferTransform & ui::Transform::ROT_90) {
359 std::swap(bufferWidth, bufferHeight);
360 }
361 if (transformToDisplayInverse) {
362 if (displayRotationFlags & ui::Transform::ROT_90) {
363 std::swap(bufferWidth, bufferHeight);
364 }
365 }
366 return {bufferWidth, bufferHeight};
367}
368
369ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000370 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
371 // If destination frame is not set, use the requested transform set via
372 // Transaction::setPosition and Transaction::setMatrix.
373 return requestedTransform;
374 }
375
376 Rect destRect = destinationFrame;
377 int32_t destW = destRect.width();
378 int32_t destH = destRect.height();
379 if (destRect.left < 0) {
380 destRect.left = 0;
381 destRect.right = destW;
382 }
383 if (destRect.top < 0) {
384 destRect.top = 0;
385 destRect.bottom = destH;
386 }
387
388 if (!externalTexture) {
389 ui::Transform transform;
390 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
391 return transform;
392 }
393
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000394 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000395
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000396 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
397 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000398 ui::Transform transform;
399 transform.set(sx, 0, 0, sy);
400 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
401 return transform;
402}
403
404std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000405 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700406 debug << "RequestedLayerState{" << name;
407 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
408 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
409 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
410 if (!handleAlive) debug << " !handle";
411 if (z != 0) debug << " z=" << z;
412 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Patrick Williams0db92f12023-10-18 12:02:06 -0500413 debug << "}";
Vishnu Nair80a5a702023-02-11 01:21:51 +0000414 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000415}
416
Ady Abraham4cb525b2023-09-22 17:34:45 -0700417std::ostream& operator<<(std::ostream& out, const scheduler::LayerInfo::FrameRate& obj) {
418 out << obj.vote.rate;
419 out << " " << ftl::enum_string_full(obj.vote.type);
420 out << " " << ftl::enum_string_full(obj.category);
421 return out;
422}
423
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000424std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
425 out << obj.debugName;
426 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
427 if (!obj.handleAlive) out << " handleNotAlive";
Ady Abraham4cb525b2023-09-22 17:34:45 -0700428 if (obj.requestedFrameRate.isValid())
429 out << " requestedFrameRate: {" << obj.requestedFrameRate << "}";
Vishnu Nairf13c8982023-12-02 11:26:09 -0800430 if (obj.dropInputMode != gui::DropInputMode::NONE)
431 out << " dropInputMode=" << static_cast<uint32_t>(obj.dropInputMode);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000432 return out;
433}
434
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000435std::string RequestedLayerState::getDebugStringShort() const {
436 return "[" + std::to_string(id) + "]" + name;
437}
438
439bool RequestedLayerState::canBeDestroyed() const {
440 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
441}
442bool RequestedLayerState::isRoot() const {
443 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
444}
445bool RequestedLayerState::isHiddenByPolicy() const {
446 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
447};
448half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700449 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000450 return {0._hf, 0._hf, 0._hf, color.a};
451 }
452 return color;
453}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000454Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000455 // for buffer state layers we use the display frame size as the buffer size.
456 if (!externalTexture) {
457 return Rect::INVALID_RECT;
458 }
459
460 uint32_t bufWidth = externalTexture->getWidth();
461 uint32_t bufHeight = externalTexture->getHeight();
462
463 // Undo any transformations on the buffer and return the result.
464 if (bufferTransform & ui::Transform::ROT_90) {
465 std::swap(bufWidth, bufHeight);
466 }
467
468 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000469 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000470 if (invTransform & ui::Transform::ROT_90) {
471 std::swap(bufWidth, bufHeight);
472 }
473 }
474
475 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
476}
477
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000478Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
479 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000480 if (!crop.isEmpty() && size.isValid()) {
481 size.intersect(crop, &size);
482 } else if (!crop.isEmpty()) {
483 size = crop;
484 }
485 return size;
486}
487
488Rect RequestedLayerState::getBufferCrop() const {
489 // this is the crop rectangle that applies to the buffer
490 // itself (as opposed to the window)
Chavi Weingarten07597342023-09-14 21:10:59 +0000491 if (!bufferCrop.isEmpty() && externalTexture != nullptr) {
492 // if the buffer crop is defined and there's a valid buffer, intersect buffer size and crop
493 // since the crop should never exceed the size of the buffer.
494 Rect sizeAndCrop;
495 externalTexture->getBounds().intersect(bufferCrop, &sizeAndCrop);
496 return sizeAndCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000497 } else if (externalTexture != nullptr) {
498 // otherwise we use the whole buffer
499 return externalTexture->getBounds();
Chavi Weingarten07597342023-09-14 21:10:59 +0000500 } else if (!bufferCrop.isEmpty()) {
501 // if the buffer crop is defined, we use that
502 return bufferCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000503 } else {
504 // if we don't have a buffer yet, we use an empty/invalid crop
505 return Rect();
506 }
507}
508
509aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
510 const {
511 using aidl::android::hardware::graphics::composer3::Composition;
512 // TODO(b/238781169) check about sidestream ready flag
513 if (sidebandStream.get()) {
514 return Composition::SIDEBAND;
515 }
516 if (!externalTexture) {
517 return Composition::SOLID_COLOR;
518 }
519 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
520 return Composition::DISPLAY_DECORATION;
521 }
Vishnu Nairbd51f952023-08-31 22:50:14 -0700522 if (flags & layer_state_t::eLayerIsRefreshRateIndicator) {
523 return Composition::REFRESH_RATE_INDICATOR;
524 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000525 if (potentialCursor) {
526 return Composition::CURSOR;
527 }
528 return Composition::DEVICE;
529}
530
531Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
532 if (CC_LIKELY(exclude.isEmpty())) {
533 return win;
534 }
535 if (exclude.isRect()) {
536 return win.reduce(exclude.getBounds());
537 }
538 return Region(win).subtract(exclude).getBounds();
539}
540
Vishnu Nair04f89692022-11-16 23:21:05 +0000541// Returns true if the layer has a relative parent that is not its own parent. This is an input
542// error from the client, and this check allows us to handle it gracefully. If both parentId and
543// relativeParentId is unassigned then the layer does not have a valid relative parent.
544// If the relative parentid is unassigned, the layer will be considered relative but won't be
545// reachable.
546bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000547 return isRelativeOf &&
548 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000549}
550
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000551bool RequestedLayerState::hasInputInfo() const {
552 if (!windowInfoHandle) {
553 return false;
554 }
555 const auto windowInfo = windowInfoHandle->getInfo();
556 return windowInfo->token != nullptr ||
557 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
558}
559
Vishnu Nair80a5a702023-02-11 01:21:51 +0000560bool RequestedLayerState::hasBlur() const {
561 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
562}
563
Vishnu Naird47bcee2023-02-24 18:08:51 +0000564bool RequestedLayerState::hasFrameUpdate() const {
565 return what & layer_state_t::CONTENT_DIRTY &&
566 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
567}
568
569bool RequestedLayerState::hasReadyFrame() const {
570 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
571}
572
573bool RequestedLayerState::hasSidebandStreamFrame() const {
574 return hasFrameUpdate() && sidebandStream.get();
575}
576
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700577bool RequestedLayerState::willReleaseBufferOnLatch() const {
578 return changes.test(Changes::Buffer) && !externalTexture;
579}
580
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000581bool RequestedLayerState::backpressureEnabled() const {
582 return flags & layer_state_t::eEnableBackpressure;
583}
584
585bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
586 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
587 if ((s.what & requiredFlags) != requiredFlags) {
588 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
589 (s.what | requiredFlags) & ~s.what);
590 return false;
591 }
592
Ady Abrahambb1ad762024-03-27 18:31:28 -0700593 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
594 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
595 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
596 layer_state_t::eLayerStackChanged | layer_state_t::eReparent |
597 (FlagManager::getInstance().latch_unsignaled_with_auto_refresh_changed()
598 ? 0
599 : layer_state_t::eAutoRefreshChanged);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000600 if (s.what & deniedFlags) {
601 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
602 s.what & deniedFlags);
603 return false;
604 }
605
606 bool changedFlags = diff(s);
607 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
608 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
609 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
610 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
611 layer_state_t::eBufferTransformChanged |
612 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
613 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
614 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
615 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
616 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
617 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000618 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged |
619 layer_state_t::eDesiredHdrHeadroomChanged;
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000620 if (changedFlags & deniedChanges) {
621 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
622 s.what & deniedChanges);
623 return false;
624 }
625
626 return true;
627}
628
Vishnu Nair0808ae62023-08-07 21:42:42 -0700629bool RequestedLayerState::isProtected() const {
630 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
631}
632
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700633bool RequestedLayerState::hasSomethingToDraw() const {
634 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
635 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
636 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
637}
638
Vishnu Naird47bcee2023-02-24 18:08:51 +0000639void RequestedLayerState::clearChanges() {
640 what = 0;
641 changes.clear();
642}
643
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000644} // namespace android::surfaceflinger::frontend