• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 namespace android::surfaceflinger::frontend {
24 
25 using namespace ftl::flag_operators;
26 
27 namespace {
28 
updateSurfaceDamage(const RequestedLayerState & requested,bool hasReadyFrame,bool forceFullDamage,Region & outSurfaceDamageRegion)29 void 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 
LayerSnapshot(const RequestedLayerState & state,const LayerHierarchy::TraversalPath & path)44 LayerSnapshot::LayerSnapshot(const RequestedLayerState& state,
45                              const LayerHierarchy::TraversalPath& path)
46       : path(path) {
47     // 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.
54     if (path.isClone()) {
55         uniqueSequence =
56                 LayerCreationArgs::getInternalLayerId(LayerCreationArgs::sInternalSequence++);
57     } else {
58         uniqueSequence = state.id;
59     }
60     sequence = static_cast<int32_t>(state.id);
61     name = state.name;
62     textureName = state.textureName;
63     premultipliedAlpha = state.premultipliedAlpha;
64     inputInfo.name = state.name;
65     inputInfo.id = static_cast<int32_t>(uniqueSequence);
66     inputInfo.ownerUid = gui::Uid{state.ownerUid};
67     inputInfo.ownerPid = gui::Pid{state.ownerPid};
68     uid = state.ownerUid;
69     pid = state.ownerPid;
70     changes = RequestedLayerState::Changes::Created;
71     clientChanges = 0;
72     mirrorRootPath = path.variant == LayerHierarchy::Variant::Mirror
73             ? path
74             : LayerHierarchy::TraversalPath::ROOT;
75     reachablilty = LayerSnapshot::Reachablilty::Unreachable;
76 }
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 
isOpaqueFormat(PixelFormat format)85 bool 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 
hasBufferOrSidebandStream() const101 bool LayerSnapshot::hasBufferOrSidebandStream() const {
102     return ((sidebandStream != nullptr) || (externalTexture != nullptr));
103 }
104 
drawShadows() const105 bool LayerSnapshot::drawShadows() const {
106     return shadowSettings.length > 0.f;
107 }
108 
fillsColor() const109 bool LayerSnapshot::fillsColor() const {
110     return !hasBufferOrSidebandStream() && color.r >= 0.0_hf && color.g >= 0.0_hf &&
111             color.b >= 0.0_hf;
112 }
113 
hasBlur() const114 bool LayerSnapshot::hasBlur() const {
115     return backgroundBlurRadius > 0 || blurRegions.size() > 0;
116 }
117 
hasEffect() const118 bool LayerSnapshot::hasEffect() const {
119     return fillsColor() || drawShadows() || hasBlur();
120 }
121 
hasSomethingToDraw() const122 bool LayerSnapshot::hasSomethingToDraw() const {
123     return hasEffect() || hasBufferOrSidebandStream();
124 }
125 
isContentOpaque() const126 bool 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() &&
140         isOpaqueFormat(externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE)) {
141         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 
isHiddenByPolicy() const148 bool LayerSnapshot::isHiddenByPolicy() const {
149     return invalidTransform || isHiddenByPolicyFromParent || isHiddenByPolicyFromRelativeParent;
150 }
151 
getIsVisible() const152 bool LayerSnapshot::getIsVisible() const {
153     if (reachablilty != LayerSnapshot::Reachablilty::Reachable) {
154         return false;
155     }
156 
157     if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) {
158         return false;
159     }
160 
161     if (!hasSomethingToDraw()) {
162         return false;
163     }
164 
165     if (isHiddenByPolicy()) {
166         return false;
167     }
168 
169     return color.a > 0.0f || hasBlur();
170 }
171 
getIsVisibleReason() const172 std::string LayerSnapshot::getIsVisibleReason() const {
173     // not visible
174     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";
178     if (isHiddenByPolicyFromParent) return "hidden by parent or layer flag";
179     if (isHiddenByPolicyFromRelativeParent) return "hidden by relative parent";
180     if (handleSkipScreenshotFlag & outputFilter.toInternalDisplay) return "eLayerSkipScreenshot";
181     if (invalidTransform) return "invalidTransform";
182     if (color.a == 0.0f && !hasBlur()) return "alpha = 0 and no blur";
183     if (!hasSomethingToDraw()) return "!hasSomethingToDraw";
184 
185     // visible
186     std::stringstream reason;
187     if (sidebandStream != nullptr) reason << " sidebandStream";
188     if (externalTexture != nullptr)
189         reason << " buffer:" << externalTexture->getId() << " frame:" << frameNumber;
190     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();
195 }
196 
canReceiveInput() const197 bool LayerSnapshot::canReceiveInput() const {
198     return !isHiddenByPolicy() && (!hasBufferOrSidebandStream() || color.a > 0.0f);
199 }
200 
isTransformValid(const ui::Transform & t)201 bool LayerSnapshot::isTransformValid(const ui::Transform& t) {
202     float transformDet = t.det();
203     return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
204 }
205 
hasInputInfo() const206 bool LayerSnapshot::hasInputInfo() const {
207     return (inputInfo.token != nullptr ||
208             inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) &&
209             reachablilty == Reachablilty::Reachable;
210 }
211 
getDebugString() const212 std::string LayerSnapshot::getDebugString() const {
213     std::stringstream debug;
214     debug << "Snapshot{" << path.toString() << name << " isVisible=" << isVisible << " {"
215           << getIsVisibleReason() << "} changes=" << changes.string()
216           << " 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           << "}";
222     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     }
232     return debug.str();
233 }
234 
sourceBounds() const235 FloatRect LayerSnapshot::sourceBounds() const {
236     if (!externalTexture) {
237         return geomLayerBounds;
238     }
239     return geomBufferSize.toFloatRect();
240 }
241 
getBlendMode(const RequestedLayerState & requested) const242 Hwc2::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 
merge(const RequestedLayerState & requested,bool forceUpdate,bool displayChanges,bool forceFullDamage,uint32_t displayRotationFlags)252 void 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 
410 } // namespace android::surfaceflinger::frontend
411