blob: 17d3f4bf386b19b72f5e8ab83a05d2dd97cfe359 [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;
Vishnu Nair1391de22023-03-05 19:56:14 -080065 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000066 canBeRoot = false;
67 }
Vishnu Naira9c43762023-01-27 19:10:25 +000068 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
69 changes |= RequestedLayerState::Changes::Mirror;
70 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
71 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000072 changes |= RequestedLayerState::Changes::Mirror;
73 }
74
75 flags = 0;
76 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
77 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
78 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
79 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
80 flags |= layer_state_t::eLayerSkipScreenshot;
81 }
82 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
83 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
84 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
85 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
86 // Set an invalid color so there is no color fill.
87 // (b/259981098) use an explicit flag instead of relying on invalid values.
88 color.r = -1.0_hf;
89 color.g = -1.0_hf;
90 color.b = -1.0_hf;
91 } else {
92 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
93 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000094 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000095 color.a = 1.0f;
96
97 crop.makeInvalid();
98 z = 0;
99 layerStack = ui::DEFAULT_LAYER_STACK;
100 transformToDisplayInverse = false;
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000101 desiredHdrSdrRatio = -1.f;
Sally Qi963049b2023-03-23 14:06:21 -0700102 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000103 dataspaceRequested = false;
104 hdrMetadata.validTypes = 0;
105 surfaceDamageRegion = Region::INVALID_REGION;
106 cornerRadius = 0.0f;
107 backgroundBlurRadius = 0;
108 api = -1;
109 hasColorTransform = false;
110 bufferTransform = 0;
111 requestedTransform.reset();
112 bufferData = std::make_shared<BufferData>();
113 bufferData->frameNumber = 0;
114 bufferData->acquireFence = sp<Fence>::make(-1);
115 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
116 colorSpaceAgnostic = false;
117 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
118 shadowRadius = 0.f;
119 fixedTransformHint = ui::Transform::ROT_INVALID;
120 destinationFrame.makeInvalid();
Vishnu Nair9e0017e2024-05-22 19:02:44 +0000121 trustedOverlay = gui::TrustedOverlay::UNSET;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000122 dropInputMode = gui::DropInputMode::NONE;
123 dimmingEnabled = true;
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700124 defaultFrameRateCompatibility = static_cast<int8_t>(scheduler::FrameRateCompatibility::Default);
Rachel Leece6e0042023-06-27 11:22:54 -0700125 frameRateCategory = static_cast<int8_t>(FrameRateCategory::Default);
Rachel Lee67afbea2023-09-28 15:35:07 -0700126 frameRateCategorySmoothSwitchOnly = false;
Rachel Lee58cc90d2023-09-05 18:50:20 -0700127 frameRateSelectionStrategy =
Rachel Lee70f7b692023-11-22 11:24:02 -0800128 static_cast<int8_t>(scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000129 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000130 gameMode = gui::GameMode::Unsupported;
131 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000132 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000133
134 if (name.length() > 77) {
135 std::string shortened;
136 shortened.append(name, 0, 36);
137 shortened.append("[...]");
138 shortened.append(name, name.length() - 36);
139 debugName = std::move(shortened);
140 } else {
141 debugName = name;
142 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000143}
144
145void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000146 const uint32_t oldFlags = flags;
147 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700148 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700149 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700150 const ui::Size oldBufferSize = hadBuffer
151 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
152 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700153 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700154 const bool oldBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
155 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
Vishnu Nair0808ae62023-08-07 21:42:42 -0700156
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700157 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000158 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700159 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000160 uint64_t clientChanges = what | layer_state_t::diff(clientState);
161 layer_state_t::merge(clientState);
162 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700163 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000164
165 if (clientState.what & layer_state_t::eFlagsChanged) {
Patrick Williamsd8edec02024-03-11 18:14:48 -0500166 if ((oldFlags ^ flags) &
167 (layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque |
168 layer_state_t::eLayerSecure)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000169 changes |= RequestedLayerState::Changes::Visibility |
170 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000171 }
172 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
173 changes |= RequestedLayerState::Changes::Geometry;
174 }
Vishnu Nair59a6be32024-01-29 10:26:21 -0800175 if ((oldFlags ^ flags) & layer_state_t::eCanOccludePresentation) {
176 changes |= RequestedLayerState::Changes::Input;
177 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000178 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700179
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700180 if (clientState.what & layer_state_t::eBufferChanged) {
181 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700182 const bool hasBuffer = externalTexture != nullptr;
183 if (hasBuffer || hasBuffer != hadBuffer) {
184 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700185 const ui::Size newBufferSize = hasBuffer
186 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
187 : ui::Size();
188 if (oldBufferSize != newBufferSize) {
189 changes |= RequestedLayerState::Changes::BufferSize;
190 changes |= RequestedLayerState::Changes::Geometry;
191 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700192 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
193 if (oldUsageFlags != usageFlags) {
194 changes |= RequestedLayerState::Changes::BufferUsageFlags;
195 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700196 }
197
198 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000199 changes |= RequestedLayerState::Changes::Geometry |
200 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000201 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
202 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700203
204 if (hasBuffer) {
205 const bool frameNumberChanged =
206 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
207 const uint64_t frameNumber =
208 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
209 bufferData->frameNumber = frameNumber;
210
211 if ((barrierProducerId > bufferData->producerId) ||
212 ((barrierProducerId == bufferData->producerId) &&
213 (barrierFrameNumber > bufferData->frameNumber))) {
214 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
215 " -> producedId=%d frameNumber=%" PRIu64,
Patrick Williams0db92f12023-10-18 12:02:06 -0500216 getDebugString().c_str(), barrierProducerId, barrierFrameNumber,
Vishnu Naird1f74982023-06-15 20:16:51 -0700217 bufferData->producerId, frameNumber);
218 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
219 /*overwrite=*/false);
220 }
221
222 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
223 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
224 }
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700225
226 const bool newBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
227 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
228 if (newBufferFormatOpaque != oldBufferFormatOpaque) {
229 changes |= RequestedLayerState::Changes::Visibility |
230 RequestedLayerState::Changes::VisibleRegion;
231 }
Vishnu Nair63221212023-04-06 15:17:37 -0700232 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700233
Vishnu Nair63221212023-04-06 15:17:37 -0700234 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
235 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700236 const bool hasSideStream = sidebandStream != nullptr;
237 if (hasSideStream != hadSideStream) {
238 changes |= RequestedLayerState::Changes::Geometry |
239 RequestedLayerState::Changes::VisibleRegion |
240 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
241 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000242 }
243 if (what & (layer_state_t::eAlphaChanged)) {
244 if (oldAlpha == 0 || color.a == 0) {
Vishnu Nair93e26b92023-10-16 21:36:51 -0700245 changes |= RequestedLayerState::Changes::Visibility;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000246 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000247 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700248
249 if (hadSomethingToDraw != hasSomethingToDraw()) {
250 changes |= RequestedLayerState::Changes::Visibility |
251 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000252 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000253 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
254 changes |= RequestedLayerState::Changes::Hierarchy;
255 if (clientChanges & layer_state_t::CONTENT_CHANGES)
256 changes |= RequestedLayerState::Changes::Content;
257 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
258 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000259 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
260 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000261 if (clientChanges & layer_state_t::INPUT_CHANGES)
262 changes |= RequestedLayerState::Changes::Input;
263 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
264 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000265 if (clientState.what & layer_state_t::eColorTransformChanged) {
266 static const mat4 identityMatrix = mat4();
267 hasColorTransform = colorTransform != identityMatrix;
268 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000269 if (clientState.what &
270 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
271 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000272 changes |= RequestedLayerState::Changes::Z;
273 }
274 if (clientState.what & layer_state_t::eReparent) {
275 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800276 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000277 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000278 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
279 // but thats the existing logic and until we make this behavior more explicit, we need
280 // to maintain this logic.
281 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000282 }
283 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
284 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800285 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000286 isRelativeOf = true;
287 relativeLayerSurfaceControl = nullptr;
288 }
289 if ((clientState.what & layer_state_t::eLayerChanged ||
290 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
291 isRelativeOf) {
292 // clear out relz data
293 relativeParentId = UNASSIGNED_LAYER_ID;
294 isRelativeOf = false;
295 changes |= RequestedLayerState::Changes::RelativeParent;
296 }
297 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
298 // provide a hint that we are are now a direct child and not a relative child.
299 changes |= RequestedLayerState::Changes::RelativeParent;
300 }
301 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800302 touchCropId = resolvedComposerState.touchCropId;
303 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000304 }
305 if (clientState.what & layer_state_t::eStretchChanged) {
306 stretchEffect.sanitize();
307 }
308
309 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
310 // TODO(b/238781169) handle callbacks
311 }
312
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000313 if (clientState.what & layer_state_t::ePositionChanged) {
314 requestedTransform.set(x, y);
315 }
316
317 if (clientState.what & layer_state_t::eMatrixChanged) {
318 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
319 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000320 if (clientState.what & layer_state_t::eMetadataChanged) {
321 const int32_t requestedGameMode =
322 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
323 if (requestedGameMode != -1) {
324 // The transaction will be received on the Task layer and needs to be applied to all
325 // child layers.
326 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
327 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700328 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000329 }
330 }
Nergi Rahardi27613c32024-05-23 06:57:02 +0000331 changes |= RequestedLayerState::Changes::Metadata;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000332 }
333 if (clientState.what & layer_state_t::eFrameRateChanged) {
334 const auto compatibility =
335 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
336 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
337 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700338 requestedFrameRate.vote =
339 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
340 compatibility, strategy);
341 changes |= RequestedLayerState::Changes::FrameRate;
342 }
343 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
344 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
345 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000346 changes |= RequestedLayerState::Changes::FrameRate;
347 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000348}
349
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000350ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
351 uint32_t bufferWidth = externalTexture->getWidth();
352 uint32_t bufferHeight = externalTexture->getHeight();
353 // Undo any transformations on the buffer.
354 if (bufferTransform & ui::Transform::ROT_90) {
355 std::swap(bufferWidth, bufferHeight);
356 }
357 if (transformToDisplayInverse) {
358 if (displayRotationFlags & ui::Transform::ROT_90) {
359 std::swap(bufferWidth, bufferHeight);
360 }
361 }
362 return {bufferWidth, bufferHeight};
363}
364
365ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000366 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
367 // If destination frame is not set, use the requested transform set via
368 // Transaction::setPosition and Transaction::setMatrix.
369 return requestedTransform;
370 }
371
372 Rect destRect = destinationFrame;
373 int32_t destW = destRect.width();
374 int32_t destH = destRect.height();
375 if (destRect.left < 0) {
376 destRect.left = 0;
377 destRect.right = destW;
378 }
379 if (destRect.top < 0) {
380 destRect.top = 0;
381 destRect.bottom = destH;
382 }
383
384 if (!externalTexture) {
385 ui::Transform transform;
386 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
387 return transform;
388 }
389
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000390 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000391
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000392 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
393 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000394 ui::Transform transform;
395 transform.set(sx, 0, 0, sy);
396 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
397 return transform;
398}
399
400std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000401 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700402 debug << "RequestedLayerState{" << name;
403 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
404 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
405 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
406 if (!handleAlive) debug << " !handle";
407 if (z != 0) debug << " z=" << z;
408 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Patrick Williams0db92f12023-10-18 12:02:06 -0500409 debug << "}";
Vishnu Nair80a5a702023-02-11 01:21:51 +0000410 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000411}
412
Ady Abraham4cb525b2023-09-22 17:34:45 -0700413std::ostream& operator<<(std::ostream& out, const scheduler::LayerInfo::FrameRate& obj) {
414 out << obj.vote.rate;
415 out << " " << ftl::enum_string_full(obj.vote.type);
416 out << " " << ftl::enum_string_full(obj.category);
417 return out;
418}
419
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000420std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
421 out << obj.debugName;
422 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
423 if (!obj.handleAlive) out << " handleNotAlive";
Ady Abraham4cb525b2023-09-22 17:34:45 -0700424 if (obj.requestedFrameRate.isValid())
425 out << " requestedFrameRate: {" << obj.requestedFrameRate << "}";
Vishnu Nairf13c8982023-12-02 11:26:09 -0800426 if (obj.dropInputMode != gui::DropInputMode::NONE)
427 out << " dropInputMode=" << static_cast<uint32_t>(obj.dropInputMode);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000428 return out;
429}
430
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000431std::string RequestedLayerState::getDebugStringShort() const {
432 return "[" + std::to_string(id) + "]" + name;
433}
434
435bool RequestedLayerState::canBeDestroyed() const {
436 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
437}
438bool RequestedLayerState::isRoot() const {
439 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
440}
441bool RequestedLayerState::isHiddenByPolicy() const {
442 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
443};
444half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700445 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000446 return {0._hf, 0._hf, 0._hf, color.a};
447 }
448 return color;
449}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000450Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000451 // for buffer state layers we use the display frame size as the buffer size.
452 if (!externalTexture) {
453 return Rect::INVALID_RECT;
454 }
455
456 uint32_t bufWidth = externalTexture->getWidth();
457 uint32_t bufHeight = externalTexture->getHeight();
458
459 // Undo any transformations on the buffer and return the result.
460 if (bufferTransform & ui::Transform::ROT_90) {
461 std::swap(bufWidth, bufHeight);
462 }
463
464 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000465 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000466 if (invTransform & ui::Transform::ROT_90) {
467 std::swap(bufWidth, bufHeight);
468 }
469 }
470
471 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
472}
473
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000474Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
475 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000476 if (!crop.isEmpty() && size.isValid()) {
477 size.intersect(crop, &size);
478 } else if (!crop.isEmpty()) {
479 size = crop;
480 }
481 return size;
482}
483
484Rect RequestedLayerState::getBufferCrop() const {
485 // this is the crop rectangle that applies to the buffer
486 // itself (as opposed to the window)
Chavi Weingarten07597342023-09-14 21:10:59 +0000487 if (!bufferCrop.isEmpty() && externalTexture != nullptr) {
488 // if the buffer crop is defined and there's a valid buffer, intersect buffer size and crop
489 // since the crop should never exceed the size of the buffer.
490 Rect sizeAndCrop;
491 externalTexture->getBounds().intersect(bufferCrop, &sizeAndCrop);
492 return sizeAndCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000493 } else if (externalTexture != nullptr) {
494 // otherwise we use the whole buffer
495 return externalTexture->getBounds();
Chavi Weingarten07597342023-09-14 21:10:59 +0000496 } else if (!bufferCrop.isEmpty()) {
497 // if the buffer crop is defined, we use that
498 return bufferCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000499 } else {
500 // if we don't have a buffer yet, we use an empty/invalid crop
501 return Rect();
502 }
503}
504
505aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
506 const {
507 using aidl::android::hardware::graphics::composer3::Composition;
508 // TODO(b/238781169) check about sidestream ready flag
509 if (sidebandStream.get()) {
510 return Composition::SIDEBAND;
511 }
512 if (!externalTexture) {
513 return Composition::SOLID_COLOR;
514 }
515 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
516 return Composition::DISPLAY_DECORATION;
517 }
Vishnu Nairbd51f952023-08-31 22:50:14 -0700518 if (flags & layer_state_t::eLayerIsRefreshRateIndicator) {
519 return Composition::REFRESH_RATE_INDICATOR;
520 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000521 if (potentialCursor) {
522 return Composition::CURSOR;
523 }
524 return Composition::DEVICE;
525}
526
527Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
528 if (CC_LIKELY(exclude.isEmpty())) {
529 return win;
530 }
531 if (exclude.isRect()) {
532 return win.reduce(exclude.getBounds());
533 }
534 return Region(win).subtract(exclude).getBounds();
535}
536
Vishnu Nair04f89692022-11-16 23:21:05 +0000537// Returns true if the layer has a relative parent that is not its own parent. This is an input
538// error from the client, and this check allows us to handle it gracefully. If both parentId and
539// relativeParentId is unassigned then the layer does not have a valid relative parent.
540// If the relative parentid is unassigned, the layer will be considered relative but won't be
541// reachable.
542bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000543 return isRelativeOf &&
544 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000545}
546
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000547bool RequestedLayerState::hasInputInfo() const {
548 if (!windowInfoHandle) {
549 return false;
550 }
551 const auto windowInfo = windowInfoHandle->getInfo();
552 return windowInfo->token != nullptr ||
553 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
554}
555
Vishnu Nair80a5a702023-02-11 01:21:51 +0000556bool RequestedLayerState::hasBlur() const {
557 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
558}
559
Vishnu Naird47bcee2023-02-24 18:08:51 +0000560bool RequestedLayerState::hasFrameUpdate() const {
561 return what & layer_state_t::CONTENT_DIRTY &&
562 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
563}
564
565bool RequestedLayerState::hasReadyFrame() const {
566 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
567}
568
569bool RequestedLayerState::hasSidebandStreamFrame() const {
570 return hasFrameUpdate() && sidebandStream.get();
571}
572
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700573bool RequestedLayerState::willReleaseBufferOnLatch() const {
574 return changes.test(Changes::Buffer) && !externalTexture;
575}
576
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000577bool RequestedLayerState::backpressureEnabled() const {
578 return flags & layer_state_t::eEnableBackpressure;
579}
580
581bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
582 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
583 if ((s.what & requiredFlags) != requiredFlags) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000584 SFTRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
585 (s.what | requiredFlags) & ~s.what);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000586 return false;
587 }
588
Ady Abrahambb1ad762024-03-27 18:31:28 -0700589 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
590 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
Vishnu Nairc5545d52024-05-22 17:28:49 -0700591 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
592 layer_state_t::eReparent |
Ady Abrahambb1ad762024-03-27 18:31:28 -0700593 (FlagManager::getInstance().latch_unsignaled_with_auto_refresh_changed()
594 ? 0
Vishnu Nairc5545d52024-05-22 17:28:49 -0700595 : (layer_state_t::eAutoRefreshChanged | layer_state_t::eFlagsChanged));
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000596 if (s.what & deniedFlags) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000597 SFTRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
598 s.what & deniedFlags);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000599 return false;
600 }
601
Vishnu Nairc5545d52024-05-22 17:28:49 -0700602 const uint64_t changedFlags = diff(s);
603 const uint64_t deniedChanges = layer_state_t::ePositionChanged | layer_state_t::eAlphaChanged |
604 layer_state_t::eColorTransformChanged | layer_state_t::eBackgroundColorChanged |
605 layer_state_t::eMatrixChanged | layer_state_t::eCornerRadiusChanged |
606 layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBufferTransformChanged |
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000607 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
608 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
609 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
610 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
611 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
612 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000613 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged |
Vishnu Nairc5545d52024-05-22 17:28:49 -0700614 layer_state_t::eDesiredHdrHeadroomChanged |
615 (FlagManager::getInstance().latch_unsignaled_with_auto_refresh_changed()
616 ? layer_state_t::eFlagsChanged
617 : 0);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000618 if (changedFlags & deniedChanges) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000619 SFTRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
620 changedFlags & deniedChanges);
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000621 return false;
622 }
623
624 return true;
625}
626
Vishnu Nair0808ae62023-08-07 21:42:42 -0700627bool RequestedLayerState::isProtected() const {
628 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
629}
630
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700631bool RequestedLayerState::hasSomethingToDraw() const {
632 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
633 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
634 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
635}
636
Vishnu Naird47bcee2023-02-24 18:08:51 +0000637void RequestedLayerState::clearChanges() {
638 what = 0;
639 changes.clear();
640}
641
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000642} // namespace android::surfaceflinger::frontend