blob: ee9302b93770f4f32023a194b2cba5bf0fd52609 [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),
Melody Hsu16355782024-10-09 08:56:28 +000059 layerIdToMirror(args.layerIdToMirror),
60 pendingBuffers(args.pendingBuffers) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000061 layerId = static_cast<int32_t>(args.sequence);
62 changes |= RequestedLayerState::Changes::Created;
63 metadata.merge(args.metadata);
64 changes |= RequestedLayerState::Changes::Metadata;
65 handleAlive = true;
Wenhui Yangab89d812024-09-11 23:21:38 +000066 // TODO: b/305254099 remove once we don't pass invisible windows to input
67 windowInfoHandle = nullptr;
Vishnu Nair1391de22023-03-05 19:56:14 -080068 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000069 canBeRoot = false;
70 }
Vishnu Naira9c43762023-01-27 19:10:25 +000071 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
72 changes |= RequestedLayerState::Changes::Mirror;
73 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
74 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000075 changes |= RequestedLayerState::Changes::Mirror;
76 }
77
78 flags = 0;
79 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
80 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
81 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
82 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
83 flags |= layer_state_t::eLayerSkipScreenshot;
84 }
85 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
86 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
87 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
88 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
89 // Set an invalid color so there is no color fill.
90 // (b/259981098) use an explicit flag instead of relying on invalid values.
91 color.r = -1.0_hf;
92 color.g = -1.0_hf;
93 color.b = -1.0_hf;
94 } else {
95 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
96 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000097 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000098 color.a = 1.0f;
99
Vishnu Naira9123c82024-10-03 03:56:44 +0000100 crop = {0, 0, -1, -1};
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000101 z = 0;
102 layerStack = ui::DEFAULT_LAYER_STACK;
103 transformToDisplayInverse = false;
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000104 desiredHdrSdrRatio = -1.f;
Sally Qi963049b2023-03-23 14:06:21 -0700105 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000106 dataspaceRequested = false;
107 hdrMetadata.validTypes = 0;
108 surfaceDamageRegion = Region::INVALID_REGION;
109 cornerRadius = 0.0f;
110 backgroundBlurRadius = 0;
111 api = -1;
112 hasColorTransform = false;
113 bufferTransform = 0;
114 requestedTransform.reset();
115 bufferData = std::make_shared<BufferData>();
116 bufferData->frameNumber = 0;
117 bufferData->acquireFence = sp<Fence>::make(-1);
118 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
119 colorSpaceAgnostic = false;
120 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
121 shadowRadius = 0.f;
122 fixedTransformHint = ui::Transform::ROT_INVALID;
123 destinationFrame.makeInvalid();
Vishnu Nair9e0017e2024-05-22 19:02:44 +0000124 trustedOverlay = gui::TrustedOverlay::UNSET;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000125 dropInputMode = gui::DropInputMode::NONE;
126 dimmingEnabled = true;
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700127 defaultFrameRateCompatibility = static_cast<int8_t>(scheduler::FrameRateCompatibility::Default);
Rachel Leece6e0042023-06-27 11:22:54 -0700128 frameRateCategory = static_cast<int8_t>(FrameRateCategory::Default);
Rachel Lee67afbea2023-09-28 15:35:07 -0700129 frameRateCategorySmoothSwitchOnly = false;
Rachel Lee58cc90d2023-09-05 18:50:20 -0700130 frameRateSelectionStrategy =
Rachel Lee70f7b692023-11-22 11:24:02 -0800131 static_cast<int8_t>(scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000132 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000133 gameMode = gui::GameMode::Unsupported;
134 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000135 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000136
137 if (name.length() > 77) {
138 std::string shortened;
139 shortened.append(name, 0, 36);
140 shortened.append("[...]");
141 shortened.append(name, name.length() - 36);
142 debugName = std::move(shortened);
143 } else {
144 debugName = name;
145 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000146}
147
148void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000149 const uint32_t oldFlags = flags;
150 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700151 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700152 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700153 const ui::Size oldBufferSize = hadBuffer
154 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
155 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700156 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700157 const bool oldBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
158 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
Vishnu Nair0808ae62023-08-07 21:42:42 -0700159
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700160 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000161 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700162 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000163 uint64_t clientChanges = what | layer_state_t::diff(clientState);
164 layer_state_t::merge(clientState);
165 what = clientChanges;
Sally Qief006582024-10-11 13:23:09 -0700166 LLOGV(layerId, "requested=%" PRIu64 " flags=%" PRIu64 " ", clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000167
168 if (clientState.what & layer_state_t::eFlagsChanged) {
Patrick Williamsd8edec02024-03-11 18:14:48 -0500169 if ((oldFlags ^ flags) &
170 (layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque |
171 layer_state_t::eLayerSecure)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000172 changes |= RequestedLayerState::Changes::Visibility |
173 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000174 }
175 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
176 changes |= RequestedLayerState::Changes::Geometry;
177 }
Vishnu Nair59a6be32024-01-29 10:26:21 -0800178 if ((oldFlags ^ flags) & layer_state_t::eCanOccludePresentation) {
179 changes |= RequestedLayerState::Changes::Input;
180 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000181 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700182
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700183 if (clientState.what & layer_state_t::eBufferChanged) {
184 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700185 const bool hasBuffer = externalTexture != nullptr;
186 if (hasBuffer || hasBuffer != hadBuffer) {
187 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700188 const ui::Size newBufferSize = hasBuffer
189 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
190 : ui::Size();
191 if (oldBufferSize != newBufferSize) {
192 changes |= RequestedLayerState::Changes::BufferSize;
193 changes |= RequestedLayerState::Changes::Geometry;
194 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700195 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
196 if (oldUsageFlags != usageFlags) {
197 changes |= RequestedLayerState::Changes::BufferUsageFlags;
198 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700199 }
200
201 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000202 changes |= RequestedLayerState::Changes::Geometry |
203 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000204 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
205 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700206
207 if (hasBuffer) {
208 const bool frameNumberChanged =
209 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
210 const uint64_t frameNumber =
211 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
212 bufferData->frameNumber = frameNumber;
213
214 if ((barrierProducerId > bufferData->producerId) ||
215 ((barrierProducerId == bufferData->producerId) &&
216 (barrierFrameNumber > bufferData->frameNumber))) {
217 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
218 " -> producedId=%d frameNumber=%" PRIu64,
Patrick Williams0db92f12023-10-18 12:02:06 -0500219 getDebugString().c_str(), barrierProducerId, barrierFrameNumber,
Vishnu Naird1f74982023-06-15 20:16:51 -0700220 bufferData->producerId, frameNumber);
221 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
222 /*overwrite=*/false);
223 }
224
225 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
226 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
227 }
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700228
229 const bool newBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
230 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
231 if (newBufferFormatOpaque != oldBufferFormatOpaque) {
232 changes |= RequestedLayerState::Changes::Visibility |
233 RequestedLayerState::Changes::VisibleRegion;
234 }
Vishnu Nair63221212023-04-06 15:17:37 -0700235 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700236
Vishnu Nair63221212023-04-06 15:17:37 -0700237 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
238 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700239 const bool hasSideStream = sidebandStream != nullptr;
240 if (hasSideStream != hadSideStream) {
241 changes |= RequestedLayerState::Changes::Geometry |
242 RequestedLayerState::Changes::VisibleRegion |
243 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
244 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000245 }
246 if (what & (layer_state_t::eAlphaChanged)) {
247 if (oldAlpha == 0 || color.a == 0) {
Vishnu Nair93e26b92023-10-16 21:36:51 -0700248 changes |= RequestedLayerState::Changes::Visibility;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000249 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000250 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700251
252 if (hadSomethingToDraw != hasSomethingToDraw()) {
253 changes |= RequestedLayerState::Changes::Visibility |
254 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000255 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000256 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
257 changes |= RequestedLayerState::Changes::Hierarchy;
258 if (clientChanges & layer_state_t::CONTENT_CHANGES)
259 changes |= RequestedLayerState::Changes::Content;
260 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
261 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000262 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
263 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000264 if (clientChanges & layer_state_t::INPUT_CHANGES)
265 changes |= RequestedLayerState::Changes::Input;
266 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
267 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000268 if (clientState.what & layer_state_t::eColorTransformChanged) {
269 static const mat4 identityMatrix = mat4();
270 hasColorTransform = colorTransform != identityMatrix;
271 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000272 if (clientState.what &
273 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
274 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000275 changes |= RequestedLayerState::Changes::Z;
276 }
277 if (clientState.what & layer_state_t::eReparent) {
278 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800279 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000280 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000281 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
282 // but thats the existing logic and until we make this behavior more explicit, we need
283 // to maintain this logic.
284 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000285 }
286 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
287 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800288 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000289 isRelativeOf = true;
290 relativeLayerSurfaceControl = nullptr;
291 }
292 if ((clientState.what & layer_state_t::eLayerChanged ||
293 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
294 isRelativeOf) {
295 // clear out relz data
296 relativeParentId = UNASSIGNED_LAYER_ID;
297 isRelativeOf = false;
298 changes |= RequestedLayerState::Changes::RelativeParent;
299 }
300 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
301 // provide a hint that we are are now a direct child and not a relative child.
302 changes |= RequestedLayerState::Changes::RelativeParent;
303 }
304 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800305 touchCropId = resolvedComposerState.touchCropId;
306 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000307 }
308 if (clientState.what & layer_state_t::eStretchChanged) {
309 stretchEffect.sanitize();
310 }
311
312 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
313 // TODO(b/238781169) handle callbacks
314 }
315
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000316 if (clientState.what & layer_state_t::ePositionChanged) {
317 requestedTransform.set(x, y);
318 }
319
320 if (clientState.what & layer_state_t::eMatrixChanged) {
321 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
322 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000323 if (clientState.what & layer_state_t::eMetadataChanged) {
324 const int32_t requestedGameMode =
325 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
326 if (requestedGameMode != -1) {
327 // The transaction will be received on the Task layer and needs to be applied to all
328 // child layers.
329 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
330 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700331 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000332 }
333 }
Nergi Rahardi27613c32024-05-23 06:57:02 +0000334 changes |= RequestedLayerState::Changes::Metadata;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000335 }
336 if (clientState.what & layer_state_t::eFrameRateChanged) {
337 const auto compatibility =
338 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
339 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
340 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700341 requestedFrameRate.vote =
342 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
343 compatibility, strategy);
344 changes |= RequestedLayerState::Changes::FrameRate;
345 }
346 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
347 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
348 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000349 changes |= RequestedLayerState::Changes::FrameRate;
350 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000351}
352
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000353ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
354 uint32_t bufferWidth = externalTexture->getWidth();
355 uint32_t bufferHeight = externalTexture->getHeight();
356 // Undo any transformations on the buffer.
357 if (bufferTransform & ui::Transform::ROT_90) {
358 std::swap(bufferWidth, bufferHeight);
359 }
360 if (transformToDisplayInverse) {
361 if (displayRotationFlags & ui::Transform::ROT_90) {
362 std::swap(bufferWidth, bufferHeight);
363 }
364 }
365 return {bufferWidth, bufferHeight};
366}
367
368ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000369 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
370 // If destination frame is not set, use the requested transform set via
371 // Transaction::setPosition and Transaction::setMatrix.
372 return requestedTransform;
373 }
374
375 Rect destRect = destinationFrame;
376 int32_t destW = destRect.width();
377 int32_t destH = destRect.height();
378 if (destRect.left < 0) {
379 destRect.left = 0;
380 destRect.right = destW;
381 }
382 if (destRect.top < 0) {
383 destRect.top = 0;
384 destRect.bottom = destH;
385 }
386
387 if (!externalTexture) {
388 ui::Transform transform;
389 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
390 return transform;
391 }
392
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000393 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000394
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000395 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
396 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000397 ui::Transform transform;
398 transform.set(sx, 0, 0, sy);
399 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
400 return transform;
401}
402
403std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000404 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700405 debug << "RequestedLayerState{" << name;
406 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
407 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
408 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
409 if (!handleAlive) debug << " !handle";
410 if (z != 0) debug << " z=" << z;
411 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Patrick Williams0db92f12023-10-18 12:02:06 -0500412 debug << "}";
Vishnu Nair80a5a702023-02-11 01:21:51 +0000413 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000414}
415
Ady Abraham4cb525b2023-09-22 17:34:45 -0700416std::ostream& operator<<(std::ostream& out, const scheduler::LayerInfo::FrameRate& obj) {
417 out << obj.vote.rate;
418 out << " " << ftl::enum_string_full(obj.vote.type);
419 out << " " << ftl::enum_string_full(obj.category);
420 return out;
421}
422
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000423std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
424 out << obj.debugName;
425 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
426 if (!obj.handleAlive) out << " handleNotAlive";
Ady Abraham4cb525b2023-09-22 17:34:45 -0700427 if (obj.requestedFrameRate.isValid())
428 out << " requestedFrameRate: {" << obj.requestedFrameRate << "}";
Vishnu Nairf13c8982023-12-02 11:26:09 -0800429 if (obj.dropInputMode != gui::DropInputMode::NONE)
430 out << " dropInputMode=" << static_cast<uint32_t>(obj.dropInputMode);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000431 return out;
432}
433
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000434std::string RequestedLayerState::getDebugStringShort() const {
435 return "[" + std::to_string(id) + "]" + name;
436}
437
438bool RequestedLayerState::canBeDestroyed() const {
439 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
440}
441bool RequestedLayerState::isRoot() const {
442 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
443}
444bool RequestedLayerState::isHiddenByPolicy() const {
445 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
446};
447half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700448 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000449 return {0._hf, 0._hf, 0._hf, color.a};
450 }
451 return color;
452}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000453Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000454 // for buffer state layers we use the display frame size as the buffer size.
455 if (!externalTexture) {
456 return Rect::INVALID_RECT;
457 }
458
459 uint32_t bufWidth = externalTexture->getWidth();
460 uint32_t bufHeight = externalTexture->getHeight();
461
462 // Undo any transformations on the buffer and return the result.
463 if (bufferTransform & ui::Transform::ROT_90) {
464 std::swap(bufWidth, bufHeight);
465 }
466
467 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000468 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000469 if (invTransform & ui::Transform::ROT_90) {
470 std::swap(bufWidth, bufHeight);
471 }
472 }
473
474 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
475}
476
Vishnu Naira9123c82024-10-03 03:56:44 +0000477FloatRect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
478 FloatRect size = bufferSize.toFloatRect();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000479 if (!crop.isEmpty() && size.isValid()) {
Vishnu Naira9123c82024-10-03 03:56:44 +0000480 size = size.intersect(crop);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000481 } else if (!crop.isEmpty()) {
482 size = crop;
483 }
484 return size;
485}
486
487Rect RequestedLayerState::getBufferCrop() const {
488 // this is the crop rectangle that applies to the buffer
489 // itself (as opposed to the window)
Chavi Weingarten07597342023-09-14 21:10:59 +0000490 if (!bufferCrop.isEmpty() && externalTexture != nullptr) {
491 // if the buffer crop is defined and there's a valid buffer, intersect buffer size and crop
492 // since the crop should never exceed the size of the buffer.
493 Rect sizeAndCrop;
494 externalTexture->getBounds().intersect(bufferCrop, &sizeAndCrop);
495 return sizeAndCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000496 } else if (externalTexture != nullptr) {
497 // otherwise we use the whole buffer
498 return externalTexture->getBounds();
Chavi Weingarten07597342023-09-14 21:10:59 +0000499 } else if (!bufferCrop.isEmpty()) {
500 // if the buffer crop is defined, we use that
501 return bufferCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000502 } else {
503 // if we don't have a buffer yet, we use an empty/invalid crop
504 return Rect();
505 }
506}
507
508aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
509 const {
510 using aidl::android::hardware::graphics::composer3::Composition;
511 // TODO(b/238781169) check about sidestream ready flag
512 if (sidebandStream.get()) {
513 return Composition::SIDEBAND;
514 }
515 if (!externalTexture) {
516 return Composition::SOLID_COLOR;
517 }
518 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
519 return Composition::DISPLAY_DECORATION;
520 }
Vishnu Nairbd51f952023-08-31 22:50:14 -0700521 if (flags & layer_state_t::eLayerIsRefreshRateIndicator) {
522 return Composition::REFRESH_RATE_INDICATOR;
523 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000524 if (potentialCursor) {
525 return Composition::CURSOR;
526 }
527 return Composition::DEVICE;
528}
529
530Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
531 if (CC_LIKELY(exclude.isEmpty())) {
532 return win;
533 }
534 if (exclude.isRect()) {
535 return win.reduce(exclude.getBounds());
536 }
537 return Region(win).subtract(exclude).getBounds();
538}
539
Vishnu Nair04f89692022-11-16 23:21:05 +0000540// Returns true if the layer has a relative parent that is not its own parent. This is an input
541// error from the client, and this check allows us to handle it gracefully. If both parentId and
542// relativeParentId is unassigned then the layer does not have a valid relative parent.
543// If the relative parentid is unassigned, the layer will be considered relative but won't be
544// reachable.
545bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000546 return isRelativeOf &&
547 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000548}
549
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000550bool RequestedLayerState::hasInputInfo() const {
551 if (!windowInfoHandle) {
552 return false;
553 }
554 const auto windowInfo = windowInfoHandle->getInfo();
555 return windowInfo->token != nullptr ||
556 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
557}
558
Wenhui Yangab89d812024-09-11 23:21:38 +0000559bool RequestedLayerState::needsInputInfo() const {
560 if (potentialCursor) {
561 return false;
562 }
563
564 if ((sidebandStream != nullptr) || (externalTexture != nullptr)) {
565 return true;
566 }
567
568 if (!windowInfoHandle) {
569 return false;
570 }
571
572 const auto windowInfo = windowInfoHandle->getInfo();
573 return windowInfo->token != nullptr ||
574 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
575}
576
Vishnu Nair80a5a702023-02-11 01:21:51 +0000577bool RequestedLayerState::hasBlur() const {
578 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
579}
580
Vishnu Naird47bcee2023-02-24 18:08:51 +0000581bool RequestedLayerState::hasFrameUpdate() const {
582 return what & layer_state_t::CONTENT_DIRTY &&
583 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
584}
585
586bool RequestedLayerState::hasReadyFrame() const {
587 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
588}
589
590bool RequestedLayerState::hasSidebandStreamFrame() const {
591 return hasFrameUpdate() && sidebandStream.get();
592}
593
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700594bool RequestedLayerState::willReleaseBufferOnLatch() const {
595 return changes.test(Changes::Buffer) && !externalTexture;
596}
597
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000598bool RequestedLayerState::backpressureEnabled() const {
599 return flags & layer_state_t::eEnableBackpressure;
600}
601
602bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
603 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
604 if ((s.what & requiredFlags) != requiredFlags) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000605 SFTRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
606 (s.what | requiredFlags) & ~s.what);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000607 return false;
608 }
609
Ady Abrahambb1ad762024-03-27 18:31:28 -0700610 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
611 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
Vishnu Nairc5545d52024-05-22 17:28:49 -0700612 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
613 layer_state_t::eReparent |
Ady Abrahambb1ad762024-03-27 18:31:28 -0700614 (FlagManager::getInstance().latch_unsignaled_with_auto_refresh_changed()
615 ? 0
Vishnu Nairc5545d52024-05-22 17:28:49 -0700616 : (layer_state_t::eAutoRefreshChanged | layer_state_t::eFlagsChanged));
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000617 if (s.what & deniedFlags) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000618 SFTRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
619 s.what & deniedFlags);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000620 return false;
621 }
622
Vishnu Nairc5545d52024-05-22 17:28:49 -0700623 const uint64_t changedFlags = diff(s);
624 const uint64_t deniedChanges = layer_state_t::ePositionChanged | layer_state_t::eAlphaChanged |
625 layer_state_t::eColorTransformChanged | layer_state_t::eBackgroundColorChanged |
626 layer_state_t::eMatrixChanged | layer_state_t::eCornerRadiusChanged |
627 layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBufferTransformChanged |
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000628 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
629 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
630 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
631 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
632 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
Marzia Favarodcc9d9b2024-01-10 10:17:00 +0000633 layer_state_t::eEdgeExtensionChanged | layer_state_t::eBufferCropChanged |
634 layer_state_t::eDestinationFrameChanged | layer_state_t::eDimmingEnabledChanged |
635 layer_state_t::eExtendedRangeBrightnessChanged |
Sally Qief006582024-10-11 13:23:09 -0700636 layer_state_t::eDesiredHdrHeadroomChanged | layer_state_t::eLutsChanged |
Vishnu Nairc5545d52024-05-22 17:28:49 -0700637 (FlagManager::getInstance().latch_unsignaled_with_auto_refresh_changed()
638 ? layer_state_t::eFlagsChanged
639 : 0);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000640 if (changedFlags & deniedChanges) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000641 SFTRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
642 changedFlags & deniedChanges);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000643 return false;
644 }
645
646 return true;
647}
648
Vishnu Nair0808ae62023-08-07 21:42:42 -0700649bool RequestedLayerState::isProtected() const {
650 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
651}
652
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700653bool RequestedLayerState::hasSomethingToDraw() const {
654 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
655 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
656 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
657}
658
Vishnu Naird47bcee2023-02-24 18:08:51 +0000659void RequestedLayerState::clearChanges() {
660 what = 0;
661 changes.clear();
662}
663
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000664} // namespace android::surfaceflinger::frontend