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) { |
| 30 | sequence = static_cast<int32_t>(state.id); |
| 31 | name = state.name; |
| 32 | textureName = state.textureName; |
| 33 | premultipliedAlpha = state.premultipliedAlpha; |
| 34 | inputInfo.name = state.name; |
| 35 | inputInfo.id = static_cast<int32_t>(state.id); |
| 36 | inputInfo.ownerUid = static_cast<int32_t>(state.ownerUid); |
| 37 | inputInfo.ownerPid = state.ownerPid; |
| 38 | } |
| 39 | |
| 40 | // As documented in libhardware header, formats in the range |
| 41 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 42 | // are known to have no alpha channel |
| 43 | // TODO: move definition for device-specific range into |
| 44 | // hardware.h, instead of using hard-coded values here. |
| 45 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 46 | |
| 47 | bool LayerSnapshot::isOpaqueFormat(PixelFormat format) { |
| 48 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 49 | return true; |
| 50 | } |
| 51 | switch (format) { |
| 52 | case PIXEL_FORMAT_RGBA_8888: |
| 53 | case PIXEL_FORMAT_BGRA_8888: |
| 54 | case PIXEL_FORMAT_RGBA_FP16: |
| 55 | case PIXEL_FORMAT_RGBA_1010102: |
| 56 | case PIXEL_FORMAT_R_8: |
| 57 | return false; |
| 58 | } |
| 59 | // in all other case, we have no blending (also for unknown formats) |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool LayerSnapshot::hasBufferOrSidebandStream() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 64 | return ((sidebandStream != nullptr) || (externalTexture != nullptr)); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool LayerSnapshot::drawShadows() const { |
| 68 | return shadowSettings.length > 0.f; |
| 69 | } |
| 70 | |
| 71 | bool LayerSnapshot::fillsColor() const { |
| 72 | return !hasBufferOrSidebandStream() && color.r >= 0.0_hf && color.g >= 0.0_hf && |
| 73 | color.b >= 0.0_hf; |
| 74 | } |
| 75 | |
| 76 | bool LayerSnapshot::hasBlur() const { |
| 77 | return backgroundBlurRadius > 0 || blurRegions.size() > 0; |
| 78 | } |
| 79 | |
| 80 | bool LayerSnapshot::hasEffect() const { |
| 81 | return fillsColor() || drawShadows() || hasBlur(); |
| 82 | } |
| 83 | |
| 84 | bool LayerSnapshot::hasSomethingToDraw() const { |
| 85 | return hasEffect() || hasBufferOrSidebandStream(); |
| 86 | } |
| 87 | |
| 88 | bool LayerSnapshot::isContentOpaque() const { |
| 89 | // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the |
| 90 | // layer's opaque flag. |
| 91 | if (!hasSomethingToDraw()) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | // if the layer has the opaque flag, then we're always opaque |
| 96 | if (layerOpaqueFlagSet) { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | // If the buffer has no alpha channel, then we are opaque |
| 101 | if (hasBufferOrSidebandStream() && |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 102 | isOpaqueFormat(externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE)) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // Lastly consider the layer opaque if drawing a color with alpha == 1.0 |
| 107 | return fillsColor() && color.a == 1.0_hf; |
| 108 | } |
| 109 | |
| 110 | bool LayerSnapshot::isHiddenByPolicy() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 111 | return invalidTransform || isHiddenByPolicyFromParent || isHiddenByPolicyFromRelativeParent; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool LayerSnapshot::getIsVisible() const { |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame^] | 115 | if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) { |
| 116 | return false; |
| 117 | } |
| 118 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 119 | if (!hasSomethingToDraw()) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if (isHiddenByPolicy()) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return color.a > 0.0f || hasBlur(); |
| 128 | } |
| 129 | |
| 130 | std::string LayerSnapshot::getIsVisibleReason() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 131 | // not visible |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame^] | 132 | if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) return "eLayerSkipScreenshot"; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 133 | if (!hasSomethingToDraw()) return "!hasSomethingToDraw"; |
| 134 | if (invalidTransform) return "invalidTransform"; |
| 135 | if (isHiddenByPolicyFromParent) return "hidden by parent or layer flag"; |
| 136 | if (isHiddenByPolicyFromRelativeParent) return "hidden by relative parent"; |
| 137 | if (color.a == 0.0f && !hasBlur()) return "alpha = 0 and no blur"; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 138 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 139 | // visible |
| 140 | std::stringstream reason; |
| 141 | if (sidebandStream != nullptr) reason << " sidebandStream"; |
| 142 | if (externalTexture != nullptr) reason << " buffer"; |
| 143 | if (fillsColor() || color.a > 0.0f) reason << " color{" << color << "}"; |
| 144 | if (drawShadows()) reason << " shadowSettings.length=" << shadowSettings.length; |
| 145 | if (backgroundBlurRadius > 0) reason << " backgroundBlurRadius=" << backgroundBlurRadius; |
| 146 | if (blurRegions.size() > 0) reason << " blurRegions.size()=" << blurRegions.size(); |
| 147 | return reason.str(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | bool LayerSnapshot::canReceiveInput() const { |
| 151 | return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f); |
| 152 | } |
| 153 | |
| 154 | bool LayerSnapshot::isTransformValid(const ui::Transform& t) { |
| 155 | float transformDet = t.det(); |
| 156 | return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet); |
| 157 | } |
| 158 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 159 | bool LayerSnapshot::hasInputInfo() const { |
| 160 | return inputInfo.token != nullptr || |
| 161 | inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
| 162 | } |
| 163 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 164 | std::string LayerSnapshot::getDebugString() const { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 165 | std::stringstream debug; |
| 166 | debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {" |
| 167 | << getIsVisibleReason() << "} changes=" << changes.string() << "}"; |
| 168 | return debug.str(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } // namespace android::surfaceflinger::frontend |