blob: 4e695653e71e8bf7f47f108995296e421c04fe62 [file] [log] [blame]
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001/*
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
23namespace android::surfaceflinger::frontend {
24
25using namespace ftl::flag_operators;
26
27LayerSnapshot::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
47bool 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
63bool LayerSnapshot::hasBufferOrSidebandStream() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +000064 return ((sidebandStream != nullptr) || (externalTexture != nullptr));
Vishnu Nair8fc721b2022-12-22 20:06:32 +000065}
66
67bool LayerSnapshot::drawShadows() const {
68 return shadowSettings.length > 0.f;
69}
70
71bool LayerSnapshot::fillsColor() const {
72 return !hasBufferOrSidebandStream() && color.r >= 0.0_hf && color.g >= 0.0_hf &&
73 color.b >= 0.0_hf;
74}
75
76bool LayerSnapshot::hasBlur() const {
77 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
78}
79
80bool LayerSnapshot::hasEffect() const {
81 return fillsColor() || drawShadows() || hasBlur();
82}
83
84bool LayerSnapshot::hasSomethingToDraw() const {
85 return hasEffect() || hasBufferOrSidebandStream();
86}
87
88bool 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 Naircfb2d252023-01-19 04:44:02 +0000102 isOpaqueFormat(externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000103 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
110bool LayerSnapshot::isHiddenByPolicy() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000111 return invalidTransform || isHiddenByPolicyFromParent || isHiddenByPolicyFromRelativeParent;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000112}
113
114bool LayerSnapshot::getIsVisible() const {
Vishnu Naira9c43762023-01-27 19:10:25 +0000115 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) {
116 return false;
117 }
118
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000119 if (!hasSomethingToDraw()) {
120 return false;
121 }
122
123 if (isHiddenByPolicy()) {
124 return false;
125 }
126
127 return color.a > 0.0f || hasBlur();
128}
129
130std::string LayerSnapshot::getIsVisibleReason() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000131 // not visible
Vishnu Naira9c43762023-01-27 19:10:25 +0000132 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) return "eLayerSkipScreenshot";
Vishnu Naircfb2d252023-01-19 04:44:02 +0000133 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 Nair8fc721b2022-12-22 20:06:32 +0000138
Vishnu Naircfb2d252023-01-19 04:44:02 +0000139 // 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 Nair8fc721b2022-12-22 20:06:32 +0000148}
149
150bool LayerSnapshot::canReceiveInput() const {
151 return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f);
152}
153
154bool LayerSnapshot::isTransformValid(const ui::Transform& t) {
155 float transformDet = t.det();
156 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
157}
158
Vishnu Naircfb2d252023-01-19 04:44:02 +0000159bool LayerSnapshot::hasInputInfo() const {
160 return inputInfo.token != nullptr ||
161 inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
162}
163
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000164std::string LayerSnapshot::getDebugString() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000165 std::stringstream debug;
166 debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {"
167 << getIsVisibleReason() << "} changes=" << changes.string() << "}";
168 return debug.str();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000169}
170
171} // namespace android::surfaceflinger::frontend