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