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