[Impeller] Absorb root pass DrawPaints for most blend modes (flutter/engine#41088)

We can be a lot more aggressive with absorbing DrawPaints by just
cheaply blending them together on the CPU. We're missing some blend
modes at the moment but can slot those in later.
This commit is contained in:
Brandon DeRosier
2023-04-11 17:40:06 -07:00
committed by GitHub
parent e70c6aed44
commit 1f6af2f479
3 changed files with 20 additions and 12 deletions

View File

@@ -1230,11 +1230,17 @@ TEST_P(AiksTest, TextRotated) {
}
TEST_P(AiksTest, CanDrawPaint) {
Paint paint;
paint.color = Color::MediumTurquoise();
Canvas canvas;
canvas.Scale(Vector2(0.2, 0.2));
canvas.DrawPaint(paint);
canvas.DrawPaint({.color = Color::MediumTurquoise()});
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, CanDrawPaintMultipleTimes) {
Canvas canvas;
canvas.Scale(Vector2(0.2, 0.2));
canvas.DrawPaint({.color = Color::MediumTurquoise()});
canvas.DrawPaint({.color = Color::Color::OrangeRed().WithAlpha(0.5)});
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

View File

@@ -167,15 +167,16 @@ void Canvas::DrawPath(const Path& path, const Paint& paint) {
}
void Canvas::DrawPaint(const Paint& paint) {
bool is_clear =
paint.blend_mode == BlendMode::kSource ||
(paint.blend_mode == BlendMode::kSourceOver && paint.color.alpha == 1);
if (xformation_stack_.size() == 1 && // If we're recording the root pass,
GetCurrentPass().GetElementCount() == 0 && // and this is the first item,
is_clear // and the backdrop is being replaced
// TODO(bdero): Fix the advanced blends in Color::BlendColor.
// https://github.com/flutter/flutter/issues/124623
paint.blend_mode <= Entity::kLastPipelineBlendMode //
) {
// Then we can absorb this drawPaint as the clear color of the pass.
GetCurrentPass().SetClearColor(paint.color);
auto color = Color::BlendColor(
paint.color, GetCurrentPass().GetClearColor(), paint.blend_mode);
GetCurrentPass().SetClearColor(color);
return;
}

View File

@@ -214,8 +214,9 @@ bool EntityPass::Render(ContentContext& renderer,
.stencil_depth = 0}};
if (GetTotalPassReads(renderer) > 0) {
auto offscreen_target = CreateRenderTarget(
renderer, render_target.GetRenderTargetSize(), true, clear_color_);
auto offscreen_target =
CreateRenderTarget(renderer, render_target.GetRenderTargetSize(), true,
clear_color_.Premultiply());
if (!OnRender(renderer, // renderer
offscreen_target.GetRenderTarget()
@@ -277,7 +278,7 @@ bool EntityPass::Render(ContentContext& renderer,
// Set up the clear color of the root pass.
auto color0 = render_target.GetColorAttachments().find(0)->second;
color0.clear_color = clear_color_;
color0.clear_color = clear_color_.Premultiply();
auto root_render_target = render_target;
root_render_target.SetColorAttachment(color0, 0);
@@ -402,7 +403,7 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
CreateRenderTarget(renderer, //
ISize(subpass_coverage->size), //
subpass->GetTotalPassReads(renderer) > 0, //
clear_color_);
clear_color_.Premultiply());
if (!subpass_target.GetRenderTarget().GetRenderTargetTexture()) {
return EntityPass::EntityResult::Failure();