[Impeller] Provide the clear color to an advanced blend if it was optimized out (flutter/engine#48646)

If all previous elements were skipped due to clear color optimization,
then the clear color must be rendered as an input to the advanced blend
if framebuffer fetch is not available.
This commit is contained in:
Jason Simmons
2023-12-06 10:46:22 -08:00
committed by GitHub
parent ea34df3c56
commit 2de054c92a
3 changed files with 36 additions and 2 deletions

View File

@@ -4249,5 +4249,17 @@ TEST_P(AiksTest, MaskBlurWithZeroSigmaIsSkipped) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, AdvancedBlendWithClearColorOptimization) {
Canvas canvas;
canvas.DrawPaint(
{.color = {1.0, 1.0, 0.0, 1.0}, .blend_mode = BlendMode::kSource});
canvas.DrawRect(
Rect::MakeXYWH(0, 0, 200, 300),
{.color = {1.0, 0.0, 1.0, 1.0}, .blend_mode = BlendMode::kMultiply});
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
} // namespace testing
} // namespace impeller

View File

@@ -19,6 +19,7 @@
#include "impeller/entity/contents/filters/color_filter_contents.h"
#include "impeller/entity/contents/filters/inputs/filter_input.h"
#include "impeller/entity/contents/framebuffer_blend_contents.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/contents/texture_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/entity/inline_pass_context.h"
@@ -877,6 +878,7 @@ bool EntityPass::OnRender(
!backdrop_filter_proc_;
for (const auto& element : elements_) {
// Skip elements that are incorporated into the clear color.
bool was_collapsing_clear_colors = is_collapsing_clear_colors;
if (is_collapsing_clear_colors) {
auto [entity_color, _] =
ElementAsBackgroundColor(element, clear_color_size);
@@ -950,9 +952,20 @@ bool EntityPass::OnRender(
FilterInput::Vector inputs = {
FilterInput::Make(texture, result.entity.GetTransform().Invert()),
FilterInput::Make(result.entity.GetContents())};
std::optional<Color> foreground_color;
std::optional<Rect> coverage;
if (was_collapsing_clear_colors) {
// If all previous elements were skipped due to clear color
// optimization, then provide the clear color as the foreground of the
// advanced blend.
foreground_color = GetClearColor(clear_color_size);
coverage = Rect::MakeSize(clear_color_size);
} else {
coverage = result.entity.GetCoverage();
}
auto contents = ColorFilterContents::MakeBlend(
result.entity.GetBlendMode(), inputs);
contents->SetCoverageHint(result.entity.GetCoverage());
result.entity.GetBlendMode(), inputs, foreground_color);
contents->SetCoverageHint(coverage);
result.entity.SetContents(std::move(contents));
result.entity.SetBlendMode(BlendMode::kSource);
}

View File

@@ -2457,6 +2457,14 @@ TEST_P(EntityTest, AdvancedBlendCoverageHintIsNotResetByEntityPass) {
"for advanced blends.";
}
auto background_contents = std::make_shared<SolidColorContents>();
background_contents->SetGeometry(
Geometry::MakeRect(Rect::MakeXYWH(200, 200, 200, 200)));
background_contents->SetColor(Color::Blue());
Entity background_entity;
background_entity.SetTransform(Matrix::MakeScale(Vector3(2, 2, 1)));
background_entity.SetContents(background_contents);
auto contents = std::make_shared<SolidColorContents>();
contents->SetGeometry(Geometry::MakeRect(Rect::MakeXYWH(100, 100, 100, 100)));
contents->SetColor(Color::Red());
@@ -2482,6 +2490,7 @@ TEST_P(EntityTest, AdvancedBlendCoverageHintIsNotResetByEntityPass) {
RenderTarget::kDefaultColorAttachmentConfig, stencil_config);
auto content_context = ContentContext(
GetContext(), TypographerContextSkia::Make(), test_allocator);
pass->AddEntity(std::move(background_entity));
pass->AddEntity(std::move(entity));
EXPECT_TRUE(pass->Render(content_context, rt));