Draw shadows when the casting layer is occluded or transparent
Ignore transparent region when generating the composition layer for shadows. Client may
provide transparent region hints but the shadow should be drawn over the entire layer.
Solves an issue with YouTube in PiP which sets a transparent region where the
SurfaceView is shown causing a shadow of incorrect size to be drawn.
Draw shadows even if the layer is completely occluded by another layer. The layer will
not be composed in this case causing the shadow to not be drawn either. So extend its
visible region by the shadow length so we can check if the shadows will be occluded as
well.
Bug: 136561771
Test: atest libcompositionengine_test
Test: manual tests with pip overriding shadows to be drawn by sf
Change-Id: I4c6cae1716caebe46119ebd1643d8b5e3eda56c3
diff --git a/services/surfaceflinger/CompositionEngine/include/compositionengine/LayerFECompositionState.h b/services/surfaceflinger/CompositionEngine/include/compositionengine/LayerFECompositionState.h
index 2ba781d..a64fdbf 100644
--- a/services/surfaceflinger/CompositionEngine/include/compositionengine/LayerFECompositionState.h
+++ b/services/surfaceflinger/CompositionEngine/include/compositionengine/LayerFECompositionState.h
@@ -79,6 +79,9 @@
// The bounds of the layer in layer local coordinates
FloatRect geomLayerBounds;
+ // length of the shadow in screen space
+ float shadowRadius;
+
/*
* Geometry state
*/
diff --git a/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/OutputLayerCompositionState.h b/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/OutputLayerCompositionState.h
index 11cfccc..00baa89 100644
--- a/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/OutputLayerCompositionState.h
+++ b/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/OutputLayerCompositionState.h
@@ -52,6 +52,9 @@
// The visibleRegion transformed to output space
Region outputSpaceVisibleRegion;
+ // Region cast by the layer's shadow
+ Region shadowRegion;
+
// If true, client composition will be used on this output
bool forceClientComposition{false};
diff --git a/services/surfaceflinger/CompositionEngine/src/LayerFECompositionState.cpp b/services/surfaceflinger/CompositionEngine/src/LayerFECompositionState.cpp
index 1ca03dc..e740529 100644
--- a/services/surfaceflinger/CompositionEngine/src/LayerFECompositionState.cpp
+++ b/services/surfaceflinger/CompositionEngine/src/LayerFECompositionState.cpp
@@ -51,6 +51,9 @@
out.append(" ");
dumpVal(out, "geomLayerBounds", geomLayerBounds);
+ out.append(" ");
+ dumpVal(out, "shadowRadius", shadowRadius);
+
out.append("\n ");
dumpVal(out, "blend", toString(blendMode), blendMode);
dumpVal(out, "alpha", alpha);
diff --git a/services/surfaceflinger/CompositionEngine/src/Output.cpp b/services/surfaceflinger/CompositionEngine/src/Output.cpp
index 7e5a720..28d2653 100644
--- a/services/surfaceflinger/CompositionEngine/src/Output.cpp
+++ b/services/surfaceflinger/CompositionEngine/src/Output.cpp
@@ -408,6 +408,11 @@
*/
Region transparentRegion;
+ /*
+ * shadowRegion: Region cast by the layer's shadow.
+ */
+ Region shadowRegion;
+
// handle hidden surfaces by setting the visible region to empty
if (CC_UNLIKELY(!layerFEState.isVisible)) {
return;
@@ -418,7 +423,18 @@
// Get the visible region
// TODO(b/121291683): Is it worth creating helper methods on LayerFEState
// for computations like this?
- visibleRegion.set(Rect(tr.transform(layerFEState.geomLayerBounds)));
+ const Rect visibleRect(tr.transform(layerFEState.geomLayerBounds));
+ visibleRegion.set(visibleRect);
+
+ if (layerFEState.shadowRadius > 0.0f) {
+ // if the layer casts a shadow, offset the layers visible region and
+ // calculate the shadow region.
+ const int32_t inset = layerFEState.shadowRadius * -1.0f;
+ Rect visibleRectWithShadows(visibleRect);
+ visibleRectWithShadows.inset(inset, inset, inset, inset);
+ visibleRegion.set(visibleRectWithShadows);
+ shadowRegion = visibleRegion.subtract(visibleRect);
+ }
if (visibleRegion.isEmpty()) {
return;
@@ -444,7 +460,7 @@
// Otherwise we don't try and compute the opaque region since there may
// be errors at the edges, and we treat the entire layer as
// translucent.
- opaqueRegion = visibleRegion;
+ opaqueRegion.set(visibleRect);
}
// Clip the covered region to the visible region
@@ -510,7 +526,7 @@
// Compute the visible non-transparent region
Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
- // Peform the final check to see if this layer is visible on this output
+ // Perform the final check to see if this layer is visible on this output
// TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
const auto& outputState = getState();
Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
@@ -519,6 +535,8 @@
return;
}
+ Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
+
// The layer is visible. Either reuse the existing outputLayer if we have
// one, or create a new one if we do not.
auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
@@ -529,8 +547,9 @@
outputLayerState.visibleRegion = visibleRegion;
outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
outputLayerState.coveredRegion = coveredRegion;
- outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
- outputLayerState.visibleRegion.intersect(outputState.viewport));
+ outputLayerState.outputSpaceVisibleRegion =
+ outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
+ outputLayerState.shadowRegion = shadowRegion;
}
void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
@@ -884,7 +903,7 @@
continue;
}
- bool clientComposition = layer->requiresClientComposition();
+ const bool clientComposition = layer->requiresClientComposition();
// We clear the client target for non-client composed layers if
// requested by the HWC. We skip this if the layer is not an opaque
@@ -921,8 +940,15 @@
}
}
- layer->editState().clientCompositionTimestamp = systemTime();
- clientCompositionLayers.push_back(*result);
+ // If the layer casts a shadow but the content casting the shadow is occluded, skip
+ // composing the non-shadow content and only draw the shadows.
+ const bool skipNonShadowContentComposition = clientComposition &&
+ layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
+
+ if (!skipNonShadowContentComposition) {
+ layer->editState().clientCompositionTimestamp = systemTime();
+ clientCompositionLayers.push_back(*result);
+ }
}
}
diff --git a/services/surfaceflinger/CompositionEngine/src/OutputLayerCompositionState.cpp b/services/surfaceflinger/CompositionEngine/src/OutputLayerCompositionState.cpp
index ad668b6..cc3c54c 100644
--- a/services/surfaceflinger/CompositionEngine/src/OutputLayerCompositionState.cpp
+++ b/services/surfaceflinger/CompositionEngine/src/OutputLayerCompositionState.cpp
@@ -51,6 +51,9 @@
dumpVal(out, "output visibleRegion", outputSpaceVisibleRegion);
out.append(" ");
+ dumpVal(out, "shadowRegion", shadowRegion);
+
+ out.append(" ");
dumpVal(out, "forceClientComposition", forceClientComposition);
dumpVal(out, "clearClientTarget", clearClientTarget);
dumpVal(out, "displayFrame", displayFrame);
diff --git a/services/surfaceflinger/CompositionEngine/tests/OutputTest.cpp b/services/surfaceflinger/CompositionEngine/tests/OutputTest.cpp
index 87beb0d..cb6c50c 100644
--- a/services/surfaceflinger/CompositionEngine/tests/OutputTest.cpp
+++ b/services/surfaceflinger/CompositionEngine/tests/OutputTest.cpp
@@ -1439,6 +1439,87 @@
EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kExpectedLayerVisibleRegion));
}
+TEST_F(OutputEnsureOutputLayerIfVisibleTest, coverageAccumulatesWithShadowsTest) {
+ ui::Transform translate;
+ translate.set(50, 50);
+ mLayerFEState.geomLayerTransform = translate;
+ mLayerFEState.shadowRadius = 10.0f;
+
+ mCoverageState.dirtyRegion = Region(Rect(0, 0, 500, 500));
+ // half of the layer including the casting shadow is covered and opaque
+ mCoverageState.aboveCoveredLayers = Region(Rect(40, 40, 100, 260));
+ mCoverageState.aboveOpaqueLayers = Region(Rect(40, 40, 100, 260));
+
+ EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
+ .WillOnce(Return(&mOutputLayer));
+
+ mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
+
+ const Region kExpectedDirtyRegion = Region(Rect(0, 0, 500, 500));
+ const Region kExpectedAboveCoveredRegion = Region(Rect(40, 40, 160, 260));
+ // add starting opaque region to the opaque half of the casting layer bounds
+ const Region kExpectedAboveOpaqueRegion =
+ Region(Rect(40, 40, 100, 260)).orSelf(Rect(100, 50, 150, 250));
+ const Region kExpectedLayerVisibleRegion = Region(Rect(100, 40, 160, 260));
+ const Region kExpectedoutputSpaceLayerVisibleRegion = Region(Rect(100, 50, 150, 250));
+ const Region kExpectedLayerCoveredRegion = Region(Rect(40, 40, 100, 260));
+ const Region kExpectedLayerVisibleNonTransparentRegion = Region(Rect(100, 40, 160, 260));
+ const Region kExpectedLayerShadowRegion =
+ Region(Rect(40, 40, 160, 260)).subtractSelf(Rect(50, 50, 150, 250));
+
+ EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kExpectedDirtyRegion));
+ EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kExpectedAboveCoveredRegion));
+ EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kExpectedAboveOpaqueRegion));
+
+ EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kExpectedLayerVisibleRegion));
+ EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion,
+ RegionEq(kExpectedLayerVisibleNonTransparentRegion));
+ EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kExpectedLayerCoveredRegion));
+ EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion,
+ RegionEq(kExpectedoutputSpaceLayerVisibleRegion));
+ EXPECT_THAT(mOutputLayerState.shadowRegion, RegionEq(kExpectedLayerShadowRegion));
+ EXPECT_FALSE(kExpectedLayerVisibleRegion.subtract(kExpectedLayerShadowRegion).isEmpty());
+}
+
+TEST_F(OutputEnsureOutputLayerIfVisibleTest, shadowRegionOnlyTest) {
+ ui::Transform translate;
+ translate.set(50, 50);
+ mLayerFEState.geomLayerTransform = translate;
+ mLayerFEState.shadowRadius = 10.0f;
+
+ mCoverageState.dirtyRegion = Region(Rect(0, 0, 500, 500));
+ // Casting layer is covered by an opaque region leaving only part of its shadow to be drawn
+ mCoverageState.aboveCoveredLayers = Region(Rect(40, 40, 150, 260));
+ mCoverageState.aboveOpaqueLayers = Region(Rect(40, 40, 150, 260));
+
+ EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
+ .WillOnce(Return(&mOutputLayer));
+
+ mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
+
+ const Region kExpectedLayerVisibleRegion = Region(Rect(150, 40, 160, 260));
+ const Region kExpectedLayerShadowRegion =
+ Region(Rect(40, 40, 160, 260)).subtractSelf(Rect(50, 50, 150, 250));
+
+ EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kExpectedLayerVisibleRegion));
+ EXPECT_THAT(mOutputLayerState.shadowRegion, RegionEq(kExpectedLayerShadowRegion));
+ EXPECT_TRUE(kExpectedLayerVisibleRegion.subtract(kExpectedLayerShadowRegion).isEmpty());
+}
+
+TEST_F(OutputEnsureOutputLayerIfVisibleTest, takesNotSoEarlyOutifLayerWithShadowIsCovered) {
+ ui::Transform translate;
+ translate.set(50, 50);
+ mLayerFEState.geomLayerTransform = translate;
+ mLayerFEState.shadowRadius = 10.0f;
+
+ mCoverageState.dirtyRegion = Region(Rect(0, 0, 500, 500));
+ // Casting layer and its shadows are covered by an opaque region
+ mCoverageState.aboveCoveredLayers = Region(Rect(40, 40, 160, 260));
+ mCoverageState.aboveOpaqueLayers = Region(Rect(40, 40, 160, 260));
+
+ mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
+}
+
/*
* Output::present()
*/
@@ -3635,5 +3716,67 @@
EXPECT_EQ(rightLayer.mRELayerSettings, requests[1]);
}
+TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
+ shadowRegionOnlyVisibleSkipsContentComposition) {
+ const Rect kContentWithShadow(40, 40, 70, 90);
+ const Rect kContent(50, 50, 60, 80);
+ const Region kShadowRegion = Region(kContentWithShadow).subtract(kContent);
+ const Region kPartialShadowRegion = Region(kContentWithShadow).subtract(Rect(40, 40, 60, 80));
+
+ renderengine::LayerSettings mREShadowSettings;
+ mREShadowSettings.source.solidColor = {0.1f, 0.1f, 0.1f};
+
+ mLayers[2].mOutputLayerState.visibleRegion = kPartialShadowRegion;
+ mLayers[2].mOutputLayerState.shadowRegion = kShadowRegion;
+
+ EXPECT_CALL(mLayers[0].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
+ EXPECT_CALL(mLayers[1].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
+ EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(_))
+ .WillOnce(Return(mLayers[2].mRELayerSettings));
+ EXPECT_CALL(mLayers[2].mLayerFE,
+ prepareShadowClientComposition(mLayers[2].mRELayerSettings, kDisplayViewport,
+ kDisplayDataspace))
+ .WillOnce(Return(mREShadowSettings));
+
+ Region accumClearRegion(Rect(10, 11, 12, 13));
+ auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
+ accumClearRegion, kDisplayDataspace);
+ ASSERT_EQ(1u, requests.size());
+
+ EXPECT_EQ(mREShadowSettings, requests[0]);
+}
+
+TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
+ shadowRegionWithContentVisibleRequestsContentAndShadowComposition) {
+ const Rect kContentWithShadow(40, 40, 70, 90);
+ const Rect kContent(50, 50, 60, 80);
+ const Region kShadowRegion = Region(kContentWithShadow).subtract(kContent);
+ const Region kPartialContentWithPartialShadowRegion =
+ Region(kContentWithShadow).subtract(Rect(40, 40, 50, 80));
+
+ renderengine::LayerSettings mREShadowSettings;
+ mREShadowSettings.source.solidColor = {0.1f, 0.1f, 0.1f};
+
+ mLayers[2].mOutputLayerState.visibleRegion = kPartialContentWithPartialShadowRegion;
+ mLayers[2].mOutputLayerState.shadowRegion = kShadowRegion;
+
+ EXPECT_CALL(mLayers[0].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
+ EXPECT_CALL(mLayers[1].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
+ EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(_))
+ .WillOnce(Return(mLayers[2].mRELayerSettings));
+ EXPECT_CALL(mLayers[2].mLayerFE,
+ prepareShadowClientComposition(mLayers[2].mRELayerSettings, kDisplayViewport,
+ kDisplayDataspace))
+ .WillOnce(Return(mREShadowSettings));
+
+ Region accumClearRegion(Rect(10, 11, 12, 13));
+ auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
+ accumClearRegion, kDisplayDataspace);
+ ASSERT_EQ(2u, requests.size());
+
+ EXPECT_EQ(mREShadowSettings, requests[0]);
+ EXPECT_EQ(mLayers[2].mRELayerSettings, requests[1]);
+}
+
} // namespace
} // namespace android::compositionengine
diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index 35fc4be..8185775 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -430,6 +430,7 @@
compositionState.internalOnly = getPrimaryDisplayOnly();
compositionState.isVisible = isVisible();
compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
+ compositionState.shadowRadius = mEffectiveShadowRadius;
compositionState.contentDirty = contentDirty;
contentDirty = false;
@@ -585,6 +586,7 @@
renderengine::LayerSettings shadowLayer = casterLayerSettings;
shadowLayer.shadow = shadow;
+ shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
// If the casting layer is translucent, we need to fill in the shadow underneath the layer.
// Otherwise the generated shadow will only be shown around the casting layer.