Skip to content

Commit 8d916f6

Browse files
committed
[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) (cherry picked from commit 3e5ae6a)
1 parent e14db68 commit 8d916f6

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,5 +2837,33 @@ TEST_P(AiksTest, TextForegroundShaderWithTransform) {
28372837
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
28382838
}
28392839

2840+
TEST_P(AiksTest, MatrixBackdropFilter) {
2841+
Canvas canvas;
2842+
canvas.DrawPaint({.color = Color::Black()});
2843+
canvas.SaveLayer({}, std::nullopt);
2844+
{
2845+
canvas.DrawCircle(Point(200, 200), 100,
2846+
{.color = Color::Green().WithAlpha(0.5),
2847+
.blend_mode = BlendMode::kPlus});
2848+
// Should render a second circle, centered on the bottom-right-most edge of
2849+
// the circle.
2850+
canvas.SaveLayer({}, std::nullopt,
2851+
[](const FilterInput::Ref& input,
2852+
const Matrix& effect_transform, bool is_subpass) {
2853+
Matrix matrix =
2854+
Matrix::MakeTranslation(Vector2(1, 1) *
2855+
(100 + 100 * k1OverSqrt2)) *
2856+
Matrix::MakeScale(Vector2(1, 1) * 0.2) *
2857+
Matrix::MakeTranslation(Vector2(-100, -100));
2858+
return FilterContents::MakeMatrixFilter(
2859+
input, matrix, {}, Matrix(), true);
2860+
});
2861+
canvas.Restore();
2862+
}
2863+
canvas.Restore();
2864+
2865+
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
2866+
}
2867+
28402868
} // namespace testing
28412869
} // namespace impeller

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
@@ -434,12 +434,12 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
434434
return EntityPass::EntityResult::Skip();
435435
}
436436

437-
std::shared_ptr<Contents> backdrop_filter_contents = nullptr;
437+
std::shared_ptr<Contents> subpass_backdrop_filter_contents = nullptr;
438438
if (subpass->backdrop_filter_proc_) {
439439
auto texture = pass_context.GetTexture();
440440
// Render the backdrop texture before any of the pass elements.
441441
const auto& proc = subpass->backdrop_filter_proc_;
442-
backdrop_filter_contents =
442+
subpass_backdrop_filter_contents =
443443
proc(FilterInput::Make(std::move(texture)), subpass->xformation_,
444444
/*is_subpass*/ true);
445445

@@ -476,9 +476,10 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
476476
return EntityPass::EntityResult::Skip();
477477
}
478478

479-
auto subpass_coverage = (subpass->flood_clip_ || backdrop_filter_contents)
480-
? coverage_limit
481-
: GetSubpassCoverage(*subpass, coverage_limit);
479+
auto subpass_coverage =
480+
(subpass->flood_clip_ || subpass_backdrop_filter_contents)
481+
? coverage_limit
482+
: GetSubpassCoverage(*subpass, coverage_limit);
482483
if (!subpass_coverage.has_value()) {
483484
return EntityPass::EntityResult::Skip();
484485
}
@@ -501,17 +502,18 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
501502

502503
// Stencil textures aren't shared between EntityPasses (as much of the
503504
// time they are transient).
504-
if (!subpass->OnRender(renderer, // renderer
505-
root_pass_size, // root_pass_size
506-
subpass_target, // pass_target
507-
subpass_coverage->origin, // global_pass_position
508-
subpass_coverage->origin -
509-
global_pass_position, // local_pass_position
510-
++pass_depth, // pass_depth
511-
stencil_coverage_stack, // stencil_coverage_stack
512-
subpass->stencil_depth_, // stencil_depth_floor
513-
backdrop_filter_contents // backdrop_filter_contents
514-
)) {
505+
if (!subpass->OnRender(
506+
renderer, // renderer
507+
root_pass_size, // root_pass_size
508+
subpass_target, // pass_target
509+
subpass_coverage->origin, // global_pass_position
510+
subpass_coverage->origin -
511+
global_pass_position, // local_pass_position
512+
++pass_depth, // pass_depth
513+
stencil_coverage_stack, // stencil_coverage_stack
514+
subpass->stencil_depth_, // stencil_depth_floor
515+
subpass_backdrop_filter_contents // backdrop_filter_contents
516+
)) {
515517
// Validation error messages are triggered for all `OnRender()` failure
516518
// cases.
517519
return EntityPass::EntityResult::Failure();

0 commit comments

Comments
 (0)