Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Fix coverage values when a stroked path is rotated #40917

Merged
merged 8 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ using AiksTest = AiksPlayground;
#endif
INSTANTIATE_PLAYGROUND_SUITE(AiksTest);

TEST_P(AiksTest, RotateColorFilteredPath) {
Canvas canvas;
canvas.Concat(Matrix::MakeTranslation({300, 300}));
canvas.Concat(Matrix::MakeRotationZ(Radians(kPiOver2)));
auto arrow_stem =
PathBuilder{}.MoveTo({120, 190}).LineTo({120, 50}).TakePath();
auto arrow_head = PathBuilder{}
.MoveTo({50, 120})
.LineTo({120, 190})
.LineTo({190, 120})
.TakePath();
auto paint = Paint{
.stroke_width = 15.0,
.stroke_cap = Cap::kRound,
.stroke_join = Join::kRound,
.style = Paint::Style::kStroke,
.color_filter =
[](FilterInput::Ref input) {
return ColorFilterContents::MakeBlend(
BlendMode::kSourceIn, {std::move(input)}, Color::AliceBlue());
},
};

canvas.DrawPath(arrow_stem, paint);
canvas.DrawPath(arrow_head, paint);
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

TEST_P(AiksTest, CanvasCTMCanBeUpdated) {
Canvas canvas;
Matrix identity;
Expand Down
32 changes: 32 additions & 0 deletions impeller/entity/entity_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2451,5 +2451,37 @@ TEST_P(EntityTest, InheritOpacityTest) {
ASSERT_FALSE(runtime_effect->CanInheritOpacity(entity));
}

TEST_P(EntityTest, ColorFilterWithForegroundColorAdvancedBlend) {
auto image = CreateTextureForFixture("boston.jpg");
auto filter = ColorFilterContents::MakeBlend(
BlendMode::kColorBurn, FilterInput::Make({image}), Color::Red());

auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
Entity entity;
entity.SetTransformation(Matrix::MakeScale(GetContentScale()) *
Matrix::MakeTranslation({500, 300}) *
Matrix::MakeScale(Vector2{0.5, 0.5}));
entity.SetContents(filter);
return entity.Render(context, pass);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}

TEST_P(EntityTest, CoverageForStrokePathWithNegativeValuesInTransform) {
auto arrow_head = PathBuilder{}
.MoveTo({50, 120})
.LineTo({120, 190})
.LineTo({190, 120})
.TakePath();
auto geometry = Geometry::MakeStrokePath(arrow_head, 15.0, 4.0, Cap::kRound,
Join::kRound);

auto transform = Matrix::MakeTranslation({300, 300}) *
Matrix::MakeRotationZ(Radians(kPiOver2));
EXPECT_LT(transform.e[0][0], 0.f);
auto coverage = geometry->GetCoverage(transform);
ASSERT_RECT_NEAR(coverage.value(), Rect::MakeXYWH(102.5, 342.5, 85, 155));
}

} // namespace testing
} // namespace impeller
8 changes: 5 additions & 3 deletions impeller/entity/geometry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,11 @@ std::optional<Rect> StrokePathGeometry::GetCoverage(
return std::nullopt;
}
Scalar min_size = 1.0f / sqrt(std::abs(determinant));
Vector2 max_radius_xy = transform.TransformDirection(
Vector2(max_radius, max_radius) * std::max(stroke_width_, min_size));

Vector2 max_radius_xy =
transform
.TransformDirection(Vector2(max_radius, max_radius) *
std::max(stroke_width_, min_size))
.Abs();
return Rect(path_coverage.origin - max_radius_xy,
Size(path_coverage.size.width + max_radius_xy.x * 2,
path_coverage.size.height + max_radius_xy.y * 2));
Expand Down