Skip to content

Commit 3e5ae6a

Browse files
authored
[Impeller] Use basis of effect transform in MatrixFilter. (flutter#43990)
Follow-up/reland of flutter#43943. Issue flutter/flutter#130355. I've updated the golden to better reproduce the problem at hand; the second circle is a scaled down copy of the backdrop. If the matrix filter is handling the transform correctly, the circle is centered over the bottom-right-most edge (4:30) of the circle: ![image](https://github.com/flutter/engine/assets/919017/3f2e099d-9d43-4ee6-8efe-3203edad47ad)
1 parent 21203f9 commit 3e5ae6a

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,15 +2932,27 @@ TEST_P(AiksTest, DrawPictureWithText) {
29322932

29332933
TEST_P(AiksTest, MatrixBackdropFilter) {
29342934
Canvas canvas;
2935-
canvas.SaveLayer({}, std::nullopt,
2936-
[](const FilterInput::Ref& input,
2937-
const Matrix& effect_transform, bool is_subpass) {
2938-
return FilterContents::MakeMatrixFilter(
2939-
input, Matrix::MakeTranslation(Vector2(100, 100)), {},
2940-
Matrix(), true);
2941-
});
2942-
canvas.DrawCircle(Point(100, 100), 100,
2943-
{.color = Color::Green(), .blend_mode = BlendMode::kPlus});
2935+
canvas.DrawPaint({.color = Color::Black()});
2936+
canvas.SaveLayer({}, std::nullopt);
2937+
{
2938+
canvas.DrawCircle(Point(200, 200), 100,
2939+
{.color = Color::Green().WithAlpha(0.5),
2940+
.blend_mode = BlendMode::kPlus});
2941+
// Should render a second circle, centered on the bottom-right-most edge of
2942+
// the circle.
2943+
canvas.SaveLayer({}, std::nullopt,
2944+
[](const FilterInput::Ref& input,
2945+
const Matrix& effect_transform, bool is_subpass) {
2946+
Matrix matrix =
2947+
Matrix::MakeTranslation(Vector2(1, 1) *
2948+
(100 + 100 * k1OverSqrt2)) *
2949+
Matrix::MakeScale(Vector2(1, 1) * 0.2) *
2950+
Matrix::MakeTranslation(Vector2(-100, -100));
2951+
return FilterContents::MakeMatrixFilter(
2952+
input, matrix, {}, Matrix(), true);
2953+
});
2954+
canvas.Restore();
2955+
}
29442956
canvas.Restore();
29452957

29462958
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));

impeller/entity/contents/filters/matrix_filter_contents.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,28 @@ std::optional<Entity> MatrixFilterContents::RenderFilter(
3434
return std::nullopt;
3535
}
3636

37-
auto& transform = is_subpass_ ? effect_transform : entity.GetTransformation();
37+
// The filter's matrix needs to be applied within the space defined by the
38+
// scene's current transformation matrix (CTM). For example: If the CTM is
39+
// scaled up, then translations applied by the matrix should be magnified
40+
// accordingly.
41+
//
42+
// To accomplish this, we sandwitch the filter's matrix within the CTM in both
43+
// cases. But notice that for the subpass backdrop filter case, we use the
44+
// "effect transform" instead of the Entity's transform!
45+
//
46+
// That's because in the subpass backdrop filter case, the Entity's transform
47+
// isn't actually the captured CTM of the scene like it usually is; instead,
48+
// it's just a screen space translation that offsets the backdrop texture (as
49+
// mentioned above). And so we sneak the subpass's captured CTM in through the
50+
// effect transform.
51+
52+
auto transform = is_subpass_ ? effect_transform.Basis()
53+
: entity.GetTransformation().Basis();
3854
snapshot->transform = transform * //
3955
matrix_ * //
4056
transform.Invert() * //
4157
snapshot->transform;
58+
4259
snapshot->sampler_descriptor = sampler_descriptor_;
4360
return Entity::FromSnapshot(snapshot, entity.GetBlendMode(),
4461
entity.GetStencilDepth());

impeller/entity/entity_pass.cc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,12 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
465465
return EntityPass::EntityResult::Skip();
466466
}
467467

468-
std::shared_ptr<Contents> backdrop_filter_contents = nullptr;
468+
std::shared_ptr<Contents> subpass_backdrop_filter_contents = nullptr;
469469
if (subpass->backdrop_filter_proc_) {
470470
auto texture = pass_context.GetTexture();
471471
// Render the backdrop texture before any of the pass elements.
472472
const auto& proc = subpass->backdrop_filter_proc_;
473-
backdrop_filter_contents =
473+
subpass_backdrop_filter_contents =
474474
proc(FilterInput::Make(std::move(texture)), subpass->xformation_,
475475
/*is_subpass*/ true);
476476

@@ -507,9 +507,10 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
507507
return EntityPass::EntityResult::Skip();
508508
}
509509

510-
auto subpass_coverage = (subpass->flood_clip_ || backdrop_filter_contents)
511-
? coverage_limit
512-
: GetSubpassCoverage(*subpass, coverage_limit);
510+
auto subpass_coverage =
511+
(subpass->flood_clip_ || subpass_backdrop_filter_contents)
512+
? coverage_limit
513+
: GetSubpassCoverage(*subpass, coverage_limit);
513514
if (!subpass_coverage.has_value()) {
514515
return EntityPass::EntityResult::Skip();
515516
}
@@ -532,17 +533,18 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
532533

533534
// Stencil textures aren't shared between EntityPasses (as much of the
534535
// time they are transient).
535-
if (!subpass->OnRender(renderer, // renderer
536-
root_pass_size, // root_pass_size
537-
subpass_target, // pass_target
538-
subpass_coverage->origin, // global_pass_position
539-
subpass_coverage->origin -
540-
global_pass_position, // local_pass_position
541-
++pass_depth, // pass_depth
542-
stencil_coverage_stack, // stencil_coverage_stack
543-
subpass->stencil_depth_, // stencil_depth_floor
544-
backdrop_filter_contents // backdrop_filter_contents
545-
)) {
536+
if (!subpass->OnRender(
537+
renderer, // renderer
538+
root_pass_size, // root_pass_size
539+
subpass_target, // pass_target
540+
subpass_coverage->origin, // global_pass_position
541+
subpass_coverage->origin -
542+
global_pass_position, // local_pass_position
543+
++pass_depth, // pass_depth
544+
stencil_coverage_stack, // stencil_coverage_stack
545+
subpass->stencil_depth_, // stencil_depth_floor
546+
subpass_backdrop_filter_contents // backdrop_filter_contents
547+
)) {
546548
// Validation error messages are triggered for all `OnRender()` failure
547549
// cases.
548550
return EntityPass::EntityResult::Failure();

0 commit comments

Comments
 (0)