Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +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 | */ |
| 16 | |
| 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "LayerSnapshot" |
| 20 | |
| 21 | #include "LayerSnapshot.h" |
| 22 | |
| 23 | namespace android::surfaceflinger::frontend { |
| 24 | |
| 25 | using namespace ftl::flag_operators; |
| 26 | |
| 27 | LayerSnapshot::LayerSnapshot(const RequestedLayerState& state, |
| 28 | const LayerHierarchy::TraversalPath& path) |
| 29 | : path(path) { |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 30 | // Provide a unique id for all snapshots. |
| 31 | // A front end layer can generate multiple snapshots if its mirrored. |
| 32 | // Additionally, if the layer is not reachable, we may choose to destroy |
| 33 | // and recreate the snapshot in which case the unique sequence id will |
| 34 | // change. The consumer shouldn't tie any lifetimes to this unique id but |
| 35 | // register a LayerLifecycleManager::ILifecycleListener or get a list of |
| 36 | // destroyed layers from LayerLifecycleManager. |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 37 | if (path.isClone()) { |
| 38 | uniqueSequence = |
| 39 | LayerCreationArgs::getInternalLayerId(LayerCreationArgs::sInternalSequence++); |
| 40 | } else { |
| 41 | uniqueSequence = state.id; |
| 42 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 43 | sequence = static_cast<int32_t>(state.id); |
| 44 | name = state.name; |
| 45 | textureName = state.textureName; |
| 46 | premultipliedAlpha = state.premultipliedAlpha; |
| 47 | inputInfo.name = state.name; |
Vishnu Nair | 93b8b79 | 2023-02-27 19:40:24 +0000 | [diff] [blame] | 48 | inputInfo.id = static_cast<int32_t>(uniqueSequence); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 49 | inputInfo.ownerUid = gui::Uid{state.ownerUid}; |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 50 | inputInfo.ownerPid = gui::Pid{state.ownerPid}; |
Vishnu Nair | 36d5f8e | 2023-03-19 13:31:35 -0700 | [diff] [blame] | 51 | uid = state.ownerUid; |
| 52 | pid = state.ownerPid; |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 53 | changes = RequestedLayerState::Changes::Created; |
| 54 | mirrorRootPath = path.variant == LayerHierarchy::Variant::Mirror |
| 55 | ? path |
| 56 | : LayerHierarchy::TraversalPath::ROOT; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // As documented in libhardware header, formats in the range |
| 60 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 61 | // are known to have no alpha channel |
| 62 | // TODO: move definition for device-specific range into |
| 63 | // hardware.h, instead of using hard-coded values here. |
| 64 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 65 | |
| 66 | bool LayerSnapshot::isOpaqueFormat(PixelFormat format) { |
| 67 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 68 | return true; |
| 69 | } |
| 70 | switch (format) { |
| 71 | case PIXEL_FORMAT_RGBA_8888: |
| 72 | case PIXEL_FORMAT_BGRA_8888: |
| 73 | case PIXEL_FORMAT_RGBA_FP16: |
| 74 | case PIXEL_FORMAT_RGBA_1010102: |
| 75 | case PIXEL_FORMAT_R_8: |
| 76 | return false; |
| 77 | } |
| 78 | // in all other case, we have no blending (also for unknown formats) |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool LayerSnapshot::hasBufferOrSidebandStream() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 83 | return ((sidebandStream != nullptr) || (externalTexture != nullptr)); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool LayerSnapshot::drawShadows() const { |
| 87 | return shadowSettings.length > 0.f; |
| 88 | } |
| 89 | |
| 90 | bool LayerSnapshot::fillsColor() const { |
| 91 | return !hasBufferOrSidebandStream() && color.r >= 0.0_hf && color.g >= 0.0_hf && |
| 92 | color.b >= 0.0_hf; |
| 93 | } |
| 94 | |
| 95 | bool LayerSnapshot::hasBlur() const { |
| 96 | return backgroundBlurRadius > 0 || blurRegions.size() > 0; |
| 97 | } |
| 98 | |
| 99 | bool LayerSnapshot::hasEffect() const { |
| 100 | return fillsColor() || drawShadows() || hasBlur(); |
| 101 | } |
| 102 | |
| 103 | bool LayerSnapshot::hasSomethingToDraw() const { |
| 104 | return hasEffect() || hasBufferOrSidebandStream(); |
| 105 | } |
| 106 | |
| 107 | bool LayerSnapshot::isContentOpaque() const { |
| 108 | // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the |
| 109 | // layer's opaque flag. |
| 110 | if (!hasSomethingToDraw()) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | // if the layer has the opaque flag, then we're always opaque |
| 115 | if (layerOpaqueFlagSet) { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | // If the buffer has no alpha channel, then we are opaque |
| 120 | if (hasBufferOrSidebandStream() && |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 121 | isOpaqueFormat(externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE)) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
| 125 | // Lastly consider the layer opaque if drawing a color with alpha == 1.0 |
| 126 | return fillsColor() && color.a == 1.0_hf; |
| 127 | } |
| 128 | |
| 129 | bool LayerSnapshot::isHiddenByPolicy() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 130 | return invalidTransform || isHiddenByPolicyFromParent || isHiddenByPolicyFromRelativeParent; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | bool LayerSnapshot::getIsVisible() const { |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 134 | if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) { |
| 135 | return false; |
| 136 | } |
| 137 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 138 | if (!hasSomethingToDraw()) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | if (isHiddenByPolicy()) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | return color.a > 0.0f || hasBlur(); |
| 147 | } |
| 148 | |
| 149 | std::string LayerSnapshot::getIsVisibleReason() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 150 | // not visible |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 151 | if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) return "eLayerSkipScreenshot"; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 152 | if (!hasSomethingToDraw()) return "!hasSomethingToDraw"; |
| 153 | if (invalidTransform) return "invalidTransform"; |
| 154 | if (isHiddenByPolicyFromParent) return "hidden by parent or layer flag"; |
| 155 | if (isHiddenByPolicyFromRelativeParent) return "hidden by relative parent"; |
| 156 | if (color.a == 0.0f && !hasBlur()) return "alpha = 0 and no blur"; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 157 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 158 | // visible |
| 159 | std::stringstream reason; |
| 160 | if (sidebandStream != nullptr) reason << " sidebandStream"; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 161 | if (externalTexture != nullptr) |
| 162 | reason << " buffer:" << externalTexture->getId() << " frame:" << frameNumber; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 163 | if (fillsColor() || color.a > 0.0f) reason << " color{" << color << "}"; |
| 164 | if (drawShadows()) reason << " shadowSettings.length=" << shadowSettings.length; |
| 165 | if (backgroundBlurRadius > 0) reason << " backgroundBlurRadius=" << backgroundBlurRadius; |
| 166 | if (blurRegions.size() > 0) reason << " blurRegions.size()=" << blurRegions.size(); |
| 167 | return reason.str(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | bool LayerSnapshot::canReceiveInput() const { |
| 171 | return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f); |
| 172 | } |
| 173 | |
| 174 | bool LayerSnapshot::isTransformValid(const ui::Transform& t) { |
| 175 | float transformDet = t.det(); |
| 176 | return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet); |
| 177 | } |
| 178 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 179 | bool LayerSnapshot::hasInputInfo() const { |
| 180 | return inputInfo.token != nullptr || |
| 181 | inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
| 182 | } |
| 183 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 184 | std::string LayerSnapshot::getDebugString() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 185 | std::stringstream debug; |
| 186 | debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {" |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 187 | << getIsVisibleReason() << "} changes=" << changes.string() |
Vishnu Nair | 36d5f8e | 2023-03-19 13:31:35 -0700 | [diff] [blame] | 188 | << " layerStack=" << outputFilter.layerStack.id << " geomLayerBounds={" |
| 189 | << geomLayerBounds.left << "," << geomLayerBounds.top << "," << geomLayerBounds.bottom |
| 190 | << "," << geomLayerBounds.right << "}" |
| 191 | << " geomLayerTransform={tx=" << geomLayerTransform.tx() |
| 192 | << ",ty=" << geomLayerTransform.ty() << "}" |
| 193 | << "}"; |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 194 | debug << " input{ touchCropId=" << touchCropId |
| 195 | << " replaceTouchableRegionWithCrop=" << inputInfo.replaceTouchableRegionWithCrop << "}"; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 196 | return debug.str(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Vishnu Nair | 781d725 | 2023-01-30 18:16:01 +0000 | [diff] [blame] | 199 | FloatRect LayerSnapshot::sourceBounds() const { |
| 200 | if (!externalTexture) { |
| 201 | return geomLayerBounds; |
| 202 | } |
| 203 | return geomBufferSize.toFloatRect(); |
| 204 | } |
| 205 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 206 | } // namespace android::surfaceflinger::frontend |