From d540929cef21b09ad14d7ea9b9bbe31c9b732b1c Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Wed, 7 Sep 2022 07:49:38 +0800 Subject: [PATCH] [Impeller] Make 'MorphologyFilterContents' use 'effect_transform' (flutter/engine#35938) --- .../display_list/display_list_dispatcher.cc | 8 +++++--- .../entity/contents/filters/filter_contents.cc | 16 ++++++++++------ .../entity/contents/filters/filter_contents.h | 13 ++++++++----- .../filters/morphology_filter_contents.cc | 14 ++++++-------- .../flutter/impeller/entity/entity_unittests.cc | 8 +++++++- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index 9f118a45c6..33b99afa39 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -556,7 +556,8 @@ static std::optional ToImageFilterProc( return [radius_x, radius_y](FilterInput::Ref input, const Matrix& effect_transform) { return FilterContents::MakeMorphology( - input, radius_x, radius_y, FilterContents::MorphType::kDilate); + input, radius_x, radius_y, FilterContents::MorphType::kDilate, + effect_transform); }; break; } @@ -570,8 +571,9 @@ static std::optional ToImageFilterProc( auto radius_y = Radius(erode->radius_y()); return [radius_x, radius_y](FilterInput::Ref input, const Matrix& effect_transform) { - return FilterContents::MakeMorphology( - input, radius_x, radius_y, FilterContents::MorphType::kErode); + return FilterContents::MakeMorphology(input, radius_x, radius_y, + FilterContents::MorphType::kErode, + effect_transform); }; break; } diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc index 925061e61c..9ac5585614 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -128,12 +128,14 @@ std::shared_ptr FilterContents::MakeDirectionalMorphology( FilterInput::Ref input, Radius radius, Vector2 direction, - MorphType morph_type) { + MorphType morph_type, + const Matrix& effect_transform) { auto filter = std::make_shared(); filter->SetInputs({input}); filter->SetRadius(radius); filter->SetDirection(direction); filter->SetMorphType(morph_type); + filter->SetEffectTransform(effect_transform); return filter; } @@ -141,11 +143,13 @@ std::shared_ptr FilterContents::MakeMorphology( FilterInput::Ref input, Radius radius_x, Radius radius_y, - MorphType morph_type) { - auto x_morphology = - MakeDirectionalMorphology(input, radius_x, Point(1, 0), morph_type); - auto y_morphology = MakeDirectionalMorphology( - FilterInput::Make(x_morphology), radius_y, Point(0, 1), morph_type); + MorphType morph_type, + const Matrix& effect_transform) { + auto x_morphology = MakeDirectionalMorphology(input, radius_x, Point(1, 0), + morph_type, effect_transform); + auto y_morphology = + MakeDirectionalMorphology(FilterInput::Make(x_morphology), radius_y, + Point(0, 1), morph_type, effect_transform); return y_morphology; } diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h index a0c1e5996b..9f5637f7cb 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h @@ -70,12 +70,15 @@ class FilterContents : public Contents { FilterInput::Ref input, Radius radius, Vector2 direction, - MorphType morph_type); + MorphType morph_type, + const Matrix& effect_transform = Matrix()); - static std::shared_ptr MakeMorphology(FilterInput::Ref input, - Radius radius_x, - Radius radius_y, - MorphType morph_type); + static std::shared_ptr MakeMorphology( + FilterInput::Ref input, + Radius radius_x, + Radius radius_y, + MorphType morph_type, + const Matrix& effect_transform = Matrix()); static std::shared_ptr MakeColorMatrix( FilterInput::Ref input, diff --git a/engine/src/flutter/impeller/entity/contents/filters/morphology_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/morphology_filter_contents.cc index a8f5af1b5f..5a84e1176f 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/morphology_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/morphology_filter_contents.cc @@ -88,9 +88,9 @@ std::optional DirectionalMorphologyFilterContents::RenderFilter( VS::FrameInfo frame_info; frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1)); - - auto transformed_radius = entity.GetTransformation().TransformDirection( - direction_ * radius_.radius); + auto transform = entity.GetTransformation() * effect_transform; + auto transformed_radius = + transform.TransformDirection(direction_ * radius_.radius); auto transformed_texture_vertices = Rect(Size(input_snapshot->texture->GetSize())) .GetTransformedPoints(input_snapshot->transform); @@ -156,11 +156,9 @@ std::optional DirectionalMorphologyFilterContents::GetFilterCoverage( if (!coverage.has_value()) { return std::nullopt; } - - auto transformed_vector = inputs[0] - ->GetTransform(entity) - .TransformDirection(direction_ * radius_.radius) - .Abs(); + auto transform = inputs[0]->GetTransform(entity) * effect_transform; + auto transformed_vector = + transform.TransformDirection(direction_ * radius_.radius).Abs(); auto extent = coverage->size + transformed_vector * 2; return Rect(coverage->origin - transformed_vector, Size(extent.x, extent.y)); diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index fcbc8bd209..bdd5258304 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -1018,6 +1018,7 @@ TEST_P(EntityTest, MorphologyFilter) { static float path_rect[4] = {0, 0, static_cast(boston->GetSize().width), static_cast(boston->GetSize().height)}; + static float effect_transform_scale = 1; ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize); { @@ -1035,6 +1036,8 @@ TEST_P(EntityTest, MorphologyFilter) { ImGui::SliderFloat2("Scale", scale, 0, 3); ImGui::SliderFloat2("Skew", skew, -3, 3); ImGui::SliderFloat4("Path XYWH", path_rect, -1000, 1000); + ImGui::SliderFloat("Effect transform scale", &effect_transform_scale, 0, + 3); } ImGui::End(); @@ -1052,9 +1055,12 @@ TEST_P(EntityTest, MorphologyFilter) { input = texture; input_size = input_rect.size; + auto effect_transform = Matrix::MakeScale( + Vector2{effect_transform_scale, effect_transform_scale}); + auto contents = FilterContents::MakeMorphology( FilterInput::Make(input), Radius{radius[0]}, Radius{radius[1]}, - morphology_types[selected_morphology_type]); + morphology_types[selected_morphology_type], effect_transform); auto ctm = Matrix::MakeScale(GetContentScale()) * Matrix::MakeTranslation(Vector3(offset[0], offset[1])) *