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     return reason.str();
254 }
255 
canReceiveInput() const256 bool LayerSnapshot::canReceiveInput() const {
257     return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f);
258 }
259 
isTransformValid(const ui::Transform & t)260 bool LayerSnapshot::isTransformValid(const ui::Transform& t) {
261     float transformDet = t.det();
262     return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
263 }
264 
hasInputInfo() const265 bool LayerSnapshot::hasInputInfo() const {
266     return (inputInfo.token != nullptr ||
267             inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) &&
268             reachablilty == Reachablilty::Reachable;
269 }
270 
getDebugString() const271 std::string LayerSnapshot::getDebugString() const {
272     std::stringstream debug;
273     debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {"
274           << getIsVisibleReason() << "} changes=" << changes.string()
275           << " layerStack=" << outputFilter.layerStack.id << " geomLayerBounds={"
276           << geomLayerBounds.left << "," << geomLayerBounds.top << "," << geomLayerBounds.bottom
277           << "," << geomLayerBounds.right << "}"
278           << " geomLayerTransform={tx=" << geomLayerTransform.tx()
279           << ",ty=" << geomLayerTransform.ty() << "}"
280           << "}";
281     if (hasInputInfo()) {
282         debug << " input{"
283               << "(" << inputInfo.inputConfig.string() << ")";
284         if (touchCropId != UNASSIGNED_LAYER_ID) debug << " touchCropId=" << touchCropId;
285         if (inputInfo.replaceTouchableRegionWithCrop) debug << " replaceTouchableRegionWithCrop";
286         auto touchableRegion = inputInfo.touchableRegion.getBounds();
287         debug << " touchableRegion={" << touchableRegion.left << "," << touchableRegion.top << ","
288               << touchableRegion.bottom << "," << touchableRegion.right << "}"
289               << "}";
290     }
291     return debug.str();
292 }
293 
operator <<(std::ostream & out,const LayerSnapshot & obj)294 std::ostream& operator<<(std::ostream& out, const LayerSnapshot& obj) {
295     out << "Layer [" << obj.path.id;
296     if (!obj.path.mirrorRootIds.empty()) {
297         out << " mirrored from ";
298         for (auto rootId : obj.path.mirrorRootIds) {
299             out << rootId << ",";
300         }
301     }
302     out << "] " << obj.name << "\n    " << (obj.isVisible ? "visible" : "invisible")
303         << " reason=" << obj.getIsVisibleReason();
304 
305     if (!obj.geomLayerBounds.isEmpty()) {
306         out << "\n    bounds={" << obj.transformedBounds.left << "," << obj.transformedBounds.top
307             << "," << obj.transformedBounds.bottom << "," << obj.transformedBounds.right << "}";
308     }
309 
310     if (obj.geomLayerTransform.getType() != ui::Transform::IDENTITY) {
311         out << " toDisplayTransform={" << obj.geomLayerTransform << "}";
312     }
313 
314     if (obj.hasInputInfo()) {
315         out << "\n    input{"
316             << "(" << obj.inputInfo.inputConfig.string() << ")";
317         if (obj.inputInfo.canOccludePresentation) out << " canOccludePresentation";
318         if (obj.touchCropId != UNASSIGNED_LAYER_ID) out << " touchCropId=" << obj.touchCropId;
319         if (obj.inputInfo.replaceTouchableRegionWithCrop) out << " replaceTouchableRegionWithCrop";
320         auto touchableRegion = obj.inputInfo.touchableRegion.getBounds();
321         out << " touchableRegion={" << touchableRegion.left << "," << touchableRegion.top << ","
322             << touchableRegion.bottom << "," << touchableRegion.right << "}"
323             << "}";
324     }
325     return out;
326 }
327 
sourceBounds() const328 FloatRect LayerSnapshot::sourceBounds() const {
329     if (!externalTexture) {
330         return geomLayerBounds;
331     }
332     return geomBufferSize.toFloatRect();
333 }
334 
isFrontBuffered() const335 bool LayerSnapshot::isFrontBuffered() const {
336     if (!externalTexture) {
337         return false;
338     }
339 
340     return externalTexture->getUsage() & AHARDWAREBUFFER_USAGE_FRONT_BUFFER;
341 }
342 
getBlendMode(const RequestedLayerState & requested) const343 Hwc2::IComposerClient::BlendMode LayerSnapshot::getBlendMode(
344         const RequestedLayerState& requested) const {
345     auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
346     if (alpha != 1.0f || !contentOpaque) {
347         blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
348                                                  : Hwc2::IComposerClient::BlendMode::COVERAGE;
349     }
350     return blendMode;
351 }
352 
merge(const RequestedLayerState & requested,bool forceUpdate,bool displayChanges,bool forceFullDamage,uint32_t displayRotationFlags)353 void LayerSnapshot::merge(const RequestedLayerState& requested, bool forceUpdate,
354                           bool displayChanges, bool forceFullDamage,
355                           uint32_t displayRotationFlags) {
356     clientChanges = requested.what;
357     changes = requested.changes;
358     contentDirty = requested.what & layer_state_t::CONTENT_DIRTY;
359     hasReadyFrame = requested.autoRefresh;
360     sidebandStreamHasFrame = requested.hasSidebandStreamFrame();
361     updateSurfaceDamage(requested, requested.hasReadyFrame(), forceFullDamage, surfaceDamage);
362 
363     if (forceUpdate || requested.what & layer_state_t::eTransparentRegionChanged) {
364         transparentRegionHint = requested.transparentRegion;
365     }
366     if (forceUpdate || requested.what & layer_state_t::eFlagsChanged) {
367         layerOpaqueFlagSet =
368                 (requested.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque;
369     }
370     if (forceUpdate || requested.what & layer_state_t::eBufferTransformChanged) {
371         geomBufferTransform = requested.bufferTransform;
372     }
373     if (forceUpdate || requested.what & layer_state_t::eTransformToDisplayInverseChanged) {
374         geomBufferUsesDisplayInverseTransform = requested.transformToDisplayInverse;
375     }
376     if (forceUpdate || requested.what & layer_state_t::eDataspaceChanged) {
377         dataspace = Layer::translateDataspace(requested.dataspace);
378     }
379     if (forceUpdate || requested.what & layer_state_t::eExtendedRangeBrightnessChanged) {
380         currentHdrSdrRatio = requested.currentHdrSdrRatio;
381         desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
382     }
383     if (forceUpdate || requested.what & layer_state_t::eDesiredHdrHeadroomChanged) {
384         desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
385     }
386     if (forceUpdate || requested.what & layer_state_t::eCachingHintChanged) {
387         cachingHint = requested.cachingHint;
388     }
389     if (forceUpdate || requested.what & layer_state_t::eHdrMetadataChanged) {
390         hdrMetadata = requested.hdrMetadata;
391     }
392     if (forceUpdate || requested.what & layer_state_t::eSidebandStreamChanged) {
393         sidebandStream = requested.sidebandStream;
394     }
395     if (forceUpdate || requested.what & layer_state_t::eShadowRadiusChanged) {
396         shadowSettings.length = requested.shadowRadius;
397     }
398     if (forceUpdate || requested.what & layer_state_t::eFrameRateSelectionPriority) {
399         frameRateSelectionPriority = requested.frameRateSelectionPriority;
400     }
401     if (forceUpdate || requested.what & layer_state_t::eColorSpaceAgnosticChanged) {
402         isColorspaceAgnostic = requested.colorSpaceAgnostic;
403     }
404     if (forceUpdate || requested.what & layer_state_t::eDimmingEnabledChanged) {
405         dimmingEnabled = requested.dimmingEnabled;
406     }
407     if (forceUpdate || requested.what & layer_state_t::eCropChanged) {
408         geomCrop = requested.crop;
409     }
410 
411     if (forceUpdate || requested.what & layer_state_t::eDefaultFrameRateCompatibilityChanged) {
412         const auto compatibility =
413                 Layer::FrameRate::convertCompatibility(requested.defaultFrameRateCompatibility);
414         if (defaultFrameRateCompatibility != compatibility) {
415             clientChanges |= layer_state_t::eDefaultFrameRateCompatibilityChanged;
416         }
417         defaultFrameRateCompatibility = compatibility;
418     }
419 
420     if (forceUpdate ||
421         requested.what &
422                 (layer_state_t::eFlagsChanged | layer_state_t::eBufferChanged |
423                  layer_state_t::eSidebandStreamChanged)) {
424         compositionType = requested.getCompositionType();
425     }
426 
427     if (forceUpdate || requested.what & layer_state_t::eInputInfoChanged) {
428         if (requested.windowInfoHandle) {
429             inputInfo = *requested.windowInfoHandle->getInfo();
430         } else {
431             inputInfo = {};
432             // b/271132344 revisit this and see if we can always use the layers uid/pid
433             inputInfo.name = requested.name;
434             inputInfo.ownerUid = requested.ownerUid;
435             inputInfo.ownerPid = requested.ownerPid;
436         }
437         inputInfo.id = static_cast<int32_t>(uniqueSequence);
438         touchCropId = requested.touchCropId;
439     }
440 
441     if (forceUpdate ||
442         requested.what &
443                 (layer_state_t::eColorChanged | layer_state_t::eBufferChanged |
444                  layer_state_t::eSidebandStreamChanged)) {
445         color.rgb = requested.getColor().rgb;
446     }
447 
448     if (forceUpdate || requested.what & layer_state_t::eBufferChanged) {
449         acquireFence =
450                 (requested.externalTexture &&
451                  requested.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged))
452                 ? requested.bufferData->acquireFence
453                 : Fence::NO_FENCE;
454         buffer = requested.externalTexture ? requested.externalTexture->getBuffer() : nullptr;
455         externalTexture = requested.externalTexture;
456         frameNumber = (requested.bufferData) ? requested.bufferData->frameNumber : 0;
457         hasProtectedContent = requested.externalTexture &&
458                 requested.externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
459         geomUsesSourceCrop = hasBufferOrSidebandStream();
460     }
461 
462     if (forceUpdate ||
463         requested.what &
464                 (layer_state_t::eCropChanged | layer_state_t::eBufferCropChanged |
465                  layer_state_t::eBufferTransformChanged |
466                  layer_state_t::eTransformToDisplayInverseChanged) ||
467         requested.changes.test(RequestedLayerState::Changes::BufferSize) || displayChanges) {
468         bufferSize = requested.getBufferSize(displayRotationFlags);
469         geomBufferSize = bufferSize;
470         croppedBufferSize = requested.getCroppedBufferSize(bufferSize);
471         geomContentCrop = requested.getBufferCrop();
472     }
473 
474     if ((forceUpdate ||
475          requested.what &
476                  (layer_state_t::eFlagsChanged | layer_state_t::eDestinationFrameChanged |
477                   layer_state_t::ePositionChanged | layer_state_t::eMatrixChanged |
478                   layer_state_t::eBufferTransformChanged |
479                   layer_state_t::eTransformToDisplayInverseChanged) ||
480          requested.changes.test(RequestedLayerState::Changes::BufferSize) || displayChanges) &&
481         !ignoreLocalTransform) {
482         localTransform = requested.getTransform(displayRotationFlags);
483         localTransformInverse = localTransform.inverse();
484     }
485 
486     if (forceUpdate || requested.what & (layer_state_t::eColorChanged) ||
487         requested.changes.test(RequestedLayerState::Changes::BufferSize)) {
488         color.rgb = requested.getColor().rgb;
489     }
490 
491     if (forceUpdate ||
492         requested.what &
493                 (layer_state_t::eBufferChanged | layer_state_t::eDataspaceChanged |
494                  layer_state_t::eApiChanged | layer_state_t::eShadowRadiusChanged |
495                  layer_state_t::eBlurRegionsChanged | layer_state_t::eStretchChanged)) {
496         forceClientComposition = shadowSettings.length > 0 || stretchEffect.hasEffect();
497     }
498 
499     if (forceUpdate ||
500         requested.what &
501                 (layer_state_t::eColorChanged | layer_state_t::eShadowRadiusChanged |
502                  layer_state_t::eBlurRegionsChanged | layer_state_t::eBackgroundBlurRadiusChanged |
503                  layer_state_t::eCornerRadiusChanged | layer_state_t::eAlphaChanged |
504                  layer_state_t::eFlagsChanged | layer_state_t::eBufferChanged |
505                  layer_state_t::eSidebandStreamChanged)) {
506         contentOpaque = isContentOpaque();
507         isOpaque = contentOpaque && !roundedCorner.hasRoundedCorners() && color.a == 1.f;
508         blendMode = getBlendMode(requested);
509     }
510 }
511 
512 } // namespace android::surfaceflinger::frontend
513