blob: 2d6d8ad0b08dcd19dadeae7c62a5d9a152731b27 [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 Nair36d5f8e2023-03-19 13:31:35 -070047 uid = state.ownerUid;
48 pid = state.ownerPid;
Vishnu Nair92990e22023-02-24 20:01:05 +000049 changes = RequestedLayerState::Changes::Created;
50 mirrorRootPath = path.variant == LayerHierarchy::Variant::Mirror
51 ? path
52 : LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000053}
54
55// As documented in libhardware header, formats in the range
56// 0x100 - 0x1FF are specific to the HAL implementation, and
57// are known to have no alpha channel
58// TODO: move definition for device-specific range into
59// hardware.h, instead of using hard-coded values here.
60#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
61
62bool LayerSnapshot::isOpaqueFormat(PixelFormat format) {
63 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
64 return true;
65 }
66 switch (format) {
67 case PIXEL_FORMAT_RGBA_8888:
68 case PIXEL_FORMAT_BGRA_8888:
69 case PIXEL_FORMAT_RGBA_FP16:
70 case PIXEL_FORMAT_RGBA_1010102:
71 case PIXEL_FORMAT_R_8:
72 return false;
73 }
74 // in all other case, we have no blending (also for unknown formats)
75 return true;
76}
77
78bool LayerSnapshot::hasBufferOrSidebandStream() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +000079 return ((sidebandStream != nullptr) || (externalTexture != nullptr));
Vishnu Nair8fc721b2022-12-22 20:06:32 +000080}
81
82bool LayerSnapshot::drawShadows() const {
83 return shadowSettings.length > 0.f;
84}
85
86bool LayerSnapshot::fillsColor() const {
87 return !hasBufferOrSidebandStream() && color.r >= 0.0_hf && color.g >= 0.0_hf &&
88 color.b >= 0.0_hf;
89}
90
91bool LayerSnapshot::hasBlur() const {
92 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
93}
94
95bool LayerSnapshot::hasEffect() const {
96 return fillsColor() || drawShadows() || hasBlur();
97}
98
99bool LayerSnapshot::hasSomethingToDraw() const {
100 return hasEffect() || hasBufferOrSidebandStream();
101}
102
103bool LayerSnapshot::isContentOpaque() const {
104 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
105 // layer's opaque flag.
106 if (!hasSomethingToDraw()) {
107 return false;
108 }
109
110 // if the layer has the opaque flag, then we're always opaque
111 if (layerOpaqueFlagSet) {
112 return true;
113 }
114
115 // If the buffer has no alpha channel, then we are opaque
116 if (hasBufferOrSidebandStream() &&
Vishnu Naircfb2d252023-01-19 04:44:02 +0000117 isOpaqueFormat(externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000118 return true;
119 }
120
121 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
122 return fillsColor() && color.a == 1.0_hf;
123}
124
125bool LayerSnapshot::isHiddenByPolicy() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000126 return invalidTransform || isHiddenByPolicyFromParent || isHiddenByPolicyFromRelativeParent;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000127}
128
129bool LayerSnapshot::getIsVisible() const {
Vishnu Naira9c43762023-01-27 19:10:25 +0000130 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) {
131 return false;
132 }
133
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000134 if (!hasSomethingToDraw()) {
135 return false;
136 }
137
138 if (isHiddenByPolicy()) {
139 return false;
140 }
141
142 return color.a > 0.0f || hasBlur();
143}
144
145std::string LayerSnapshot::getIsVisibleReason() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000146 // not visible
Vishnu Naira9c43762023-01-27 19:10:25 +0000147 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) return "eLayerSkipScreenshot";
Vishnu Naircfb2d252023-01-19 04:44:02 +0000148 if (!hasSomethingToDraw()) return "!hasSomethingToDraw";
149 if (invalidTransform) return "invalidTransform";
150 if (isHiddenByPolicyFromParent) return "hidden by parent or layer flag";
151 if (isHiddenByPolicyFromRelativeParent) return "hidden by relative parent";
152 if (color.a == 0.0f && !hasBlur()) return "alpha = 0 and no blur";
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000153
Vishnu Naircfb2d252023-01-19 04:44:02 +0000154 // visible
155 std::stringstream reason;
156 if (sidebandStream != nullptr) reason << " sidebandStream";
Vishnu Naird47bcee2023-02-24 18:08:51 +0000157 if (externalTexture != nullptr)
158 reason << " buffer:" << externalTexture->getId() << " frame:" << frameNumber;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000159 if (fillsColor() || color.a > 0.0f) reason << " color{" << color << "}";
160 if (drawShadows()) reason << " shadowSettings.length=" << shadowSettings.length;
161 if (backgroundBlurRadius > 0) reason << " backgroundBlurRadius=" << backgroundBlurRadius;
162 if (blurRegions.size() > 0) reason << " blurRegions.size()=" << blurRegions.size();
163 return reason.str();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000164}
165
166bool LayerSnapshot::canReceiveInput() const {
167 return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f);
168}
169
170bool LayerSnapshot::isTransformValid(const ui::Transform& t) {
171 float transformDet = t.det();
172 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
173}
174
Vishnu Naircfb2d252023-01-19 04:44:02 +0000175bool LayerSnapshot::hasInputInfo() const {
176 return inputInfo.token != nullptr ||
177 inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
178}
179
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000180std::string LayerSnapshot::getDebugString() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000181 std::stringstream debug;
182 debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {"
Vishnu Nair92990e22023-02-24 20:01:05 +0000183 << getIsVisibleReason() << "} changes=" << changes.string()
Vishnu Nair36d5f8e2023-03-19 13:31:35 -0700184 << " layerStack=" << outputFilter.layerStack.id << " geomLayerBounds={"
185 << geomLayerBounds.left << "," << geomLayerBounds.top << "," << geomLayerBounds.bottom
186 << "," << geomLayerBounds.right << "}"
187 << " geomLayerTransform={tx=" << geomLayerTransform.tx()
188 << ",ty=" << geomLayerTransform.ty() << "}"
189 << "}";
Vishnu Naircfb2d252023-01-19 04:44:02 +0000190 return debug.str();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000191}
192
Vishnu Nair781d7252023-01-30 18:16:01 +0000193FloatRect LayerSnapshot::sourceBounds() const {
194 if (!externalTexture) {
195 return geomLayerBounds;
196 }
197 return geomBufferSize.toFloatRect();
198}
199
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000200} // namespace android::surfaceflinger::frontend