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

Commit 1edc350

Browse files
authored
[Impeller] Make transparent fills have no coverage (#34055)
1 parent fb80e86 commit 1edc350

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

impeller/entity/contents/solid_color_contents.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ void SolidColorContents::SetCover(bool cover) {
3535

3636
std::optional<Rect> SolidColorContents::GetCoverage(
3737
const Entity& entity) const {
38+
if (color_.IsTransparent()) {
39+
return std::nullopt;
40+
}
3841
return path_.GetTransformedBoundingBox(entity.GetTransformation());
3942
};
4043

@@ -60,10 +63,6 @@ VertexBuffer SolidColorContents::CreateSolidFillVertices(const Path& path,
6063
bool SolidColorContents::Render(const ContentContext& renderer,
6164
const Entity& entity,
6265
RenderPass& pass) const {
63-
if (color_.IsTransparent()) {
64-
return true;
65-
}
66-
6766
using VS = SolidFillPipeline::VertexShader;
6867

6968
Command cmd;

impeller/entity/contents/solid_stroke_contents.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ void SolidStrokeContents::SetPath(Path path) {
3535

3636
std::optional<Rect> SolidStrokeContents::GetCoverage(
3737
const Entity& entity) const {
38+
if (color_.IsTransparent()) {
39+
return std::nullopt;
40+
}
41+
3842
auto path_bounds = path_.GetBoundingBox();
3943
if (!path_bounds.has_value()) {
4044
return std::nullopt;
@@ -173,7 +177,7 @@ static VertexBuffer CreateSolidStrokeVertices(
173177
bool SolidStrokeContents::Render(const ContentContext& renderer,
174178
const Entity& entity,
175179
RenderPass& pass) const {
176-
if (color_.IsTransparent() || stroke_size_ <= 0.0) {
180+
if (stroke_size_ <= 0.0) {
177181
return true;
178182
}
179183

impeller/entity/contents/texture_contents.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "texture_contents.h"
66

7+
#include <optional>
8+
79
#include "impeller/entity/contents/content_context.h"
810
#include "impeller/entity/entity.h"
911
#include "impeller/entity/texture_fill.frag.h"
@@ -35,6 +37,9 @@ void TextureContents::SetOpacity(Scalar opacity) {
3537
}
3638

3739
std::optional<Rect> TextureContents::GetCoverage(const Entity& entity) const {
40+
if (opacity_ == 0) {
41+
return std::nullopt;
42+
}
3843
return path_.GetTransformedBoundingBox(entity.GetTransformation());
3944
};
4045

impeller/entity/entity_unittests.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) {
925925
contents->SetStrokeCap(SolidStrokeContents::Cap::kButt);
926926
contents->SetStrokeJoin(SolidStrokeContents::Join::kBevel);
927927
contents->SetStrokeSize(4);
928+
contents->SetColor(Color::Black());
928929
entity.SetContents(std::move(contents));
929930
auto actual = entity.GetCoverage();
930931
auto expected = Rect::MakeLTRB(-2, -2, 12, 12);
@@ -940,6 +941,7 @@ TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) {
940941
contents->SetStrokeCap(SolidStrokeContents::Cap::kSquare);
941942
contents->SetStrokeJoin(SolidStrokeContents::Join::kBevel);
942943
contents->SetStrokeSize(4);
944+
contents->SetColor(Color::Black());
943945
entity.SetContents(std::move(contents));
944946
auto actual = entity.GetCoverage();
945947
auto expected =
@@ -957,6 +959,7 @@ TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) {
957959
contents->SetStrokeJoin(SolidStrokeContents::Join::kMiter);
958960
contents->SetStrokeSize(4);
959961
contents->SetStrokeMiter(2);
962+
contents->SetColor(Color::Black());
960963
entity.SetContents(std::move(contents));
961964
auto actual = entity.GetCoverage();
962965
auto expected = Rect::MakeLTRB(-4, -4, 14, 14);
@@ -1011,5 +1014,47 @@ TEST_P(EntityTest, DrawVerticesSolidColorTrianglesWithoutIndex) {
10111014
ASSERT_TRUE(OpenPlaygroundHere(e));
10121015
}
10131016

1017+
TEST_P(EntityTest, SolidFillCoverageIsCorrect) {
1018+
// No transform
1019+
{
1020+
auto fill = std::make_shared<SolidColorContents>();
1021+
fill->SetColor(Color::CornflowerBlue());
1022+
auto expected = Rect::MakeLTRB(100, 110, 200, 220);
1023+
fill->SetPath(PathBuilder{}.AddRect(expected).TakePath());
1024+
1025+
auto coverage = fill->GetCoverage({});
1026+
ASSERT_TRUE(coverage.has_value());
1027+
ASSERT_RECT_NEAR(coverage.value(), expected);
1028+
}
1029+
1030+
// Entity transform
1031+
{
1032+
auto fill = std::make_shared<SolidColorContents>();
1033+
fill->SetColor(Color::CornflowerBlue());
1034+
fill->SetPath(
1035+
PathBuilder{}.AddRect(Rect::MakeLTRB(100, 110, 200, 220)).TakePath());
1036+
1037+
Entity entity;
1038+
entity.SetTransformation(Matrix::MakeTranslation(Vector2(4, 5)));
1039+
entity.SetContents(std::move(fill));
1040+
1041+
auto coverage = entity.GetCoverage();
1042+
auto expected = Rect::MakeLTRB(104, 115, 204, 225);
1043+
ASSERT_TRUE(coverage.has_value());
1044+
ASSERT_RECT_NEAR(coverage.value(), expected);
1045+
}
1046+
1047+
// No coverage for fully transparent colors
1048+
{
1049+
auto fill = std::make_shared<SolidColorContents>();
1050+
fill->SetColor(Color::WhiteTransparent());
1051+
fill->SetPath(
1052+
PathBuilder{}.AddRect(Rect::MakeLTRB(100, 110, 200, 220)).TakePath());
1053+
1054+
auto coverage = fill->GetCoverage({});
1055+
ASSERT_FALSE(coverage.has_value());
1056+
}
1057+
}
1058+
10141059
} // namespace testing
10151060
} // namespace impeller

0 commit comments

Comments
 (0)