From 7cea95af87d441477a3ad9cf407f42675390b6a1 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Sun, 24 Apr 2022 16:31:41 -0700 Subject: [PATCH] Separate FilterInput variants into their own TUs (flutter/engine#152) --- engine/src/flutter/impeller/entity/BUILD.gn | 18 +- .../contents/filters/blend_filter_contents.cc | 2 +- .../contents/filters/blend_filter_contents.h | 2 +- .../border_mask_blur_filter_contents.h | 2 +- .../contents/filters/filter_contents.cc | 2 +- .../entity/contents/filters/filter_contents.h | 4 +- .../entity/contents/filters/filter_input.cc | 158 ------------------ .../entity/contents/filters/filter_input.h | 153 ----------------- .../filters/gaussian_blur_filter_contents.h | 2 +- .../filters/inputs/contents_filter_input.cc | 32 ++++ .../filters/inputs/contents_filter_input.h | 34 ++++ .../inputs/filter_contents_filter_input.cc | 44 +++++ .../inputs/filter_contents_filter_input.h | 40 +++++ .../contents/filters/inputs/filter_input.cc | 55 ++++++ .../contents/filters/inputs/filter_input.h | 59 +++++++ .../filters/inputs/texture_filter_input.cc | 35 ++++ .../filters/inputs/texture_filter_input.h | 36 ++++ .../impeller/entity/entity_unittests.cc | 2 +- 18 files changed, 354 insertions(+), 326 deletions(-) delete mode 100644 engine/src/flutter/impeller/entity/contents/filters/filter_input.cc delete mode 100644 engine/src/flutter/impeller/entity/contents/filters/filter_input.h create mode 100644 engine/src/flutter/impeller/entity/contents/filters/inputs/contents_filter_input.cc create mode 100644 engine/src/flutter/impeller/entity/contents/filters/inputs/contents_filter_input.h create mode 100644 engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc create mode 100644 engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.h create mode 100644 engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.cc create mode 100644 engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.h create mode 100644 engine/src/flutter/impeller/entity/contents/filters/inputs/texture_filter_input.cc create mode 100644 engine/src/flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 4f942c39a2..ec4dd55104 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -39,14 +39,20 @@ impeller_component("entity") { "contents/contents.h", "contents/filters/blend_filter_contents.cc", "contents/filters/blend_filter_contents.h", - "contents/filters/filter_contents.cc", - "contents/filters/filter_contents.h", - "contents/filters/filter_input.cc", - "contents/filters/filter_input.h", - "contents/filters/gaussian_blur_filter_contents.cc", - "contents/filters/gaussian_blur_filter_contents.h", "contents/filters/border_mask_blur_filter_contents.cc", "contents/filters/border_mask_blur_filter_contents.h", + "contents/filters/filter_contents.cc", + "contents/filters/filter_contents.h", + "contents/filters/gaussian_blur_filter_contents.cc", + "contents/filters/gaussian_blur_filter_contents.h", + "contents/filters/inputs/contents_filter_input.cc", + "contents/filters/inputs/contents_filter_input.h", + "contents/filters/inputs/filter_contents_filter_input.cc", + "contents/filters/inputs/filter_contents_filter_input.h", + "contents/filters/inputs/filter_input.cc", + "contents/filters/inputs/filter_input.h", + "contents/filters/inputs/texture_filter_input.cc", + "contents/filters/inputs/texture_filter_input.h", "contents/linear_gradient_contents.cc", "contents/linear_gradient_contents.h", "contents/snapshot.cc", diff --git a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc index 05a744a422..5ff7e74677 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc @@ -5,7 +5,7 @@ #include "impeller/entity/contents/filters/blend_filter_contents.h" #include "impeller/entity/contents/content_context.h" -#include "impeller/entity/contents/filters/filter_input.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/renderer/render_pass.h" #include "impeller/renderer/sampler_library.h" diff --git a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.h index 9a10a93356..8bc833878c 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.h @@ -5,7 +5,7 @@ #pragma once #include "impeller/entity/contents/filters/filter_contents.h" -#include "impeller/entity/contents/filters/filter_input.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" namespace impeller { diff --git a/engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.h index 335edd797e..6aa95d8704 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.h @@ -7,7 +7,7 @@ #include #include #include "impeller/entity/contents/filters/filter_contents.h" -#include "impeller/entity/contents/filters/filter_input.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" namespace impeller { 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 a054aa8038..3196c42678 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -16,7 +16,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/contents/filters/blend_filter_contents.h" #include "impeller/entity/contents/filters/border_mask_blur_filter_contents.h" -#include "impeller/entity/contents/filters/filter_input.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h" #include "impeller/entity/contents/texture_contents.h" #include "impeller/entity/entity.h" 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 6f6ab02c23..fff4133fd5 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h @@ -8,7 +8,7 @@ #include #include -#include "impeller/entity/contents/filters/filter_input.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/entity.h" #include "impeller/renderer/formats.h" @@ -145,8 +145,6 @@ class FilterContents : public Contents { FilterInput::Vector inputs_; FML_DISALLOW_COPY_AND_ASSIGN(FilterContents); - - friend FilterContentsFilterInput; }; } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc b/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc deleted file mode 100644 index 6719886467..0000000000 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc +++ /dev/null @@ -1,158 +0,0 @@ -// 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/filter_input.h" - -#include -#include -#include -#include -#include - -#include "fml/logging.h" -#include "impeller/entity/contents/filters/filter_contents.h" -#include "impeller/entity/contents/snapshot.h" -#include "impeller/entity/entity.h" - -namespace impeller { - -/******************************************************************************* - ******* FilterInput - ******************************************************************************/ - -FilterInput::Ref FilterInput::Make(Variant input) { - if (auto filter = std::get_if>(&input)) { - return std::static_pointer_cast( - std::shared_ptr( - new FilterContentsFilterInput(*filter))); - } - - if (auto contents = std::get_if>(&input)) { - return std::static_pointer_cast( - std::shared_ptr( - new ContentsFilterInput(*contents))); - } - - if (auto texture = std::get_if>(&input)) { - return std::static_pointer_cast( - std::shared_ptr(new TextureFilterInput(*texture))); - } - - FML_UNREACHABLE(); -} - -FilterInput::Vector FilterInput::Make(std::initializer_list inputs) { - FilterInput::Vector result; - result.reserve(inputs.size()); - for (const auto& input : inputs) { - result.push_back(Make(input)); - } - return result; -} - -Matrix FilterInput::GetLocalTransform(const Entity& entity) const { - return Matrix(); -} - -Matrix FilterInput::GetTransform(const Entity& entity) const { - return entity.GetTransformation() * GetLocalTransform(entity); -} - -FilterInput::~FilterInput() = default; - -/******************************************************************************* - ******* FilterContentsFilterInput - ******************************************************************************/ - -FilterContentsFilterInput::FilterContentsFilterInput( - std::shared_ptr filter) - : filter_(filter) {} - -FilterContentsFilterInput::~FilterContentsFilterInput() = default; - -FilterInput::Variant FilterContentsFilterInput::GetInput() const { - return filter_; -} - -std::optional FilterContentsFilterInput::GetSnapshot( - const ContentContext& renderer, - const Entity& entity) const { - if (!snapshot_.has_value()) { - snapshot_ = filter_->RenderToSnapshot(renderer, entity); - } - return snapshot_; -} - -std::optional FilterContentsFilterInput::GetCoverage( - const Entity& entity) const { - return filter_->GetCoverage(entity); -} - -Matrix FilterContentsFilterInput::GetLocalTransform( - const Entity& entity) const { - return filter_->GetLocalTransform(); -} - -Matrix FilterContentsFilterInput::GetTransform(const Entity& entity) const { - return filter_->GetTransform(entity.GetTransformation()); -} - -/******************************************************************************* - ******* ContentsFilterInput - ******************************************************************************/ - -ContentsFilterInput::ContentsFilterInput(std::shared_ptr contents) - : contents_(contents) {} - -ContentsFilterInput::~ContentsFilterInput() = default; - -FilterInput::Variant ContentsFilterInput::GetInput() const { - return contents_; -} - -std::optional ContentsFilterInput::GetSnapshot( - const ContentContext& renderer, - const Entity& entity) const { - if (!snapshot_.has_value()) { - snapshot_ = contents_->RenderToSnapshot(renderer, entity); - } - return snapshot_; -} - -std::optional ContentsFilterInput::GetCoverage( - const Entity& entity) const { - return contents_->GetCoverage(entity); -} - -/******************************************************************************* - ******* TextureFilterInput - ******************************************************************************/ - -TextureFilterInput::TextureFilterInput(std::shared_ptr texture) - : texture_(texture) {} - -TextureFilterInput::~TextureFilterInput() = default; - -FilterInput::Variant TextureFilterInput::GetInput() const { - return texture_; -} - -std::optional TextureFilterInput::GetSnapshot( - const ContentContext& renderer, - const Entity& entity) const { - return Snapshot{.texture = texture_, .transform = GetTransform(entity)}; -} - -std::optional TextureFilterInput::GetCoverage( - const Entity& entity) const { - return Rect::MakeSize(Size(texture_->GetSize())) - .TransformBounds(GetTransform(entity)); -} - -Matrix TextureFilterInput::GetLocalTransform(const Entity& entity) const { - // Compute the local transform such that the texture is centered. - return Matrix::MakeTranslation(-Point(texture_->GetSize()) / 2); -} - -} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_input.h b/engine/src/flutter/impeller/entity/contents/filters/filter_input.h deleted file mode 100644 index dd1f907a6d..0000000000 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_input.h +++ /dev/null @@ -1,153 +0,0 @@ -// 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 -#include -#include -#include - -#include "impeller/entity/contents/contents.h" -#include "impeller/geometry/rect.h" -#include "impeller/renderer/formats.h" - -namespace impeller { - -class ContentContext; -class Entity; -class FilterContents; - -/******************************************************************************* - ******* FilterInput - ******************************************************************************/ - -/// `FilterInput` is a lazy/single eval `Snapshot` which may be shared across -/// filter parameters and used to evaluate input coverage. -/// -/// A `FilterInput` can be re-used for any filter inputs across an entity's -/// filter graph without repeating subpasses unnecessarily. -/// -/// Filters may decide to not evaluate inputs in situations where they won't -/// contribute to the filter's output texture. -class FilterInput { - public: - using Ref = std::shared_ptr; - using Vector = std::vector; - using Variant = std::variant, - std::shared_ptr, - std::shared_ptr>; - - virtual ~FilterInput(); - - static FilterInput::Ref Make(Variant input); - - static FilterInput::Vector Make(std::initializer_list inputs); - - virtual Variant GetInput() const = 0; - - virtual std::optional GetSnapshot(const ContentContext& renderer, - const Entity& entity) const = 0; - - virtual std::optional GetCoverage(const Entity& entity) const = 0; - - /// @brief Get the local transform of this filter input. This transform is - /// relative to the `Entity` transform space. - virtual Matrix GetLocalTransform(const Entity& entity) const; - - /// @brief Get the transform of this `FilterInput`. This is equivalent to - /// calling `entity.GetTransformation() * GetLocalTransform()`. - virtual Matrix GetTransform(const Entity& entity) const; -}; - -/******************************************************************************* - ******* FilterContentsFilterInput - ******************************************************************************/ - -class FilterContentsFilterInput final : public FilterInput { - public: - ~FilterContentsFilterInput() override; - - // |FilterInput| - Variant GetInput() const override; - - // |FilterInput| - std::optional GetSnapshot(const ContentContext& renderer, - const Entity& entity) const override; - - // |FilterInput| - std::optional GetCoverage(const Entity& entity) const override; - - // |FilterInput| - Matrix GetLocalTransform(const Entity& entity) const override; - - // |FilterInput| - Matrix GetTransform(const Entity& entity) const override; - - private: - FilterContentsFilterInput(std::shared_ptr filter); - - std::shared_ptr filter_; - mutable std::optional snapshot_; - - friend FilterInput; -}; - -/******************************************************************************* - ******* ContentsFilterInput - ******************************************************************************/ - -class ContentsFilterInput final : public FilterInput { - public: - ~ContentsFilterInput() override; - - // |FilterInput| - Variant GetInput() const override; - - // |FilterInput| - std::optional GetSnapshot(const ContentContext& renderer, - const Entity& entity) const override; - - // |FilterInput| - std::optional GetCoverage(const Entity& entity) const override; - - private: - ContentsFilterInput(std::shared_ptr contents); - - std::shared_ptr contents_; - mutable std::optional snapshot_; - - friend FilterInput; -}; - -/******************************************************************************* - ******* TextureFilterInput - ******************************************************************************/ - -class TextureFilterInput final : public FilterInput { - public: - ~TextureFilterInput() override; - - // |FilterInput| - Variant GetInput() const override; - - // |FilterInput| - std::optional GetSnapshot(const ContentContext& renderer, - const Entity& entity) const override; - - // |FilterInput| - std::optional GetCoverage(const Entity& entity) const override; - - // |FilterInput| - Matrix GetLocalTransform(const Entity& entity) const override; - - private: - TextureFilterInput(std::shared_ptr texture); - - std::shared_ptr texture_; - - friend FilterInput; -}; - -} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h index 6b95d52a7a..e85ad20dc5 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h @@ -7,7 +7,7 @@ #include #include #include "impeller/entity/contents/filters/filter_contents.h" -#include "impeller/entity/contents/filters/filter_input.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" namespace impeller { diff --git a/engine/src/flutter/impeller/entity/contents/filters/inputs/contents_filter_input.cc b/engine/src/flutter/impeller/entity/contents/filters/inputs/contents_filter_input.cc new file mode 100644 index 0000000000..0d462f78de --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/contents_filter_input.cc @@ -0,0 +1,32 @@ +// 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/inputs/contents_filter_input.h" + +namespace impeller { + +ContentsFilterInput::ContentsFilterInput(std::shared_ptr contents) + : contents_(contents) {} + +ContentsFilterInput::~ContentsFilterInput() = default; + +FilterInput::Variant ContentsFilterInput::GetInput() const { + return contents_; +} + +std::optional ContentsFilterInput::GetSnapshot( + const ContentContext& renderer, + const Entity& entity) const { + if (!snapshot_.has_value()) { + snapshot_ = contents_->RenderToSnapshot(renderer, entity); + } + return snapshot_; +} + +std::optional ContentsFilterInput::GetCoverage( + const Entity& entity) const { + return contents_->GetCoverage(entity); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/inputs/contents_filter_input.h b/engine/src/flutter/impeller/entity/contents/filters/inputs/contents_filter_input.h new file mode 100644 index 0000000000..7309c17ffa --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/contents_filter_input.h @@ -0,0 +1,34 @@ +// 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/inputs/filter_input.h" + +namespace impeller { + +class ContentsFilterInput final : public FilterInput { + public: + ~ContentsFilterInput() override; + + // |FilterInput| + Variant GetInput() const override; + + // |FilterInput| + std::optional GetSnapshot(const ContentContext& renderer, + const Entity& entity) const override; + + // |FilterInput| + std::optional GetCoverage(const Entity& entity) const override; + + private: + ContentsFilterInput(std::shared_ptr contents); + + std::shared_ptr contents_; + mutable std::optional snapshot_; + + friend FilterInput; +}; + +} // namespace impeller 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 new file mode 100644 index 0000000000..bbeb3842f4 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc @@ -0,0 +1,44 @@ +// 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/inputs/filter_contents_filter_input.h" + +#include "impeller/entity/contents/filters/filter_contents.h" + +namespace impeller { + +FilterContentsFilterInput::FilterContentsFilterInput( + std::shared_ptr filter) + : filter_(filter) {} + +FilterContentsFilterInput::~FilterContentsFilterInput() = default; + +FilterInput::Variant FilterContentsFilterInput::GetInput() const { + return filter_; +} + +std::optional FilterContentsFilterInput::GetSnapshot( + const ContentContext& renderer, + const Entity& entity) const { + if (!snapshot_.has_value()) { + snapshot_ = filter_->RenderToSnapshot(renderer, entity); + } + return snapshot_; +} + +std::optional FilterContentsFilterInput::GetCoverage( + const Entity& entity) const { + return filter_->GetCoverage(entity); +} + +Matrix FilterContentsFilterInput::GetLocalTransform( + const Entity& entity) const { + return filter_->GetLocalTransform(); +} + +Matrix FilterContentsFilterInput::GetTransform(const Entity& entity) const { + return filter_->GetTransform(entity.GetTransformation()); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.h b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.h new file mode 100644 index 0000000000..9562484a8f --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.h @@ -0,0 +1,40 @@ +// 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/inputs/filter_input.h" + +namespace impeller { + +class FilterContentsFilterInput final : public FilterInput { + public: + ~FilterContentsFilterInput() override; + + // |FilterInput| + Variant GetInput() const override; + + // |FilterInput| + std::optional GetSnapshot(const ContentContext& renderer, + const Entity& entity) const override; + + // |FilterInput| + std::optional GetCoverage(const Entity& entity) const override; + + // |FilterInput| + Matrix GetLocalTransform(const Entity& entity) const override; + + // |FilterInput| + Matrix GetTransform(const Entity& entity) const override; + + private: + FilterContentsFilterInput(std::shared_ptr filter); + + std::shared_ptr filter_; + mutable std::optional snapshot_; + + friend FilterInput; +}; + +} // namespace impeller 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 new file mode 100644 index 0000000000..f69986c8b8 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.cc @@ -0,0 +1,55 @@ +// 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/inputs/filter_input.h" + +#include "flutter/fml/logging.h" +#include "impeller/entity/contents/filters/filter_contents.h" +#include "impeller/entity/contents/filters/inputs/contents_filter_input.h" +#include "impeller/entity/contents/filters/inputs/filter_contents_filter_input.h" +#include "impeller/entity/contents/filters/inputs/texture_filter_input.h" + +namespace impeller { + +FilterInput::Ref FilterInput::Make(Variant input) { + if (auto filter = std::get_if>(&input)) { + return std::static_pointer_cast( + std::shared_ptr( + new FilterContentsFilterInput(*filter))); + } + + if (auto contents = std::get_if>(&input)) { + return std::static_pointer_cast( + std::shared_ptr( + new ContentsFilterInput(*contents))); + } + + if (auto texture = std::get_if>(&input)) { + return std::static_pointer_cast( + std::shared_ptr(new TextureFilterInput(*texture))); + } + + FML_UNREACHABLE(); +} + +FilterInput::Vector FilterInput::Make(std::initializer_list inputs) { + FilterInput::Vector result; + result.reserve(inputs.size()); + for (const auto& input : inputs) { + result.push_back(Make(input)); + } + return result; +} + +Matrix FilterInput::GetLocalTransform(const Entity& entity) const { + return Matrix(); +} + +Matrix FilterInput::GetTransform(const Entity& entity) const { + return entity.GetTransformation() * GetLocalTransform(entity); +} + +FilterInput::~FilterInput() = default; + +} // namespace impeller 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 new file mode 100644 index 0000000000..6203a0d024 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.h @@ -0,0 +1,59 @@ +// 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 +#include +#include +#include + +#include "impeller/entity/contents/contents.h" +#include "impeller/geometry/rect.h" + +namespace impeller { + +class ContentContext; +class Entity; +class FilterContents; + +/// `FilterInput` is a lazy/single eval `Snapshot` which may be shared across +/// filter parameters and used to evaluate input coverage. +/// +/// A `FilterInput` can be re-used for any filter inputs across an entity's +/// filter graph without repeating subpasses unnecessarily. +/// +/// Filters may decide to not evaluate inputs in situations where they won't +/// contribute to the filter's output texture. +class FilterInput { + public: + using Ref = std::shared_ptr; + using Vector = std::vector; + using Variant = std::variant, + std::shared_ptr, + std::shared_ptr>; + + virtual ~FilterInput(); + + static FilterInput::Ref Make(Variant input); + + static FilterInput::Vector Make(std::initializer_list inputs); + + virtual Variant GetInput() const = 0; + + virtual std::optional GetSnapshot(const ContentContext& renderer, + const Entity& entity) const = 0; + + virtual std::optional GetCoverage(const Entity& entity) const = 0; + + /// @brief Get the local transform of this filter input. This transform is + /// relative to the `Entity` transform space. + virtual Matrix GetLocalTransform(const Entity& entity) const; + + /// @brief Get the transform of this `FilterInput`. This is equivalent to + /// calling `entity.GetTransformation() * GetLocalTransform()`. + virtual Matrix GetTransform(const Entity& entity) const; +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/inputs/texture_filter_input.cc b/engine/src/flutter/impeller/entity/contents/filters/inputs/texture_filter_input.cc new file mode 100644 index 0000000000..5ee11edb20 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/texture_filter_input.cc @@ -0,0 +1,35 @@ +// 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/inputs/texture_filter_input.h" + +namespace impeller { + +TextureFilterInput::TextureFilterInput(std::shared_ptr texture) + : texture_(texture) {} + +TextureFilterInput::~TextureFilterInput() = default; + +FilterInput::Variant TextureFilterInput::GetInput() const { + return texture_; +} + +std::optional TextureFilterInput::GetSnapshot( + const ContentContext& renderer, + const Entity& entity) const { + return Snapshot{.texture = texture_, .transform = GetTransform(entity)}; +} + +std::optional TextureFilterInput::GetCoverage( + const Entity& entity) const { + return Rect::MakeSize(Size(texture_->GetSize())) + .TransformBounds(GetTransform(entity)); +} + +Matrix TextureFilterInput::GetLocalTransform(const Entity& entity) const { + // Compute the local transform such that the texture is centered. + return Matrix::MakeTranslation(-Point(texture_->GetSize()) / 2); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h b/engine/src/flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h new file mode 100644 index 0000000000..24fc642a1d --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h @@ -0,0 +1,36 @@ +// 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/inputs/filter_input.h" + +namespace impeller { + +class TextureFilterInput final : public FilterInput { + public: + ~TextureFilterInput() override; + + // |FilterInput| + Variant GetInput() const override; + + // |FilterInput| + std::optional GetSnapshot(const ContentContext& renderer, + const Entity& entity) const override; + + // |FilterInput| + std::optional GetCoverage(const Entity& entity) const override; + + // |FilterInput| + Matrix GetLocalTransform(const Entity& entity) const override; + + private: + TextureFilterInput(std::shared_ptr texture); + + std::shared_ptr texture_; + + friend FilterInput; +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index ba154b2af9..00fbb41989 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -7,7 +7,7 @@ #include "flutter/testing/testing.h" #include "impeller/entity/contents/filters/blend_filter_contents.h" #include "impeller/entity/contents/filters/filter_contents.h" -#include "impeller/entity/contents/filters/filter_input.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/contents/solid_color_contents.h" #include "impeller/entity/contents/solid_stroke_contents.h" #include "impeller/entity/entity.h"