[Impeller] Fix unsafe access for clip stencil coverage (flutter/engine#39595)

This commit is contained in:
Brandon DeRosier
2023-02-13 13:51:05 -08:00
committed by GitHub
parent 6df76ee07e
commit b060a89a91
2 changed files with 70 additions and 2 deletions

View File

@@ -47,10 +47,18 @@ Contents::StencilCoverage ClipContents::GetStencilCoverage(
return {.type = StencilCoverage::Type::kAppend,
.coverage = current_stencil_coverage};
case Entity::ClipOperation::kIntersect:
if (!geometry_) {
return {.type = StencilCoverage::Type::kAppend,
.coverage = std::nullopt};
}
auto coverage = geometry_->GetCoverage(entity.GetTransformation());
if (!coverage.has_value() || !current_stencil_coverage.has_value()) {
return {.type = StencilCoverage::Type::kAppend,
.coverage = std::nullopt};
}
return {
.type = StencilCoverage::Type::kAppend,
.coverage = current_stencil_coverage->Intersection(
geometry_->GetCoverage(entity.GetTransformation()).value()),
.coverage = current_stencil_coverage->Intersection(coverage.value()),
};
}
FML_UNREACHABLE();

View File

@@ -1508,6 +1508,66 @@ TEST_P(EntityTest, ClipContentsShouldRenderIsCorrect) {
}
}
TEST_P(EntityTest, ClipContentsGetStencilCoverageIsCorrect) {
// Intersection: No stencil coverage, no geometry.
{
auto clip = std::make_shared<ClipContents>();
clip->SetClipOperation(Entity::ClipOperation::kIntersect);
auto result = clip->GetStencilCoverage(Entity{}, Rect{});
ASSERT_FALSE(result.coverage.has_value());
}
// Intersection: No stencil coverage, with geometry.
{
auto clip = std::make_shared<ClipContents>();
clip->SetClipOperation(Entity::ClipOperation::kIntersect);
clip->SetGeometry(Geometry::MakeFillPath(
PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath()));
auto result = clip->GetStencilCoverage(Entity{}, Rect{});
ASSERT_FALSE(result.coverage.has_value());
}
// Intersection: With stencil coverage, no geometry.
{
auto clip = std::make_shared<ClipContents>();
clip->SetClipOperation(Entity::ClipOperation::kIntersect);
auto result =
clip->GetStencilCoverage(Entity{}, Rect::MakeLTRB(0, 0, 100, 100));
ASSERT_FALSE(result.coverage.has_value());
}
// Intersection: With stencil coverage, with geometry.
{
auto clip = std::make_shared<ClipContents>();
clip->SetClipOperation(Entity::ClipOperation::kIntersect);
clip->SetGeometry(Geometry::MakeFillPath(
PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 50, 50)).TakePath()));
auto result =
clip->GetStencilCoverage(Entity{}, Rect::MakeLTRB(0, 0, 100, 100));
ASSERT_TRUE(result.coverage.has_value());
ASSERT_RECT_NEAR(result.coverage.value(), Rect::MakeLTRB(0, 0, 50, 50));
ASSERT_EQ(result.type, Contents::StencilCoverage::Type::kAppend);
}
// Difference: With stencil coverage, with geometry.
{
auto clip = std::make_shared<ClipContents>();
clip->SetClipOperation(Entity::ClipOperation::kDifference);
clip->SetGeometry(Geometry::MakeFillPath(
PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 50, 50)).TakePath()));
auto result =
clip->GetStencilCoverage(Entity{}, Rect::MakeLTRB(0, 0, 100, 100));
ASSERT_TRUE(result.coverage.has_value());
ASSERT_RECT_NEAR(result.coverage.value(), Rect::MakeLTRB(0, 0, 100, 100));
ASSERT_EQ(result.type, Contents::StencilCoverage::Type::kAppend);
}
}
TEST_P(EntityTest, RRectShadowTest) {
auto callback = [&](ContentContext& context, RenderPass& pass) {
static Color color = Color::Red();