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

[impeller] convert src over to src for solid color #41351

Merged
merged 1 commit into from
Apr 20, 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
8 changes: 8 additions & 0 deletions impeller/entity/contents/solid_color_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ bool SolidColorContents::ShouldRender(
return Contents::ShouldRender(entity, stencil_coverage);
}

bool SolidColorContents::ConvertToSrc(const Entity& entity) const {
return entity.GetBlendMode() == BlendMode::kSourceOver &&
GetColor().alpha >= 1.0;
}

bool SolidColorContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
Expand All @@ -60,6 +65,9 @@ bool SolidColorContents::Render(const ContentContext& renderer,
GetGeometry()->GetPositionBuffer(renderer, entity, pass);

auto options = OptionsFromPassAndEntity(pass, entity);
if (ConvertToSrc(entity)) {
options.blend_mode = BlendMode::kSource;
}
if (geometry_result.prevent_overdraw) {
options.stencil_compare = CompareFunction::kEqual;
options.stencil_operation = StencilOperation::kIncrementClamp;
Expand Down
4 changes: 4 additions & 0 deletions impeller/entity/contents/solid_color_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class SolidColorContents final : public ColorSourceContents {
const Entity& entity,
RenderPass& pass) const override;

/// @brief Convert SrcOver blend modes into Src blend modes if the color has
/// no opacity.
bool ConvertToSrc(const Entity& entity) const;

private:
Color color_;

Expand Down
20 changes: 20 additions & 0 deletions impeller/entity/entity_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2547,5 +2547,25 @@ TEST_P(EntityTest, CoverageForStrokePathWithNegativeValuesInTransform) {
ASSERT_RECT_NEAR(coverage.value(), Rect::MakeXYWH(102.5, 342.5, 85, 155));
}

TEST_P(EntityTest, ConvertToSrcBlend) {
Entity entity;
entity.SetBlendMode(BlendMode::kSourceOver);

auto contents = SolidColorContents::Make(
PathBuilder{}.AddRect(Rect::MakeSize(Size(100, 100))).TakePath(),
Color::Red());

ASSERT_TRUE(contents->ConvertToSrc(entity));

// Color with alpha, should return false.
contents->SetInheritedOpacity(0.5);
ASSERT_FALSE(contents->ConvertToSrc(entity));

// Non source over blend mode, should return false.
contents->SetInheritedOpacity(1.0);
entity.SetBlendMode(BlendMode::kDestination);
ASSERT_FALSE(contents->ConvertToSrc(entity));
}

} // namespace testing
} // namespace impeller