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 "SurfaceFlinger"
20
21 #include "LayerSnapshot.h"
22 #include "Layer.h"
23
24 namespace android::surfaceflinger::frontend {
25
26 using namespace ftl::flag_operators;
27
28 namespace {
29
updateSurfaceDamage(const RequestedLayerState & requested,bool hasReadyFrame,bool forceFullDamage,Region & outSurfaceDamageRegion)30 void updateSurfaceDamage(const RequestedLayerState& requested, bool hasReadyFrame,
31 bool forceFullDamage, Region& outSurfaceDamageRegion) {
32 if (!hasReadyFrame) {
33 outSurfaceDamageRegion.clear();
34 return;
35 }
36 if (forceFullDamage) {
37 outSurfaceDamageRegion = Region::INVALID_REGION;
38 } else {
39 outSurfaceDamageRegion = requested.surfaceDamageRegion;
40 }
41 }
42
operator <<(std::ostream & os,const ui::Transform & transform)43 std::ostream& operator<<(std::ostream& os, const ui::Transform& transform) {
44 const uint32_t type = transform.getType();
45 const uint32_t orientation = transform.getOrientation();
46 if (type == ui::Transform::IDENTITY) {
47 return os;
48 }
49
50 if (type & ui::Transform::UNKNOWN) {
51 std::string out;
52 transform.dump(out, "", "");
53 os << out;
54 return os;
55 }
56
57 if (type & ui::Transform::ROTATE) {
58 switch (orientation) {
59 case ui::Transform::ROT_0:
60 os << "ROT_0";
61 break;
62 case ui::Transform::FLIP_H:
63 os << "FLIP_H";
64 break;
65 case ui::Transform::FLIP_V:
66 os << "FLIP_V";
67 break;
68 case ui::Transform::ROT_90:
69 os << "ROT_90";
70 break;
71 case ui::Transform::ROT_180:
72 os << "ROT_180";
73 break;
74 case ui::Transform::ROT_270:
75 os << "ROT_270";
76 break;
77 case ui::Transform::ROT_INVALID:
78 default:
79 os << "ROT_INVALID";
80 break;
81 }
82 }
83
84 if (type & ui::Transform::SCALE) {
85 std::string out;
86 android::base::StringAppendF(&out, " scale x=%.4f y=%.4f ", transform.getScaleX(),
87 transform.getScaleY());
88 os << out;
89 }
90
91 if (type & ui::Transform::TRANSLATE) {
92 std::string out;
93 android::base::StringAppendF(&out, " tx=%.4f ty=%.4f ", transform.tx(), transform.ty());
94 os << out;
95 }
96
97 return os;
98 }
99
100 } // namespace
101
LayerSnapshot(const RequestedLayerState & state,const LayerHierarchy::TraversalPath & path)102 LayerSnapshot::LayerSnapshot(const RequestedLayerState& state,
103 const LayerHierarchy::TraversalPath& path)
104 : path(path) {
105 // Provide a unique id for all snapshots.
106 // A front end layer can generate multiple snapshots if its mirrored.
107 // Additionally, if the layer is not reachable, we may choose to destroy
108 // and recreate the snapshot in which case the unique sequence id will
109 // change. The consumer shouldn't tie any lifetimes to this unique id but
110 // register a LayerLifecycleManager::ILifecycleListener or get a list of
111 // destroyed layers from LayerLifecycleManager.
112 if (path.isClone()) {
113 uniqueSequence =
114 LayerCreationArgs::getInternalLayerId(LayerCreationArgs::sInternalSequence++);
115 } else {
116 uniqueSequence = state.id;
117 }
118 sequence = static_cast<int32_t>(state.id);
119 name = state.name;
120 debugName = state.debugName;
121 premultipliedAlpha = state.premultipliedAlpha;
122 inputInfo.name = state.name;
123 inputInfo.id = static_cast<int32_t>(uniqueSequence);
124 inputInfo.ownerUid = gui::Uid{state.ownerUid};
125 inputInfo.ownerPid = gui::Pid{state.ownerPid};
126 uid = state.ownerUid;
127 pid = state.ownerPid;
128 changes = RequestedLayerState::Changes::Created;
129 clientChanges = 0;
130 mirrorRootPath =
131 LayerHierarchy::isMirror(path.variant) ? path : LayerHierarchy::TraversalPath::ROOT;
132 reachablilty = LayerSnapshot::Reachablilty::Unreachable;
133 frameRateSelectionPriority = state.frameRateSelectionPriority;
134 layerMetadata = state.metadata;
135 }
136
137 // As documented in libhardware header, formats in the range
138 // 0x100 - 0x1FF are specific to the HAL implementation, and
139 // are known to have no alpha channel
140 // TODO: move definition for device-specific range into
141 // hardware.h, instead of using hard-coded values here.
142 #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
143
isOpaqueFormat(PixelFormat format)144 bool LayerSnapshot::isOpaqueFormat(PixelFormat format) {
145 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
146 return true;
147 }
148 switch (format) {
149 case PIXEL_FORMAT_RGBA_8888:
150 case PIXEL_FORMAT_BGRA_8888:
151 case PIXEL_FORMAT_RGBA_FP16:
152 case PIXEL_FORMAT_RGBA_1010102:
153 case PIXEL_FORMAT_R_8:
154 return false;
155 }
156 // in all other case, we have no blending (also for unknown formats)
157 return true;
158 }
159
hasBufferOrSidebandStream() const160 bool LayerSnapshot::hasBufferOrSidebandStream() const {
161 return ((sidebandStream != nullptr) || (externalTexture != nullptr));
162 }
163
drawShadows() const164 bool LayerSnapshot::drawShadows() const {
165 return shadowSettings.length > 0.f;
166 }
167
fillsColor() const168 bool LayerSnapshot::fillsColor() const {
169 return !hasBufferOrSidebandStream() && color.r >= 0.0_hf && color.g >= 0.0_hf &&
170 color.b >= 0.0_hf;
171 }
172
hasBlur() const173 bool LayerSnapshot::hasBlur() const {
174 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
175 }
176
hasEffect() const177 bool LayerSnapshot::hasEffect() const {
178 return fillsColor() || drawShadows() || hasBlur();
179 }
180
hasSomethingToDraw() const181 bool LayerSnapshot::hasSomethingToDraw() const {
182 return hasEffect() || hasBufferOrSidebandStream();
183 }
184
isContentOpaque() const185 bool LayerSnapshot::isContentOpaque() const {
186 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
187 // layer's opaque flag.
188 if (!hasSomethingToDraw()) {
189 return false;
190 }
191
192 // if the layer has the opaque flag, then we're always opaque
193 if (layerOpaqueFlagSet) {
194 return true;
195 }
196
197 // If the buffer has no alpha channel, then we are opaque
198 if (hasBufferOrSidebandStream() &&
199 isOpaqueFormat(externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE)) {
200 return true;
201 }
202
203 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
204 return fillsColor() && color.a == 1.0_hf;
205 }
206
isHiddenByPolicy() const207 bool LayerSnapshot::isHiddenByPolicy() const {
208 return invalidTransform || isHiddenByPolicyFromParent || isHiddenByPolicyFromRelativeParent;
209 }
210
getIsVisible() const211 bool LayerSnapshot::getIsVisible() const {
212 if (reachablilty != LayerSnapshot::Reachablilty::Reachable) {
213 return false;
214 }
215
216 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) {
217 return false;
218 }
219
220 if (!hasSomethingToDraw()) {
221 return false;
222 }
223
224 if (isHiddenByPolicy()) {
225 return false;
226 }
227
228 return color.a > 0.0f || hasBlur();
229 }
230
getIsVisibleReason() const231 std::string LayerSnapshot::getIsVisibleReason() const {
232 // not visible
233 if (reachablilty == LayerSnapshot::Reachablilty::Unreachable)
234 return "layer not reachable from root";
235 if (reachablilty == LayerSnapshot::Reachablilty::ReachableByRelativeParent)
236 return "layer only reachable via relative parent";
237 if (isHiddenByPolicyFromParent) return "hidden by parent or layer flag";
238 if (isHiddenByPolicyFromRelativeParent) return "hidden by relative parent";
239 if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) return "eLayerSkipScreenshot";
240 if (invalidTransform) return "invalidTransform";
241 if (color.a == 0.0f && !hasBlur()) return "alpha = 0 and no blur";
242 if (!hasSomethingToDraw()) return "nothing to draw";
243
244 // visible
245 std::stringstream reason;
246 if (sidebandStream != nullptr) reason << " sidebandStream";
247 if (externalTexture != nullptr)
248 reason << " buffer=" << externalTexture->getId() << " frame=" << frameNumber;
249 if (fillsColor() || color.a > 0.0f) reason << " color{" << color << "}";
250 if (drawShadows()) reason << " shadowSettings.length=" << shadowSettings.length;
251 if (backgroundBlurRadius > 0) reason << " backgroundBlurRadius=" << backgroundBlurRadius;
252 if (blurRegions.size() > 0) reason << " blurRegions.size()=" << blurRegions.size();
253 if (contentDirty) reason << " contentDirty";
254 return reason.str();
255 }
256
canReceiveInput() const257 bool LayerSnapshot::canReceiveInput() const {
258 return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f);
259 }
260
isTransformValid(const ui::Transform & t)261 bool LayerSnapshot::isTransformValid(const ui::Transform& t) {
262 float transformDet = t.det();
263 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
264 }
265
hasInputInfo() const266 bool LayerSnapshot::hasInputInfo() const {
267 return (inputInfo.token != nullptr ||
268 inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) &&
269 reachablilty == Reachablilty::Reachable;
270 }
271
getDebugString() const272 std::string LayerSnapshot::getDebugString() const {
273 std::stringstream debug;
274 debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {"
275 << getIsVisibleReason() << "} changes=" << changes.string()
276 << " layerStack=" << outputFilter.layerStack.id << " geomLayerBounds={"
277 << geomLayerBounds.left << "," << geomLayerBounds.top << "," << geomLayerBounds.bottom
278 << "," << geomLayerBounds.right << "}"
279 << " geomLayerTransform={tx=" << geomLayerTransform.tx()
280 << ",ty=" << geomLayerTransform.ty() << "}"
281 << "}";
282 if (hasInputInfo()) {
283 debug << " input{"
284 << "(" << inputInfo.inputConfig.string() << ")";
285 if (touchCropId != UNASSIGNED_LAYER_ID) debug << " touchCropId=" << touchCropId;
286 if (inputInfo.replaceTouchableRegionWithCrop) debug << " replaceTouchableRegionWithCrop";
287 auto touchableRegion = inputInfo.touchableRegion.getBounds();
288 debug << " touchableRegion={" << touchableRegion.left << "," << touchableRegion.top << ","
289 << touchableRegion.bottom << "," << touchableRegion.right << "}"
290 << "}";
291 }
292 return debug.str();
293 }
294
operator <<(std::ostream & out,const LayerSnapshot & obj)295 std::ostream& operator<<(std::ostream& out, const LayerSnapshot& obj) {
296 out << "Layer [" << obj.path.id;
297 if (!obj.path.mirrorRootIds.empty()) {
298 out << " mirrored from ";
299 for (auto rootId : obj.path.mirrorRootIds) {
300 out << rootId << ",";
301 }
302 }
303 out << "] " << obj.name << "\n " << (obj.isVisible ? "visible" : "invisible")
304 << " reason=" << obj.getIsVisibleReason();
305
306 if (!obj.geomLayerBounds.isEmpty()) {
307 out << "\n bounds={" << obj.transformedBounds.left << "," << obj.transformedBounds.top
308 << "," << obj.transformedBounds.bottom << "," << obj.transformedBounds.right << "}";
309 }
310
311 if (obj.geomLayerTransform.getType() != ui::Transform::IDENTITY) {
312 out << " toDisplayTransform={" << obj.geomLayerTransform << "}";
313 }
314
315 if (obj.hasInputInfo()) {
316 out << "\n input{"
317 << "(" << obj.inputInfo.inputConfig.string() << ")";
318 if (obj.inputInfo.canOccludePresentation) out << " canOccludePresentation";
319 if (obj.touchCropId != UNASSIGNED_LAYER_ID) out << " touchCropId=" << obj.touchCropId;
320 if (obj.inputInfo.replaceTouchableRegionWithCrop) out << " replaceTouchableRegionWithCrop";
321 auto touchableRegion = obj.inputInfo.touchableRegion.getBounds();
322 out << " touchableRegion={" << touchableRegion.left << "," << touchableRegion.top << ","
323 << touchableRegion.bottom << "," << touchableRegion.right << "}"
324 << "}";
325 }
326
327 if (obj.edgeExtensionEffect.hasEffect()) {
328 out << obj.edgeExtensionEffect;
329 }
330 return out;
331 }
332
sourceBounds() const333 FloatRect LayerSnapshot::sourceBounds() const {
334 if (!externalTexture) {
335 return geomLayerBounds;
336 }
337 return geomBufferSize.toFloatRect();
338 }
339
isFrontBuffered() const340 bool LayerSnapshot::isFrontBuffered() const {
341 if (!externalTexture) {
342 return false;
343 }
344
345 return externalTexture->getUsage() & AHARDWAREBUFFER_USAGE_FRONT_BUFFER;
346 }
347
getBlendMode(const RequestedLayerState & requested) const348 Hwc2::IComposerClient::BlendMode LayerSnapshot::getBlendMode(
349 const RequestedLayerState& requested) const {
350 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
351 if (alpha != 1.0f || !contentOpaque) {
352 blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
353 : Hwc2::IComposerClient::BlendMode::COVERAGE;
354 }
355 return blendMode;
356 }
357
merge(const RequestedLayerState & requested,bool forceUpdate,bool displayChanges,bool forceFullDamage,uint32_t displayRotationFlags)358 void LayerSnapshot::merge(const RequestedLayerState& requested, bool forceUpdate,
359 bool displayChanges, bool forceFullDamage,
360 uint32_t displayRotationFlags) {
361 clientChanges = requested.what;
362 changes = requested.changes;
363 autoRefresh = requested.autoRefresh;
364 contentDirty = requested.what & layer_state_t::CONTENT_DIRTY || autoRefresh;
365 hasReadyFrame = autoRefresh;
366 sidebandStreamHasFrame = requested.hasSidebandStreamFrame();
367 updateSurfaceDamage(requested, requested.hasReadyFrame(), forceFullDamage, surfaceDamage);
368
369 if (forceUpdate || requested.what & layer_state_t::eTransparentRegionChanged) {
370 transparentRegionHint = requested.transparentRegion;
371 }
372 if (forceUpdate || requested.what & layer_state_t::eFlagsChanged) {
373 layerOpaqueFlagSet =
374 (requested.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque;
375 }
376 if (forceUpdate || requested.what & layer_state_t::eBufferTransformChanged) {
377 geomBufferTransform = requested.bufferTransform;
378 }
379 if (forceUpdate || requested.what & layer_state_t::eTransformToDisplayInverseChanged) {
380 geomBufferUsesDisplayInverseTransform = requested.transformToDisplayInverse;
381 }
382 if (forceUpdate || requested.what & layer_state_t::eDataspaceChanged) {
383 dataspace = Layer::translateDataspace(requested.dataspace);
384 }
385 if (forceUpdate || requested.what & layer_state_t::eExtendedRangeBrightnessChanged) {
386 currentHdrSdrRatio = requested.currentHdrSdrRatio;
387 desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
388 }
389 if (forceUpdate || requested.what & layer_state_t::eDesiredHdrHeadroomChanged) {
390 desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
391 }
392 if (forceUpdate || requested.what & layer_state_t::eCachingHintChanged) {
393 cachingHint = requested.cachingHint;
394 }
395 if (forceUpdate || requested.what & layer_state_t::eHdrMetadataChanged) {
396 hdrMetadata = requested.hdrMetadata;
397 }
398 if (forceUpdate || requested.what & layer_state_t::eSidebandStreamChanged) {
399 sidebandStream = requested.sidebandStream;
400 }
401 if (forceUpdate || requested.what & layer_state_t::eShadowRadiusChanged) {
402 shadowSettings.length = requested.shadowRadius;
403 }
404 if (forceUpdate || requested.what & layer_state_t::eFrameRateSelectionPriority) {
405 frameRateSelectionPriority = requested.frameRateSelectionPriority;
406 }
407 if (forceUpdate || requested.what & layer_state_t::eColorSpaceAgnosticChanged) {
408 isColorspaceAgnostic = requested.colorSpaceAgnostic;
409 }
410 if (forceUpdate || requested.what & layer_state_t::eDimmingEnabledChanged) {
411 dimmingEnabled = requested.dimmingEnabled;
412 }
413 if (forceUpdate || requested.what & layer_state_t::eCropChanged) {
414 geomCrop = requested.crop;
415 }
416 if (forceUpdate || requested.what & layer_state_t::ePictureProfileHandleChanged) {
417 pictureProfileHandle = requested.pictureProfileHandle;
418 }
419 if (forceUpdate || requested.what & layer_state_t::eAppContentPriorityChanged) {
420 // TODO(b/337330263): Also consider the system-determined priority of the app
421 pictureProfilePriority = requested.appContentPriority;
422 }
423
424 if (forceUpdate || requested.what & layer_state_t::eDefaultFrameRateCompatibilityChanged) {
425 const auto compatibility =
426 Layer::FrameRate::convertCompatibility(requested.defaultFrameRateCompatibility);
427 if (defaultFrameRateCompatibility != compatibility) {
428 clientChanges |= layer_state_t::eDefaultFrameRateCompatibilityChanged;
429 }
430 defaultFrameRateCompatibility = compatibility;
431 }
432
433 if (forceUpdate ||
434 requested.what &
435 (layer_state_t::eFlagsChanged | layer_state_t::eBufferChanged |
436 layer_state_t::eSidebandStreamChanged)) {
437 compositionType = requested.getCompositionType();
438 }
439
440 if (forceUpdate || requested.what & layer_state_t::eInputInfoChanged) {
441 if (requested.windowInfoHandle) {
442 inputInfo = *requested.windowInfoHandle->getInfo();
443 } else {
444 inputInfo = {};
445 // b/271132344 revisit this and see if we can always use the layers uid/pid
446 inputInfo.name = requested.name;
447 inputInfo.ownerUid = requested.ownerUid;
448 inputInfo.ownerPid = requested.ownerPid;
449 }
450 inputInfo.id = static_cast<int32_t>(uniqueSequence);
451 touchCropId = requested.touchCropId;
452 }
453
454 if (forceUpdate ||
455 requested.what &
456 (layer_state_t::eColorChanged | layer_state_t::eBufferChanged |
457 layer_state_t::eSidebandStreamChanged)) {
458 color.rgb = requested.getColor().rgb;
459 }
460
461 if (forceUpdate || requested.what & layer_state_t::eBufferChanged) {
462 acquireFence =
463 (requested.externalTexture &&
464 requested.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged))
465 ? requested.bufferData->acquireFence
466 : Fence::NO_FENCE;
467 buffer = requested.externalTexture ? requested.externalTexture->getBuffer() : nullptr;
468 externalTexture = requested.externalTexture;
469 frameNumber = (requested.bufferData) ? requested.bufferData->frameNumber : 0;
470 hasProtectedContent = requested.externalTexture &&
471 requested.externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
472 geomUsesSourceCrop = hasBufferOrSidebandStream();
473 }
474
475 if (forceUpdate ||
476 requested.what &
477 (layer_state_t::eCropChanged | layer_state_t::eBufferCropChanged |
478 layer_state_t::eBufferTransformChanged |
479 layer_state_t::eTransformToDisplayInverseChanged) ||
480 requested.changes.test(RequestedLayerState::Changes::BufferSize) || displayChanges) {
481 bufferSize = requested.getBufferSize(displayRotationFlags);
482 geomBufferSize = bufferSize;
483 croppedBufferSize = requested.getCroppedBufferSize(bufferSize);
484 geomContentCrop = requested.getBufferCrop();
485 }
486
487 if ((forceUpdate ||
488 requested.what &
489 (layer_state_t::eFlagsChanged | layer_state_t::eDestinationFrameChanged |
490 layer_state_t::ePositionChanged | layer_state_t::eMatrixChanged |
491 layer_state_t::eBufferTransformChanged |
492 layer_state_t::eTransformToDisplayInverseChanged) ||
493 requested.changes.test(RequestedLayerState::Changes::BufferSize) || displayChanges) &&
494 !ignoreLocalTransform) {
495 localTransform = requested.getTransform(displayRotationFlags);
496 localTransformInverse = localTransform.inverse();
497 }
498
499 if (forceUpdate || requested.what & (layer_state_t::eColorChanged) ||
500 requested.changes.test(RequestedLayerState::Changes::BufferSize)) {
501 color.rgb = requested.getColor().rgb;
502 }
503
504 if (forceUpdate ||
505 requested.what &
506 (layer_state_t::eBufferChanged | layer_state_t::eDataspaceChanged |
507 layer_state_t::eApiChanged | layer_state_t::eShadowRadiusChanged |
508 layer_state_t::eBlurRegionsChanged | layer_state_t::eStretchChanged |
509 layer_state_t::eEdgeExtensionChanged)) {
510 forceClientComposition = shadowSettings.length > 0 || stretchEffect.hasEffect() ||
511 edgeExtensionEffect.hasEffect();
512 }
513
514 if (forceUpdate ||
515 requested.what &
516 (layer_state_t::eColorChanged | layer_state_t::eShadowRadiusChanged |
517 layer_state_t::eBlurRegionsChanged | layer_state_t::eBackgroundBlurRadiusChanged |
518 layer_state_t::eCornerRadiusChanged | layer_state_t::eAlphaChanged |
519 layer_state_t::eFlagsChanged | layer_state_t::eBufferChanged |
520 layer_state_t::eSidebandStreamChanged)) {
521 contentOpaque = isContentOpaque();
522 isOpaque = contentOpaque && !roundedCorner.hasRoundedCorners() && color.a == 1.f;
523 blendMode = getBlendMode(requested);
524 }
525
526 if (forceUpdate || requested.what & layer_state_t::eLutsChanged) {
527 luts = requested.luts;
528 }
529 }
530
531 } // namespace android::surfaceflinger::frontend
532