blob: 5e7c25946b29c0cfc621588f0c45a309bee0e714 [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) {
Vishnu Nair93b8b792023-02-27 19:40:24 +000030 static uint32_t sUniqueSequenceId = 0;
Vishnu Naird0183602023-03-16 18:52:15 +000031 // 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 Nair8fc721b2022-12-22 20:06:32 +000039 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 Nair93b8b792023-02-27 19:40:24 +000044 inputInfo.id = static_cast<int32_t>(uniqueSequence);
Vishnu Nair8fc721b2022-12-22 20:06:32 +000045 inputInfo.ownerUid = static_cast<int32_t>(state.ownerUid);
46 inputInfo.ownerPid = state.ownerPid;
Vishnu Nair92990e22023-02-24 20:01:05 +000047 changes = RequestedLayerState::Changes::Created;
48 mirrorRootPath = path.variant == LayerHierarchy::Variant::Mirror
49 ? path
50 : LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000051}
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
60bool 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
76bool LayerSnapshot::hasBufferOrSidebandStream() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +000077 return ((sidebandStream != nullptr) || (externalTexture != nullptr));
Vishnu Nair8fc721b2022-12-22 20:06:32 +000078}
79
80bool LayerSnapshot::drawShadows() const {
81 return shadowSettings.length > 0.f;
82}
83
84bool LayerSnapshot::fillsColor() const {
85 return !hasBufferOrSidebandStream() && color.r >= 0.0_hf && color.g >= 0.0_hf &&
86 color.b >= 0.0_hf;
87}
88
89bool LayerSnapshot::hasBlur() const {
90 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
91}
92
93bool LayerSnapshot::hasEffect() const {
94 return fillsColor() || drawShadows() || hasBlur();
95}
96
97bool LayerSnapshot::hasSomethingToDraw() const {
98 return hasEffect() || hasBufferOrSidebandStream();
99}
100
101bool 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 Naircfb2d252023-01-19 04:44:02 +0000115 isOpaqueFormat(externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000116 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
123bool LayerSnapshot::isHiddenByPolicy() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000124 return invalidTransform || isHiddenByPolicyFromParent || isHiddenByPolicyFromRelativeParent;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000125}
126
127bool LayerSnapshot::getIsVisible() const {
Vishnu Naira9c43762023-01-27 19:10:25 +0000128 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) {
129 return false;
130 }
131
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000132 if (!hasSomethingToDraw()) {
133 return false;
134 }
135
136 if (isHiddenByPolicy()) {
137 return false;
138 }
139
140 return color.a > 0.0f || hasBlur();
141}
142
143std::string LayerSnapshot::getIsVisibleReason() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000144 // not visible
Vishnu Naira9c43762023-01-27 19:10:25 +0000145 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) return "eLayerSkipScreenshot";
Vishnu Naircfb2d252023-01-19 04:44:02 +0000146 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 Nair8fc721b2022-12-22 20:06:32 +0000151
Vishnu Naircfb2d252023-01-19 04:44:02 +0000152 // visible
153 std::stringstream reason;
154 if (sidebandStream != nullptr) reason << " sidebandStream";
Vishnu Naird47bcee2023-02-24 18:08:51 +0000155 if (externalTexture != nullptr)
156 reason << " buffer:" << externalTexture->getId() << " frame:" << frameNumber;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000157 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 Nair8fc721b2022-12-22 20:06:32 +0000162}
163
164bool LayerSnapshot::canReceiveInput() const {
165 return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f);
166}
167
168bool LayerSnapshot::isTransformValid(const ui::Transform& t) {
169 float transformDet = t.det();
170 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
171}
172
Vishnu Naircfb2d252023-01-19 04:44:02 +0000173bool LayerSnapshot::hasInputInfo() const {
174 return inputInfo.token != nullptr ||
175 inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
176}
177
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000178std::string LayerSnapshot::getDebugString() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000179 std::stringstream debug;
180 debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {"
Vishnu Nair92990e22023-02-24 20:01:05 +0000181 << getIsVisibleReason() << "} changes=" << changes.string()
182 << " layerStack=" << outputFilter.layerStack.id << "}";
Vishnu Naircfb2d252023-01-19 04:44:02 +0000183 return debug.str();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000184}
185
Vishnu Nair781d7252023-01-30 18:16:01 +0000186FloatRect LayerSnapshot::sourceBounds() const {
187 if (!externalTexture) {
188 return geomLayerBounds;
189 }
190 return geomBufferSize.toFloatRect();
191}
192
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000193} // namespace android::surfaceflinger::frontend