diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 0ad24986db..643fd39f80 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -586,6 +586,8 @@ FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_in FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h FILE: ../../../flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc FILE: ../../../flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.h +FILE: ../../../flutter/impeller/entity/contents/filters/matrix_filter_contents.cc +FILE: ../../../flutter/impeller/entity/contents/filters/matrix_filter_contents.h FILE: ../../../flutter/impeller/entity/contents/filters/morphology_filter_contents.cc FILE: ../../../flutter/impeller/entity/contents/filters/morphology_filter_contents.h FILE: ../../../flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc 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 33b99afa39..19ed32ef1a 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -577,7 +577,15 @@ static std::optional ToImageFilterProc( }; break; } - case flutter::DlImageFilterType::kMatrix: + case flutter::DlImageFilterType::kMatrix: { + auto matrix_filter = filter->asMatrix(); + FML_DCHECK(matrix_filter); + auto matrix = ToMatrix(matrix_filter->matrix()); + return [matrix](FilterInput::Ref input, const Matrix& effect_transform) { + return FilterContents::MakeMatrixFilter(input, matrix); + }; + break; + } case flutter::DlImageFilterType::kLocalMatrixFilter: case flutter::DlImageFilterType::kComposeFilter: case flutter::DlImageFilterType::kColorFilter: diff --git a/engine/src/flutter/impeller/display_list/display_list_unittests.cc b/engine/src/flutter/impeller/display_list/display_list_unittests.cc index f09fa4a40b..64ba8fff85 100644 --- a/engine/src/flutter/impeller/display_list/display_list_unittests.cc +++ b/engine/src/flutter/impeller/display_list/display_list_unittests.cc @@ -109,7 +109,7 @@ TEST_P(DisplayListTest, CanDrawArc) { static float sweep_angle = 270; static bool use_center = true; - ImGui::Begin("Controls"); + ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize); ImGui::SliderFloat("Start angle", &start_angle, -360, 360); ImGui::SliderFloat("Sweep angle", &sweep_angle, -360, 360); ImGui::Checkbox("Use center", &use_center); @@ -583,5 +583,85 @@ TEST_P(DisplayListTest, CanDrawZeroWidthLine) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +TEST_P(DisplayListTest, CanDrawWithMatrixFilter) { + auto boston = CreateTextureForFixture("boston.jpg"); + + bool first_frame = true; + auto callback = [&]() { + if (first_frame) { + first_frame = false; + ImGui::SetNextWindowPos({10, 10}); + } + + static float ctm_translation[2] = {200, 200}; + static float ctm_scale[2] = {0.65, 0.65}; + static float ctm_skew[2] = {0, 0}; + + static bool enable = true; + static float translation[2] = {100, 100}; + static float scale[2] = {0.8, 0.8}; + static float skew[2] = {0.2, 0.2}; + + static bool enable_savelayer = true; + + ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize); + { + ImGui::TextWrapped("Current Transform"); + ImGui::SliderFloat2("CTM Translation", ctm_translation, 0, 1000); + ImGui::SliderFloat2("CTM Scale", ctm_scale, 0, 3); + ImGui::SliderFloat2("CTM Skew", ctm_skew, -3, 3); + + ImGui::TextWrapped( + "The MatrixFilter applies a matrix in world space as opposed to " + "local filter space."); + ImGui::Checkbox("Enable", &enable); + ImGui::SliderFloat2("Filter Translation", translation, 0, 1000); + ImGui::SliderFloat2("Filter Scale", scale, 0, 3); + ImGui::SliderFloat2("Filter Skew", skew, -3, 3); + + ImGui::TextWrapped( + "Rendering the filtered image within a layer can expose bounds " + "issues. If the rendered image gets cut off when this setting is " + "enabled, there's a coverage bug in the filter."); + ImGui::Checkbox("Render in layer", &enable_savelayer); + } + ImGui::End(); + + flutter::DisplayListBuilder builder; + SkPaint paint; + builder.saveLayer(nullptr, nullptr); + { + auto content_scale = GetContentScale(); + builder.scale(content_scale.x, content_scale.y); + + // Set the current transform + auto ctm_matrix = + SkMatrix::MakeAll(ctm_scale[0], ctm_skew[0], ctm_translation[0], // + ctm_skew[1], ctm_scale[1], ctm_translation[1], // + 0, 0, 1); + builder.transform(ctm_matrix); + + // Set the matrix filter + auto filter_matrix = + SkMatrix::MakeAll(scale[0], skew[0], translation[0], // + skew[1], scale[1], translation[1], // + 0, 0, 1); + auto filter = flutter::DlMatrixImageFilter( + filter_matrix, flutter::DlImageSampling::kLinear); + if (enable) { + builder.setImageFilter(&filter); + } + + builder.drawImage(DlImageImpeller::Make(boston), {}, + flutter::DlImageSampling::kLinear, true); + + builder.restore(); + } + return builder.Build(); + }; + + ASSERT_TRUE(OpenPlaygroundHere(callback)); +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 9d3fb34a5e..c0ab517f49 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -93,6 +93,8 @@ impeller_component("entity") { "contents/filters/inputs/texture_filter_input.h", "contents/filters/linear_to_srgb_filter_contents.cc", "contents/filters/linear_to_srgb_filter_contents.h", + "contents/filters/matrix_filter_contents.cc", + "contents/filters/matrix_filter_contents.h", "contents/filters/morphology_filter_contents.cc", "contents/filters/morphology_filter_contents.h", "contents/filters/srgb_to_linear_filter_contents.cc", 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 9ac5585614..800efadf6f 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -20,6 +20,7 @@ #include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h" #include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/contents/filters/linear_to_srgb_filter_contents.h" +#include "impeller/entity/contents/filters/matrix_filter_contents.h" #include "impeller/entity/contents/filters/morphology_filter_contents.h" #include "impeller/entity/contents/filters/srgb_to_linear_filter_contents.h" #include "impeller/entity/contents/texture_contents.h" @@ -155,10 +156,10 @@ std::shared_ptr FilterContents::MakeMorphology( std::shared_ptr FilterContents::MakeColorMatrix( FilterInput::Ref input, - const ColorMatrix& matrix) { + const ColorMatrix& color_matrix) { auto filter = std::make_shared(); filter->SetInputs({input}); - filter->SetMatrix(matrix); + filter->SetMatrix(color_matrix); return filter; } @@ -176,6 +177,15 @@ std::shared_ptr FilterContents::MakeSrgbToLinearFilter( return filter; } +std::shared_ptr FilterContents::MakeMatrixFilter( + FilterInput::Ref input, + const Matrix& matrix) { + auto filter = std::make_shared(); + filter->SetInputs({input}); + filter->SetMatrix(matrix); + return filter; +} + FilterContents::FilterContents() = default; FilterContents::~FilterContents() = default; @@ -284,12 +294,12 @@ std::optional FilterContents::RenderToSnapshot( effect_transform_, coverage.value()); } -Matrix FilterContents::GetLocalTransform() const { +Matrix FilterContents::GetLocalTransform(const Matrix& parent_transform) const { return Matrix(); } Matrix FilterContents::GetTransform(const Matrix& parent_transform) const { - return parent_transform * GetLocalTransform(); + return parent_transform * GetLocalTransform(parent_transform); } } // namespace impeller 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 9f5637f7cb..eb7d27ee94 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h @@ -82,7 +82,7 @@ class FilterContents : public Contents { static std::shared_ptr MakeColorMatrix( FilterInput::Ref input, - const ColorMatrix& matrix); + const ColorMatrix& color_matrix); static std::shared_ptr MakeLinearToSrgbFilter( FilterInput::Ref input); @@ -90,6 +90,10 @@ class FilterContents : public Contents { static std::shared_ptr MakeSrgbToLinearFilter( FilterInput::Ref input); + static std::shared_ptr MakeMatrixFilter( + FilterInput::Ref input, + const Matrix& matrix); + FilterContents(); ~FilterContents() override; @@ -120,7 +124,7 @@ class FilterContents : public Contents { std::optional RenderToSnapshot(const ContentContext& renderer, const Entity& entity) const override; - virtual Matrix GetLocalTransform() const; + virtual Matrix GetLocalTransform(const Matrix& parent_transform) const; Matrix GetTransform(const Matrix& parent_transform) const; diff --git a/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc index bbeb3842f4..5e7d3396f8 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc @@ -34,7 +34,7 @@ std::optional FilterContentsFilterInput::GetCoverage( Matrix FilterContentsFilterInput::GetLocalTransform( const Entity& entity) const { - return filter_->GetLocalTransform(); + return filter_->GetLocalTransform(entity.GetTransformation()); } Matrix FilterContentsFilterInput::GetTransform(const Entity& entity) const { diff --git a/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.cc b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.cc index bf0e586711..5d87caa7d0 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.cc @@ -53,6 +53,12 @@ Matrix FilterInput::GetLocalTransform(const Entity& entity) const { return Matrix(); } +std::optional FilterInput::GetLocalCoverage(const Entity& entity) const { + Entity local_entity = entity; + local_entity.SetTransformation(GetLocalTransform(entity)); + return GetCoverage(local_entity); +} + Matrix FilterInput::GetTransform(const Entity& entity) const { return entity.GetTransformation() * GetLocalTransform(entity); } diff --git a/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.h b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.h index f34e6e6fa0..7bd631f600 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.h +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.h @@ -48,6 +48,8 @@ class FilterInput { virtual std::optional GetSnapshot(const ContentContext& renderer, const Entity& entity) const = 0; + std::optional GetLocalCoverage(const Entity& entity) const; + virtual std::optional GetCoverage(const Entity& entity) const = 0; /// @brief Get the local transform of this filter input. This transform is diff --git a/engine/src/flutter/impeller/entity/contents/filters/matrix_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/matrix_filter_contents.cc new file mode 100644 index 0000000000..bde911bc88 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/matrix_filter_contents.cc @@ -0,0 +1,31 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/entity/contents/filters/matrix_filter_contents.h" + +namespace impeller { + +MatrixFilterContents::MatrixFilterContents() = default; + +MatrixFilterContents::~MatrixFilterContents() = default; + +void MatrixFilterContents::SetMatrix(Matrix matrix) { + matrix_ = matrix; +} + +Matrix MatrixFilterContents::GetLocalTransform( + const Matrix& parent_transform) const { + return parent_transform.Invert() * matrix_ * parent_transform; +} + +std::optional MatrixFilterContents::RenderFilter( + const FilterInput::Vector& inputs, + const ContentContext& renderer, + const Entity& entity, + const Matrix& effect_transform, + const Rect& coverage) const { + return inputs[0]->GetSnapshot(renderer, entity); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/matrix_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/matrix_filter_contents.h new file mode 100644 index 0000000000..425372c82a --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/matrix_filter_contents.h @@ -0,0 +1,37 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/entity/contents/filters/filter_contents.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" + +namespace impeller { + +class MatrixFilterContents final : public FilterContents { + public: + MatrixFilterContents(); + + ~MatrixFilterContents() override; + + void SetMatrix(Matrix matrix); + + // |FilterContents| + Matrix GetLocalTransform(const Matrix& parent_transform) const override; + + private: + // |FilterContents| + std::optional RenderFilter( + const FilterInput::Vector& input_textures, + const ContentContext& renderer, + const Entity& entity, + const Matrix& effect_transform, + const Rect& coverage) const override; + + Matrix matrix_; + + FML_DISALLOW_COPY_AND_ASSIGN(MatrixFilterContents); +}; + +} // namespace impeller