blob: f0826c6db3e6aa802d205cc7be155477227b7bf4 [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
Vishnu Naira02943f2023-06-03 13:44:46 -070019#define LOG_TAG "SurfaceFlinger"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000020
21#include "LayerSnapshot.h"
22
23namespace android::surfaceflinger::frontend {
24
25using namespace ftl::flag_operators;
26
Vishnu Naira02943f2023-06-03 13:44:46 -070027namespace {
28
29void updateSurfaceDamage(const RequestedLayerState& requested, bool hasReadyFrame,
30 bool forceFullDamage, Region& outSurfaceDamageRegion) {
31 if (!hasReadyFrame) {
32 outSurfaceDamageRegion.clear();
33 return;
34 }
35 if (forceFullDamage) {
36 outSurfaceDamageRegion = Region::INVALID_REGION;
37 } else {
38 outSurfaceDamageRegion = requested.surfaceDamageRegion;
39 }
40}
41
42} // namespace
43
Vishnu Nair8fc721b2022-12-22 20:06:32 +000044LayerSnapshot::LayerSnapshot(const RequestedLayerState& state,
45 const LayerHierarchy::TraversalPath& path)
46 : path(path) {
Vishnu Naird0183602023-03-16 18:52:15 +000047 // Provide a unique id for all snapshots.
48 // A front end layer can generate multiple snapshots if its mirrored.
49 // Additionally, if the layer is not reachable, we may choose to destroy
50 // and recreate the snapshot in which case the unique sequence id will
51 // change. The consumer shouldn't tie any lifetimes to this unique id but
52 // register a LayerLifecycleManager::ILifecycleListener or get a list of
53 // destroyed layers from LayerLifecycleManager.
Vishnu Nair150065b2023-04-17 19:14:11 -070054 if (path.isClone()) {
55 uniqueSequence =
56 LayerCreationArgs::getInternalLayerId(LayerCreationArgs::sInternalSequence++);
57 } else {
58 uniqueSequence = state.id;
59 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +000060 sequence = static_cast<int32_t>(state.id);
61 name = state.name;
62 textureName = state.textureName;
63 premultipliedAlpha = state.premultipliedAlpha;
64 inputInfo.name = state.name;
Vishnu Nair93b8b792023-02-27 19:40:24 +000065 inputInfo.id = static_cast<int32_t>(uniqueSequence);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000066 inputInfo.ownerUid = gui::Uid{state.ownerUid};
Prabir Pradhane59c6dc2023-06-13 19:53:03 +000067 inputInfo.ownerPid = gui::Pid{state.ownerPid};
Vishnu Nair36d5f8e2023-03-19 13:31:35 -070068 uid = state.ownerUid;
69 pid = state.ownerPid;
Vishnu Nair92990e22023-02-24 20:01:05 +000070 changes = RequestedLayerState::Changes::Created;
Vishnu Naira02943f2023-06-03 13:44:46 -070071 clientChanges = 0;
Vishnu Nair92990e22023-02-24 20:01:05 +000072 mirrorRootPath = path.variant == LayerHierarchy::Variant::Mirror
73 ? path
74 : LayerHierarchy::TraversalPath::ROOT;
Vishnu Naira02943f2023-06-03 13:44:46 -070075 reachablilty = LayerSnapshot::Reachablilty::Unreachable;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000076}
77
78// As documented in libhardware header, formats in the range
79// 0x100 - 0x1FF are specific to the HAL implementation, and
80// are known to have no alpha channel
81// TODO: move definition for device-specific range into
82// hardware.h, instead of using hard-coded values here.
83#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
84
85bool LayerSnapshot::isOpaqueFormat(PixelFormat format) {
86 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
87 return true;
88 }
89 switch (format) {
90 case PIXEL_FORMAT_RGBA_8888:
91 case PIXEL_FORMAT_BGRA_8888:
92 case PIXEL_FORMAT_RGBA_FP16:
93 case PIXEL_FORMAT_RGBA_1010102:
94 case PIXEL_FORMAT_R_8:
95 return false;
96 }
97 // in all other case, we have no blending (also for unknown formats)
98 return true;
99}
100
101bool LayerSnapshot::hasBufferOrSidebandStream() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000102 return ((sidebandStream != nullptr) || (externalTexture != nullptr));
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000103}
104
105bool LayerSnapshot::drawShadows() const {
106 return shadowSettings.length > 0.f;
107}
108
109bool LayerSnapshot::fillsColor() const {
110 return !hasBufferOrSidebandStream() && color.r >= 0.0_hf && color.g >= 0.0_hf &&
111 color.b >= 0.0_hf;
112}
113
114bool LayerSnapshot::hasBlur() const {
115 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
116}
117
118bool LayerSnapshot::hasEffect() const {
119 return fillsColor() || drawShadows() || hasBlur();
120}
121
122bool LayerSnapshot::hasSomethingToDraw() const {
123 return hasEffect() || hasBufferOrSidebandStream();
124}
125
126bool LayerSnapshot::isContentOpaque() const {
127 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
128 // layer's opaque flag.
129 if (!hasSomethingToDraw()) {
130 return false;
131 }
132
133 // if the layer has the opaque flag, then we're always opaque
134 if (layerOpaqueFlagSet) {
135 return true;
136 }
137
138 // If the buffer has no alpha channel, then we are opaque
139 if (hasBufferOrSidebandStream() &&
Vishnu Naircfb2d252023-01-19 04:44:02 +0000140 isOpaqueFormat(externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000141 return true;
142 }
143
144 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
145 return fillsColor() && color.a == 1.0_hf;
146}
147
148bool LayerSnapshot::isHiddenByPolicy() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000149 return invalidTransform || isHiddenByPolicyFromParent || isHiddenByPolicyFromRelativeParent;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000150}
151
152bool LayerSnapshot::getIsVisible() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700153 if (reachablilty != LayerSnapshot::Reachablilty::Reachable) {
154 return false;
155 }
156
Vishnu Naira9c43762023-01-27 19:10:25 +0000157 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) {
158 return false;
159 }
160
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000161 if (!hasSomethingToDraw()) {
162 return false;
163 }
164
165 if (isHiddenByPolicy()) {
166 return false;
167 }
168
169 return color.a > 0.0f || hasBlur();
170}
171
172std::string LayerSnapshot::getIsVisibleReason() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000173 // not visible
Vishnu Naira02943f2023-06-03 13:44:46 -0700174 if (reachablilty == LayerSnapshot::Reachablilty::Unreachable)
175 return "layer not reachable from root";
176 if (reachablilty == LayerSnapshot::Reachablilty::ReachableByRelativeParent)
177 return "layer only reachable via relative parent";
Vishnu Naircfb2d252023-01-19 04:44:02 +0000178 if (isHiddenByPolicyFromParent) return "hidden by parent or layer flag";
179 if (isHiddenByPolicyFromRelativeParent) return "hidden by relative parent";
Vishnu Naira02943f2023-06-03 13:44:46 -0700180 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) return "eLayerSkipScreenshot";
181 if (invalidTransform) return "invalidTransform";
Vishnu Naircfb2d252023-01-19 04:44:02 +0000182 if (color.a == 0.0f && !hasBlur()) return "alpha = 0 and no blur";
Vishnu Naira02943f2023-06-03 13:44:46 -0700183 if (!hasSomethingToDraw()) return "!hasSomethingToDraw";
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000184
Vishnu Naircfb2d252023-01-19 04:44:02 +0000185 // visible
186 std::stringstream reason;
187 if (sidebandStream != nullptr) reason << " sidebandStream";
Vishnu Naird47bcee2023-02-24 18:08:51 +0000188 if (externalTexture != nullptr)
189 reason << " buffer:" << externalTexture->getId() << " frame:" << frameNumber;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000190 if (fillsColor() || color.a > 0.0f) reason << " color{" << color << "}";
191 if (drawShadows()) reason << " shadowSettings.length=" << shadowSettings.length;
192 if (backgroundBlurRadius > 0) reason << " backgroundBlurRadius=" << backgroundBlurRadius;
193 if (blurRegions.size() > 0) reason << " blurRegions.size()=" << blurRegions.size();
194 return reason.str();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000195}
196
197bool LayerSnapshot::canReceiveInput() const {
198 return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f);
199}
200
201bool LayerSnapshot::isTransformValid(const ui::Transform& t) {
202 float transformDet = t.det();
203 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
204}
205
Vishnu Naircfb2d252023-01-19 04:44:02 +0000206bool LayerSnapshot::hasInputInfo() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700207 return (inputInfo.token != nullptr ||
208 inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) &&
209 reachablilty == Reachablilty::Reachable;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000210}
211
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000212std::string LayerSnapshot::getDebugString() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000213 std::stringstream debug;
214 debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {"
Vishnu Nair92990e22023-02-24 20:01:05 +0000215 << getIsVisibleReason() << "} changes=" << changes.string()
Vishnu Nair36d5f8e2023-03-19 13:31:35 -0700216 << " layerStack=" << outputFilter.layerStack.id << " geomLayerBounds={"
217 << geomLayerBounds.left << "," << geomLayerBounds.top << "," << geomLayerBounds.bottom
218 << "," << geomLayerBounds.right << "}"
219 << " geomLayerTransform={tx=" << geomLayerTransform.tx()
220 << ",ty=" << geomLayerTransform.ty() << "}"
221 << "}";
Vishnu Naira02943f2023-06-03 13:44:46 -0700222 if (hasInputInfo()) {
223 debug << " input{"
224 << "(" << inputInfo.inputConfig.string() << ")";
225 if (touchCropId != UNASSIGNED_LAYER_ID) debug << " touchCropId=" << touchCropId;
226 if (inputInfo.replaceTouchableRegionWithCrop) debug << " replaceTouchableRegionWithCrop";
227 auto touchableRegion = inputInfo.touchableRegion.getBounds();
228 debug << " touchableRegion={" << touchableRegion.left << "," << touchableRegion.top << ","
229 << touchableRegion.bottom << "," << touchableRegion.right << "}"
230 << "}";
231 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000232 return debug.str();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000233}
234
Vishnu Nair781d7252023-01-30 18:16:01 +0000235FloatRect LayerSnapshot::sourceBounds() const {
236 if (!externalTexture) {
237 return geomLayerBounds;
238 }
239 return geomBufferSize.toFloatRect();
240}
241
Vishnu Naira02943f2023-06-03 13:44:46 -0700242Hwc2::IComposerClient::BlendMode LayerSnapshot::getBlendMode(
243 const RequestedLayerState& requested) const {
244 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
245 if (alpha != 1.0f || !contentOpaque) {
246 blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
247 : Hwc2::IComposerClient::BlendMode::COVERAGE;
248 }
249 return blendMode;
250}
251
252void LayerSnapshot::merge(const RequestedLayerState& requested, bool forceUpdate,
253 bool displayChanges, bool forceFullDamage,
254 uint32_t displayRotationFlags) {
255 clientChanges = requested.what;
256 changes = requested.changes;
257 contentDirty = requested.what & layer_state_t::CONTENT_DIRTY;
258 // TODO(b/238781169) scope down the changes to only buffer updates.
259 hasReadyFrame = requested.hasReadyFrame();
260 sidebandStreamHasFrame = requested.hasSidebandStreamFrame();
261 updateSurfaceDamage(requested, hasReadyFrame, forceFullDamage, surfaceDamage);
262
263 if (forceUpdate || requested.what & layer_state_t::eTransparentRegionChanged) {
264 transparentRegionHint = requested.transparentRegion;
265 }
266 if (forceUpdate || requested.what & layer_state_t::eFlagsChanged) {
267 layerOpaqueFlagSet =
268 (requested.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque;
269 }
270 if (forceUpdate || requested.what & layer_state_t::eBufferTransformChanged) {
271 geomBufferTransform = requested.bufferTransform;
272 }
273 if (forceUpdate || requested.what & layer_state_t::eTransformToDisplayInverseChanged) {
274 geomBufferUsesDisplayInverseTransform = requested.transformToDisplayInverse;
275 }
276 if (forceUpdate || requested.what & layer_state_t::eDataspaceChanged) {
277 dataspace = requested.dataspace;
278 }
279 if (forceUpdate || requested.what & layer_state_t::eExtendedRangeBrightnessChanged) {
280 currentHdrSdrRatio = requested.currentHdrSdrRatio;
281 desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
282 }
283 if (forceUpdate || requested.what & layer_state_t::eCachingHintChanged) {
284 cachingHint = requested.cachingHint;
285 }
286 if (forceUpdate || requested.what & layer_state_t::eHdrMetadataChanged) {
287 hdrMetadata = requested.hdrMetadata;
288 }
289 if (forceUpdate || requested.what & layer_state_t::eSidebandStreamChanged) {
290 sidebandStream = requested.sidebandStream;
291 }
292 if (forceUpdate || requested.what & layer_state_t::eShadowRadiusChanged) {
293 shadowRadius = requested.shadowRadius;
294 shadowSettings.length = requested.shadowRadius;
295 }
296 if (forceUpdate || requested.what & layer_state_t::eFrameRateSelectionPriority) {
297 frameRateSelectionPriority = requested.frameRateSelectionPriority;
298 }
299 if (forceUpdate || requested.what & layer_state_t::eColorSpaceAgnosticChanged) {
300 isColorspaceAgnostic = requested.colorSpaceAgnostic;
301 }
302 if (forceUpdate || requested.what & layer_state_t::eDimmingEnabledChanged) {
303 dimmingEnabled = requested.dimmingEnabled;
304 }
305 if (forceUpdate || requested.what & layer_state_t::eCropChanged) {
306 geomCrop = requested.crop;
307 }
308
309 if (forceUpdate ||
310 requested.what &
311 (layer_state_t::eFlagsChanged | layer_state_t::eBufferChanged |
312 layer_state_t::eSidebandStreamChanged)) {
313 compositionType = requested.getCompositionType();
314 }
315
316 if (forceUpdate || requested.what & layer_state_t::eInputInfoChanged) {
317 if (requested.windowInfoHandle) {
318 inputInfo = *requested.windowInfoHandle->getInfo();
319 } else {
320 inputInfo = {};
321 // b/271132344 revisit this and see if we can always use the layers uid/pid
322 inputInfo.name = requested.name;
323 inputInfo.ownerUid = requested.ownerUid;
324 inputInfo.ownerPid = requested.ownerPid;
325 }
326 inputInfo.id = static_cast<int32_t>(uniqueSequence);
327 touchCropId = requested.touchCropId;
328 }
329
330 if (forceUpdate ||
331 requested.what &
332 (layer_state_t::eColorChanged | layer_state_t::eBufferChanged |
333 layer_state_t::eSidebandStreamChanged)) {
334 color.rgb = requested.getColor().rgb;
335 }
336
337 if (forceUpdate || requested.what & layer_state_t::eBufferChanged) {
338 acquireFence =
339 (requested.externalTexture &&
340 requested.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged))
341 ? requested.bufferData->acquireFence
342 : Fence::NO_FENCE;
343 buffer = requested.externalTexture ? requested.externalTexture->getBuffer() : nullptr;
344 externalTexture = requested.externalTexture;
345 frameNumber = (requested.bufferData) ? requested.bufferData->frameNumber : 0;
346 hasProtectedContent = requested.externalTexture &&
347 requested.externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
348 geomUsesSourceCrop = hasBufferOrSidebandStream();
349 }
350
351 if (forceUpdate ||
352 requested.what &
353 (layer_state_t::eCropChanged | layer_state_t::eBufferCropChanged |
354 layer_state_t::eBufferTransformChanged |
355 layer_state_t::eTransformToDisplayInverseChanged) ||
356 requested.changes.test(RequestedLayerState::Changes::BufferSize) || displayChanges) {
357 bufferSize = requested.getBufferSize(displayRotationFlags);
358 geomBufferSize = bufferSize;
359 croppedBufferSize = requested.getCroppedBufferSize(bufferSize);
360 geomContentCrop = requested.getBufferCrop();
361 }
362
363 if (forceUpdate ||
364 requested.what &
365 (layer_state_t::eFlagsChanged | layer_state_t::eDestinationFrameChanged |
366 layer_state_t::ePositionChanged | layer_state_t::eMatrixChanged |
367 layer_state_t::eBufferTransformChanged |
368 layer_state_t::eTransformToDisplayInverseChanged) ||
369 requested.changes.test(RequestedLayerState::Changes::BufferSize) || displayChanges) {
370 localTransform = requested.getTransform(displayRotationFlags);
371 localTransformInverse = localTransform.inverse();
372 }
373
374 if (forceUpdate || requested.what & (layer_state_t::eColorChanged) ||
375 requested.changes.test(RequestedLayerState::Changes::BufferSize)) {
376 color.rgb = requested.getColor().rgb;
377 }
378
379 if (forceUpdate ||
380 requested.what &
381 (layer_state_t::eBufferChanged | layer_state_t::eDataspaceChanged |
382 layer_state_t::eApiChanged)) {
383 isHdrY410 = requested.dataspace == ui::Dataspace::BT2020_ITU_PQ &&
384 requested.api == NATIVE_WINDOW_API_MEDIA &&
385 requested.bufferData->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102;
386 }
387
388 if (forceUpdate ||
389 requested.what &
390 (layer_state_t::eBufferChanged | layer_state_t::eDataspaceChanged |
391 layer_state_t::eApiChanged | layer_state_t::eShadowRadiusChanged |
392 layer_state_t::eBlurRegionsChanged | layer_state_t::eStretchChanged)) {
393 forceClientComposition = isHdrY410 || shadowSettings.length > 0 ||
394 requested.blurRegions.size() > 0 || stretchEffect.hasEffect();
395 }
396
397 if (forceUpdate ||
398 requested.what &
399 (layer_state_t::eColorChanged | layer_state_t::eShadowRadiusChanged |
400 layer_state_t::eBlurRegionsChanged | layer_state_t::eBackgroundBlurRadiusChanged |
401 layer_state_t::eCornerRadiusChanged | layer_state_t::eAlphaChanged |
402 layer_state_t::eFlagsChanged | layer_state_t::eBufferChanged |
403 layer_state_t::eSidebandStreamChanged)) {
404 contentOpaque = isContentOpaque();
405 isOpaque = contentOpaque && !roundedCorner.hasRoundedCorners() && color.a == 1.f;
406 blendMode = getBlendMode(requested);
407 }
408}
409
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000410} // namespace android::surfaceflinger::frontend