Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 16 | // #define LOG_NDEBUG 0 |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 17 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | #undef LOG_TAG |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 20 | #define LOG_TAG "SurfaceFlinger" |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 21 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 22 | #include <gui/TraceUtils.h> |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 23 | #include <log/log.h> |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 24 | #include <private/android_filesystem_config.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include "Layer.h" |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 28 | #include "LayerCreationArgs.h" |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 29 | #include "LayerLog.h" |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 30 | #include "RequestedLayerState.h" |
| 31 | |
| 32 | namespace android::surfaceflinger::frontend { |
| 33 | using ftl::Flags; |
| 34 | using namespace ftl::flag_operators; |
| 35 | |
| 36 | namespace { |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 37 | std::string layerIdsToString(const std::vector<uint32_t>& layerIds) { |
| 38 | std::stringstream stream; |
| 39 | stream << "{"; |
| 40 | for (auto layerId : layerIds) { |
| 41 | stream << layerId << ","; |
| 42 | } |
| 43 | stream << "}"; |
| 44 | return stream.str(); |
| 45 | } |
| 46 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 47 | } // namespace |
| 48 | |
| 49 | RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args) |
| 50 | : id(args.sequence), |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 51 | name(args.name + "#" + std::to_string(args.sequence)), |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 52 | canBeRoot(args.addToRoot), |
| 53 | layerCreationFlags(args.flags), |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 54 | ownerUid(args.ownerUid), |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 55 | ownerPid(args.ownerPid), |
| 56 | parentId(args.parentId), |
| 57 | layerIdToMirror(args.layerIdToMirror) { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 58 | layerId = static_cast<int32_t>(args.sequence); |
| 59 | changes |= RequestedLayerState::Changes::Created; |
| 60 | metadata.merge(args.metadata); |
| 61 | changes |= RequestedLayerState::Changes::Metadata; |
| 62 | handleAlive = true; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 63 | if (parentId != UNASSIGNED_LAYER_ID) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 64 | canBeRoot = false; |
| 65 | } |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 66 | if (layerIdToMirror != UNASSIGNED_LAYER_ID) { |
| 67 | changes |= RequestedLayerState::Changes::Mirror; |
| 68 | } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) { |
| 69 | layerStackToMirror = args.layerStackToMirror; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 70 | changes |= RequestedLayerState::Changes::Mirror; |
| 71 | } |
| 72 | |
| 73 | flags = 0; |
| 74 | if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden; |
| 75 | if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque; |
| 76 | if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure; |
| 77 | if (args.flags & ISurfaceComposerClient::eSkipScreenshot) { |
| 78 | flags |= layer_state_t::eLayerSkipScreenshot; |
| 79 | } |
| 80 | premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied); |
| 81 | potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow; |
| 82 | protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp; |
| 83 | if (args.flags & ISurfaceComposerClient::eNoColorFill) { |
| 84 | // Set an invalid color so there is no color fill. |
| 85 | // (b/259981098) use an explicit flag instead of relying on invalid values. |
| 86 | color.r = -1.0_hf; |
| 87 | color.g = -1.0_hf; |
| 88 | color.b = -1.0_hf; |
| 89 | } else { |
| 90 | color.rgb = {0.0_hf, 0.0_hf, 0.0_hf}; |
| 91 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 92 | LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 93 | color.a = 1.0f; |
| 94 | |
| 95 | crop.makeInvalid(); |
| 96 | z = 0; |
| 97 | layerStack = ui::DEFAULT_LAYER_STACK; |
| 98 | transformToDisplayInverse = false; |
| 99 | dataspace = ui::Dataspace::UNKNOWN; |
Sally Qi | 963049b | 2023-03-23 14:06:21 -0700 | [diff] [blame] | 100 | desiredHdrSdrRatio = 1.f; |
| 101 | currentHdrSdrRatio = 1.f; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 102 | dataspaceRequested = false; |
| 103 | hdrMetadata.validTypes = 0; |
| 104 | surfaceDamageRegion = Region::INVALID_REGION; |
| 105 | cornerRadius = 0.0f; |
| 106 | backgroundBlurRadius = 0; |
| 107 | api = -1; |
| 108 | hasColorTransform = false; |
| 109 | bufferTransform = 0; |
| 110 | requestedTransform.reset(); |
| 111 | bufferData = std::make_shared<BufferData>(); |
| 112 | bufferData->frameNumber = 0; |
| 113 | bufferData->acquireFence = sp<Fence>::make(-1); |
| 114 | acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence); |
| 115 | colorSpaceAgnostic = false; |
| 116 | frameRateSelectionPriority = Layer::PRIORITY_UNSET; |
| 117 | shadowRadius = 0.f; |
| 118 | fixedTransformHint = ui::Transform::ROT_INVALID; |
| 119 | destinationFrame.makeInvalid(); |
| 120 | isTrustedOverlay = false; |
| 121 | dropInputMode = gui::DropInputMode::NONE; |
| 122 | dimmingEnabled = true; |
| 123 | defaultFrameRateCompatibility = |
| 124 | static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default); |
| 125 | dataspace = ui::Dataspace::V0_SRGB; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 126 | gameMode = gui::GameMode::Unsupported; |
| 127 | requestedFrameRate = {}; |
Alec Mouri | f4af03e | 2023-02-11 00:25:24 +0000 | [diff] [blame] | 128 | cachingHint = gui::CachingHint::Enabled; |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 129 | |
| 130 | if (name.length() > 77) { |
| 131 | std::string shortened; |
| 132 | shortened.append(name, 0, 36); |
| 133 | shortened.append("[...]"); |
| 134 | shortened.append(name, name.length() - 36); |
| 135 | debugName = std::move(shortened); |
| 136 | } else { |
| 137 | debugName = name; |
| 138 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 142 | const uint32_t oldFlags = flags; |
| 143 | const half oldAlpha = color.a; |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 144 | const bool hadBuffer = externalTexture != nullptr; |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 145 | uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0; |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 146 | const ui::Size oldBufferSize = hadBuffer |
| 147 | ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight()) |
| 148 | : ui::Size(); |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 149 | const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0; |
| 150 | |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 151 | const bool hadSideStream = sidebandStream != nullptr; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 152 | const layer_state_t& clientState = resolvedComposerState.state; |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame^] | 153 | const bool hadSomethingToDraw = hasSomethingToDraw(); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 154 | uint64_t clientChanges = what | layer_state_t::diff(clientState); |
| 155 | layer_state_t::merge(clientState); |
| 156 | what = clientChanges; |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 157 | LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 158 | |
| 159 | if (clientState.what & layer_state_t::eFlagsChanged) { |
| 160 | if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 161 | changes |= RequestedLayerState::Changes::Visibility | |
| 162 | RequestedLayerState::Changes::VisibleRegion; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 163 | } |
| 164 | if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) { |
| 165 | changes |= RequestedLayerState::Changes::Geometry; |
| 166 | } |
| 167 | } |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 168 | |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 169 | if (clientState.what & layer_state_t::eBufferChanged) { |
| 170 | externalTexture = resolvedComposerState.externalTexture; |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 171 | const bool hasBuffer = externalTexture != nullptr; |
| 172 | if (hasBuffer || hasBuffer != hadBuffer) { |
| 173 | changes |= RequestedLayerState::Changes::Buffer; |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 174 | const ui::Size newBufferSize = hasBuffer |
| 175 | ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight()) |
| 176 | : ui::Size(); |
| 177 | if (oldBufferSize != newBufferSize) { |
| 178 | changes |= RequestedLayerState::Changes::BufferSize; |
| 179 | changes |= RequestedLayerState::Changes::Geometry; |
| 180 | } |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 181 | const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0; |
| 182 | if (oldUsageFlags != usageFlags) { |
| 183 | changes |= RequestedLayerState::Changes::BufferUsageFlags; |
| 184 | } |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (hasBuffer != hadBuffer) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 188 | changes |= RequestedLayerState::Changes::Geometry | |
| 189 | RequestedLayerState::Changes::VisibleRegion | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 190 | RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input; |
| 191 | } |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 192 | |
| 193 | if (hasBuffer) { |
| 194 | const bool frameNumberChanged = |
| 195 | bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged); |
| 196 | const uint64_t frameNumber = |
| 197 | frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1; |
| 198 | bufferData->frameNumber = frameNumber; |
| 199 | |
| 200 | if ((barrierProducerId > bufferData->producerId) || |
| 201 | ((barrierProducerId == bufferData->producerId) && |
| 202 | (barrierFrameNumber > bufferData->frameNumber))) { |
| 203 | ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64 |
| 204 | " -> producedId=%d frameNumber=%" PRIu64, |
| 205 | getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber, |
| 206 | bufferData->producerId, frameNumber); |
| 207 | TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_", |
| 208 | /*overwrite=*/false); |
| 209 | } |
| 210 | |
| 211 | barrierProducerId = std::max(bufferData->producerId, barrierProducerId); |
| 212 | barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber); |
| 213 | } |
Vishnu Nair | 6322121 | 2023-04-06 15:17:37 -0700 | [diff] [blame] | 214 | } |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 215 | |
Vishnu Nair | 6322121 | 2023-04-06 15:17:37 -0700 | [diff] [blame] | 216 | if (clientState.what & layer_state_t::eSidebandStreamChanged) { |
| 217 | changes |= RequestedLayerState::Changes::SidebandStream; |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 218 | const bool hasSideStream = sidebandStream != nullptr; |
| 219 | if (hasSideStream != hadSideStream) { |
| 220 | changes |= RequestedLayerState::Changes::Geometry | |
| 221 | RequestedLayerState::Changes::VisibleRegion | |
| 222 | RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input; |
| 223 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 224 | } |
| 225 | if (what & (layer_state_t::eAlphaChanged)) { |
| 226 | if (oldAlpha == 0 || color.a == 0) { |
| 227 | changes |= RequestedLayerState::Changes::Visibility | |
| 228 | RequestedLayerState::Changes::VisibleRegion; |
| 229 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 230 | } |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame^] | 231 | |
| 232 | if (hadSomethingToDraw != hasSomethingToDraw()) { |
| 233 | changes |= RequestedLayerState::Changes::Visibility | |
| 234 | RequestedLayerState::Changes::VisibleRegion; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 235 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 236 | if (clientChanges & layer_state_t::HIERARCHY_CHANGES) |
| 237 | changes |= RequestedLayerState::Changes::Hierarchy; |
| 238 | if (clientChanges & layer_state_t::CONTENT_CHANGES) |
| 239 | changes |= RequestedLayerState::Changes::Content; |
| 240 | if (clientChanges & layer_state_t::GEOMETRY_CHANGES) |
| 241 | changes |= RequestedLayerState::Changes::Geometry; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 242 | if (clientChanges & layer_state_t::AFFECTS_CHILDREN) |
| 243 | changes |= RequestedLayerState::Changes::AffectsChildren; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 244 | if (clientChanges & layer_state_t::INPUT_CHANGES) |
| 245 | changes |= RequestedLayerState::Changes::Input; |
| 246 | if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES) |
| 247 | changes |= RequestedLayerState::Changes::VisibleRegion; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 248 | if (clientState.what & layer_state_t::eColorTransformChanged) { |
| 249 | static const mat4 identityMatrix = mat4(); |
| 250 | hasColorTransform = colorTransform != identityMatrix; |
| 251 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 252 | if (clientState.what & |
| 253 | (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged | |
| 254 | layer_state_t::eLayerStackChanged)) { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 255 | changes |= RequestedLayerState::Changes::Z; |
| 256 | } |
| 257 | if (clientState.what & layer_state_t::eReparent) { |
| 258 | changes |= RequestedLayerState::Changes::Parent; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 259 | parentId = resolvedComposerState.parentId; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 260 | parentSurfaceControlForChild = nullptr; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 261 | // Once a layer has be reparented, it cannot be placed at the root. It sounds odd |
| 262 | // but thats the existing logic and until we make this behavior more explicit, we need |
| 263 | // to maintain this logic. |
| 264 | canBeRoot = false; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 265 | } |
| 266 | if (clientState.what & layer_state_t::eRelativeLayerChanged) { |
| 267 | changes |= RequestedLayerState::Changes::RelativeParent; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 268 | relativeParentId = resolvedComposerState.relativeParentId; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 269 | isRelativeOf = true; |
| 270 | relativeLayerSurfaceControl = nullptr; |
| 271 | } |
| 272 | if ((clientState.what & layer_state_t::eLayerChanged || |
| 273 | (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) && |
| 274 | isRelativeOf) { |
| 275 | // clear out relz data |
| 276 | relativeParentId = UNASSIGNED_LAYER_ID; |
| 277 | isRelativeOf = false; |
| 278 | changes |= RequestedLayerState::Changes::RelativeParent; |
| 279 | } |
| 280 | if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) { |
| 281 | // provide a hint that we are are now a direct child and not a relative child. |
| 282 | changes |= RequestedLayerState::Changes::RelativeParent; |
| 283 | } |
| 284 | if (clientState.what & layer_state_t::eInputInfoChanged) { |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 285 | touchCropId = resolvedComposerState.touchCropId; |
| 286 | windowInfoHandle->editInfo()->touchableRegionCropHandle.clear(); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 287 | } |
| 288 | if (clientState.what & layer_state_t::eStretchChanged) { |
| 289 | stretchEffect.sanitize(); |
| 290 | } |
| 291 | |
| 292 | if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) { |
| 293 | // TODO(b/238781169) handle callbacks |
| 294 | } |
| 295 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 296 | if (clientState.what & layer_state_t::ePositionChanged) { |
| 297 | requestedTransform.set(x, y); |
| 298 | } |
| 299 | |
| 300 | if (clientState.what & layer_state_t::eMatrixChanged) { |
| 301 | requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy); |
| 302 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 303 | if (clientState.what & layer_state_t::eMetadataChanged) { |
| 304 | const int32_t requestedGameMode = |
| 305 | clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1); |
| 306 | if (requestedGameMode != -1) { |
| 307 | // The transaction will be received on the Task layer and needs to be applied to all |
| 308 | // child layers. |
| 309 | if (static_cast<int32_t>(gameMode) != requestedGameMode) { |
| 310 | gameMode = static_cast<gui::GameMode>(requestedGameMode); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 311 | changes |= RequestedLayerState::Changes::GameMode; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | if (clientState.what & layer_state_t::eFrameRateChanged) { |
| 316 | const auto compatibility = |
| 317 | Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility); |
| 318 | const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy( |
| 319 | clientState.changeFrameRateStrategy); |
| 320 | requestedFrameRate = |
| 321 | Layer::FrameRate(Fps::fromValue(clientState.frameRate), compatibility, strategy); |
| 322 | changes |= RequestedLayerState::Changes::FrameRate; |
| 323 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 326 | ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const { |
| 327 | uint32_t bufferWidth = externalTexture->getWidth(); |
| 328 | uint32_t bufferHeight = externalTexture->getHeight(); |
| 329 | // Undo any transformations on the buffer. |
| 330 | if (bufferTransform & ui::Transform::ROT_90) { |
| 331 | std::swap(bufferWidth, bufferHeight); |
| 332 | } |
| 333 | if (transformToDisplayInverse) { |
| 334 | if (displayRotationFlags & ui::Transform::ROT_90) { |
| 335 | std::swap(bufferWidth, bufferHeight); |
| 336 | } |
| 337 | } |
| 338 | return {bufferWidth, bufferHeight}; |
| 339 | } |
| 340 | |
| 341 | ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 342 | if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) { |
| 343 | // If destination frame is not set, use the requested transform set via |
| 344 | // Transaction::setPosition and Transaction::setMatrix. |
| 345 | return requestedTransform; |
| 346 | } |
| 347 | |
| 348 | Rect destRect = destinationFrame; |
| 349 | int32_t destW = destRect.width(); |
| 350 | int32_t destH = destRect.height(); |
| 351 | if (destRect.left < 0) { |
| 352 | destRect.left = 0; |
| 353 | destRect.right = destW; |
| 354 | } |
| 355 | if (destRect.top < 0) { |
| 356 | destRect.top = 0; |
| 357 | destRect.bottom = destH; |
| 358 | } |
| 359 | |
| 360 | if (!externalTexture) { |
| 361 | ui::Transform transform; |
| 362 | transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top)); |
| 363 | return transform; |
| 364 | } |
| 365 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 366 | ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 367 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 368 | float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width); |
| 369 | float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 370 | ui::Transform transform; |
| 371 | transform.set(sx, 0, 0, sy); |
| 372 | transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top)); |
| 373 | return transform; |
| 374 | } |
| 375 | |
| 376 | std::string RequestedLayerState::getDebugString() const { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 377 | std::stringstream debug; |
Vishnu Nair | 444f395 | 2023-04-11 13:01:02 -0700 | [diff] [blame] | 378 | debug << "RequestedLayerState{" << name; |
| 379 | if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId; |
| 380 | if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId; |
| 381 | if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds); |
| 382 | if (!handleAlive) debug << " !handle"; |
| 383 | if (z != 0) debug << " z=" << z; |
| 384 | if (layerStack.id != 0) debug << " layerStack=" << layerStack.id; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 385 | return debug.str(); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 388 | std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) { |
| 389 | out << obj.debugName; |
| 390 | if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId; |
| 391 | if (!obj.handleAlive) out << " handleNotAlive"; |
| 392 | return out; |
| 393 | } |
| 394 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 395 | std::string RequestedLayerState::getDebugStringShort() const { |
| 396 | return "[" + std::to_string(id) + "]" + name; |
| 397 | } |
| 398 | |
| 399 | bool RequestedLayerState::canBeDestroyed() const { |
| 400 | return !handleAlive && parentId == UNASSIGNED_LAYER_ID; |
| 401 | } |
| 402 | bool RequestedLayerState::isRoot() const { |
| 403 | return canBeRoot && parentId == UNASSIGNED_LAYER_ID; |
| 404 | } |
| 405 | bool RequestedLayerState::isHiddenByPolicy() const { |
| 406 | return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden; |
| 407 | }; |
| 408 | half4 RequestedLayerState::getColor() const { |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 409 | if (sidebandStream || externalTexture) { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 410 | return {0._hf, 0._hf, 0._hf, color.a}; |
| 411 | } |
| 412 | return color; |
| 413 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 414 | Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 415 | // for buffer state layers we use the display frame size as the buffer size. |
| 416 | if (!externalTexture) { |
| 417 | return Rect::INVALID_RECT; |
| 418 | } |
| 419 | |
| 420 | uint32_t bufWidth = externalTexture->getWidth(); |
| 421 | uint32_t bufHeight = externalTexture->getHeight(); |
| 422 | |
| 423 | // Undo any transformations on the buffer and return the result. |
| 424 | if (bufferTransform & ui::Transform::ROT_90) { |
| 425 | std::swap(bufWidth, bufHeight); |
| 426 | } |
| 427 | |
| 428 | if (transformToDisplayInverse) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 429 | uint32_t invTransform = displayRotationFlags; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 430 | if (invTransform & ui::Transform::ROT_90) { |
| 431 | std::swap(bufWidth, bufHeight); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight)); |
| 436 | } |
| 437 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 438 | Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const { |
| 439 | Rect size = bufferSize; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 440 | if (!crop.isEmpty() && size.isValid()) { |
| 441 | size.intersect(crop, &size); |
| 442 | } else if (!crop.isEmpty()) { |
| 443 | size = crop; |
| 444 | } |
| 445 | return size; |
| 446 | } |
| 447 | |
| 448 | Rect RequestedLayerState::getBufferCrop() const { |
| 449 | // this is the crop rectangle that applies to the buffer |
| 450 | // itself (as opposed to the window) |
| 451 | if (!bufferCrop.isEmpty()) { |
| 452 | // if the buffer crop is defined, we use that |
| 453 | return bufferCrop; |
| 454 | } else if (externalTexture != nullptr) { |
| 455 | // otherwise we use the whole buffer |
| 456 | return externalTexture->getBounds(); |
| 457 | } else { |
| 458 | // if we don't have a buffer yet, we use an empty/invalid crop |
| 459 | return Rect(); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType() |
| 464 | const { |
| 465 | using aidl::android::hardware::graphics::composer3::Composition; |
| 466 | // TODO(b/238781169) check about sidestream ready flag |
| 467 | if (sidebandStream.get()) { |
| 468 | return Composition::SIDEBAND; |
| 469 | } |
| 470 | if (!externalTexture) { |
| 471 | return Composition::SOLID_COLOR; |
| 472 | } |
| 473 | if (flags & layer_state_t::eLayerIsDisplayDecoration) { |
| 474 | return Composition::DISPLAY_DECORATION; |
| 475 | } |
| 476 | if (potentialCursor) { |
| 477 | return Composition::CURSOR; |
| 478 | } |
| 479 | return Composition::DEVICE; |
| 480 | } |
| 481 | |
| 482 | Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) { |
| 483 | if (CC_LIKELY(exclude.isEmpty())) { |
| 484 | return win; |
| 485 | } |
| 486 | if (exclude.isRect()) { |
| 487 | return win.reduce(exclude.getBounds()); |
| 488 | } |
| 489 | return Region(win).subtract(exclude).getBounds(); |
| 490 | } |
| 491 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 492 | // Returns true if the layer has a relative parent that is not its own parent. This is an input |
| 493 | // error from the client, and this check allows us to handle it gracefully. If both parentId and |
| 494 | // relativeParentId is unassigned then the layer does not have a valid relative parent. |
| 495 | // If the relative parentid is unassigned, the layer will be considered relative but won't be |
| 496 | // reachable. |
| 497 | bool RequestedLayerState::hasValidRelativeParent() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 498 | return isRelativeOf && |
| 499 | (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 502 | bool RequestedLayerState::hasInputInfo() const { |
| 503 | if (!windowInfoHandle) { |
| 504 | return false; |
| 505 | } |
| 506 | const auto windowInfo = windowInfoHandle->getInfo(); |
| 507 | return windowInfo->token != nullptr || |
| 508 | windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
| 509 | } |
| 510 | |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 511 | bool RequestedLayerState::hasBlur() const { |
| 512 | return backgroundBlurRadius > 0 || blurRegions.size() > 0; |
| 513 | } |
| 514 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 515 | bool RequestedLayerState::hasFrameUpdate() const { |
| 516 | return what & layer_state_t::CONTENT_DIRTY && |
| 517 | (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID); |
| 518 | } |
| 519 | |
| 520 | bool RequestedLayerState::hasReadyFrame() const { |
| 521 | return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh; |
| 522 | } |
| 523 | |
| 524 | bool RequestedLayerState::hasSidebandStreamFrame() const { |
| 525 | return hasFrameUpdate() && sidebandStream.get(); |
| 526 | } |
| 527 | |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 528 | bool RequestedLayerState::willReleaseBufferOnLatch() const { |
| 529 | return changes.test(Changes::Buffer) && !externalTexture; |
| 530 | } |
| 531 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 532 | bool RequestedLayerState::backpressureEnabled() const { |
| 533 | return flags & layer_state_t::eEnableBackpressure; |
| 534 | } |
| 535 | |
| 536 | bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const { |
| 537 | static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged; |
| 538 | if ((s.what & requiredFlags) != requiredFlags) { |
| 539 | ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__, |
| 540 | (s.what | requiredFlags) & ~s.what); |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect | |
| 545 | layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged | |
| 546 | layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged | |
| 547 | layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged | |
| 548 | layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent; |
| 549 | if (s.what & deniedFlags) { |
| 550 | ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__, |
| 551 | s.what & deniedFlags); |
| 552 | return false; |
| 553 | } |
| 554 | |
| 555 | bool changedFlags = diff(s); |
| 556 | static constexpr auto deniedChanges = layer_state_t::ePositionChanged | |
| 557 | layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged | |
| 558 | layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged | |
| 559 | layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged | |
| 560 | layer_state_t::eBufferTransformChanged | |
| 561 | layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged | |
| 562 | layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged | |
| 563 | layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged | |
| 564 | layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged | |
| 565 | layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged | |
| 566 | layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged | |
| 567 | layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged; |
| 568 | if (changedFlags & deniedChanges) { |
| 569 | ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__, |
| 570 | s.what & deniedChanges); |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | return true; |
| 575 | } |
| 576 | |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 577 | bool RequestedLayerState::isProtected() const { |
| 578 | return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED; |
| 579 | } |
| 580 | |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame^] | 581 | bool RequestedLayerState::hasSomethingToDraw() const { |
| 582 | return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f || |
| 583 | backgroundBlurRadius > 0 || blurRegions.size() > 0 || |
| 584 | (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf); |
| 585 | } |
| 586 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 587 | void RequestedLayerState::clearChanges() { |
| 588 | what = 0; |
| 589 | changes.clear(); |
| 590 | } |
| 591 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 592 | } // namespace android::surfaceflinger::frontend |