Separate FilterInput variants into their own TUs (flutter/engine#152)

This commit is contained in:
Brandon DeRosier
2022-04-24 16:31:41 -07:00
committed by Dan Field
parent fababbc9c7
commit 7cea95af87
18 changed files with 354 additions and 326 deletions

View File

@@ -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",

View File

@@ -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"

View File

@@ -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 {

View File

@@ -7,7 +7,7 @@
#include <memory>
#include <optional>
#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 {

View File

@@ -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"

View File

@@ -8,7 +8,7 @@
#include <variant>
#include <vector>
#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

View File

@@ -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 <cstdarg>
#include <initializer_list>
#include <memory>
#include <optional>
#include <variant>
#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<std::shared_ptr<FilterContents>>(&input)) {
return std::static_pointer_cast<FilterInput>(
std::shared_ptr<FilterContentsFilterInput>(
new FilterContentsFilterInput(*filter)));
}
if (auto contents = std::get_if<std::shared_ptr<Contents>>(&input)) {
return std::static_pointer_cast<FilterInput>(
std::shared_ptr<ContentsFilterInput>(
new ContentsFilterInput(*contents)));
}
if (auto texture = std::get_if<std::shared_ptr<Texture>>(&input)) {
return std::static_pointer_cast<FilterInput>(
std::shared_ptr<TextureFilterInput>(new TextureFilterInput(*texture)));
}
FML_UNREACHABLE();
}
FilterInput::Vector FilterInput::Make(std::initializer_list<Variant> 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<FilterContents> filter)
: filter_(filter) {}
FilterContentsFilterInput::~FilterContentsFilterInput() = default;
FilterInput::Variant FilterContentsFilterInput::GetInput() const {
return filter_;
}
std::optional<Snapshot> FilterContentsFilterInput::GetSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
if (!snapshot_.has_value()) {
snapshot_ = filter_->RenderToSnapshot(renderer, entity);
}
return snapshot_;
}
std::optional<Rect> 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_(contents) {}
ContentsFilterInput::~ContentsFilterInput() = default;
FilterInput::Variant ContentsFilterInput::GetInput() const {
return contents_;
}
std::optional<Snapshot> ContentsFilterInput::GetSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
if (!snapshot_.has_value()) {
snapshot_ = contents_->RenderToSnapshot(renderer, entity);
}
return snapshot_;
}
std::optional<Rect> ContentsFilterInput::GetCoverage(
const Entity& entity) const {
return contents_->GetCoverage(entity);
}
/*******************************************************************************
******* TextureFilterInput
******************************************************************************/
TextureFilterInput::TextureFilterInput(std::shared_ptr<Texture> texture)
: texture_(texture) {}
TextureFilterInput::~TextureFilterInput() = default;
FilterInput::Variant TextureFilterInput::GetInput() const {
return texture_;
}
std::optional<Snapshot> TextureFilterInput::GetSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
return Snapshot{.texture = texture_, .transform = GetTransform(entity)};
}
std::optional<Rect> 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

View File

@@ -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 <memory>
#include <optional>
#include <variant>
#include <vector>
#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<FilterInput>;
using Vector = std::vector<FilterInput::Ref>;
using Variant = std::variant<std::shared_ptr<FilterContents>,
std::shared_ptr<Contents>,
std::shared_ptr<Texture>>;
virtual ~FilterInput();
static FilterInput::Ref Make(Variant input);
static FilterInput::Vector Make(std::initializer_list<Variant> inputs);
virtual Variant GetInput() const = 0;
virtual std::optional<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const = 0;
virtual std::optional<Rect> 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<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const override;
// |FilterInput|
std::optional<Rect> 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<FilterContents> filter);
std::shared_ptr<FilterContents> filter_;
mutable std::optional<Snapshot> snapshot_;
friend FilterInput;
};
/*******************************************************************************
******* ContentsFilterInput
******************************************************************************/
class ContentsFilterInput final : public FilterInput {
public:
~ContentsFilterInput() override;
// |FilterInput|
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
private:
ContentsFilterInput(std::shared_ptr<Contents> contents);
std::shared_ptr<Contents> contents_;
mutable std::optional<Snapshot> snapshot_;
friend FilterInput;
};
/*******************************************************************************
******* TextureFilterInput
******************************************************************************/
class TextureFilterInput final : public FilterInput {
public:
~TextureFilterInput() override;
// |FilterInput|
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |FilterInput|
Matrix GetLocalTransform(const Entity& entity) const override;
private:
TextureFilterInput(std::shared_ptr<Texture> texture);
std::shared_ptr<Texture> texture_;
friend FilterInput;
};
} // namespace impeller

View File

@@ -7,7 +7,7 @@
#include <memory>
#include <optional>
#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 {

View File

@@ -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_(contents) {}
ContentsFilterInput::~ContentsFilterInput() = default;
FilterInput::Variant ContentsFilterInput::GetInput() const {
return contents_;
}
std::optional<Snapshot> ContentsFilterInput::GetSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
if (!snapshot_.has_value()) {
snapshot_ = contents_->RenderToSnapshot(renderer, entity);
}
return snapshot_;
}
std::optional<Rect> ContentsFilterInput::GetCoverage(
const Entity& entity) const {
return contents_->GetCoverage(entity);
}
} // namespace impeller

View File

@@ -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<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
private:
ContentsFilterInput(std::shared_ptr<Contents> contents);
std::shared_ptr<Contents> contents_;
mutable std::optional<Snapshot> snapshot_;
friend FilterInput;
};
} // namespace impeller

View File

@@ -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<FilterContents> filter)
: filter_(filter) {}
FilterContentsFilterInput::~FilterContentsFilterInput() = default;
FilterInput::Variant FilterContentsFilterInput::GetInput() const {
return filter_;
}
std::optional<Snapshot> FilterContentsFilterInput::GetSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
if (!snapshot_.has_value()) {
snapshot_ = filter_->RenderToSnapshot(renderer, entity);
}
return snapshot_;
}
std::optional<Rect> 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

View File

@@ -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<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const override;
// |FilterInput|
std::optional<Rect> 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<FilterContents> filter);
std::shared_ptr<FilterContents> filter_;
mutable std::optional<Snapshot> snapshot_;
friend FilterInput;
};
} // namespace impeller

View File

@@ -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<std::shared_ptr<FilterContents>>(&input)) {
return std::static_pointer_cast<FilterInput>(
std::shared_ptr<FilterContentsFilterInput>(
new FilterContentsFilterInput(*filter)));
}
if (auto contents = std::get_if<std::shared_ptr<Contents>>(&input)) {
return std::static_pointer_cast<FilterInput>(
std::shared_ptr<ContentsFilterInput>(
new ContentsFilterInput(*contents)));
}
if (auto texture = std::get_if<std::shared_ptr<Texture>>(&input)) {
return std::static_pointer_cast<FilterInput>(
std::shared_ptr<TextureFilterInput>(new TextureFilterInput(*texture)));
}
FML_UNREACHABLE();
}
FilterInput::Vector FilterInput::Make(std::initializer_list<Variant> 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

View File

@@ -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 <memory>
#include <optional>
#include <variant>
#include <vector>
#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<FilterInput>;
using Vector = std::vector<FilterInput::Ref>;
using Variant = std::variant<std::shared_ptr<FilterContents>,
std::shared_ptr<Contents>,
std::shared_ptr<Texture>>;
virtual ~FilterInput();
static FilterInput::Ref Make(Variant input);
static FilterInput::Vector Make(std::initializer_list<Variant> inputs);
virtual Variant GetInput() const = 0;
virtual std::optional<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const = 0;
virtual std::optional<Rect> 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

View File

@@ -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_(texture) {}
TextureFilterInput::~TextureFilterInput() = default;
FilterInput::Variant TextureFilterInput::GetInput() const {
return texture_;
}
std::optional<Snapshot> TextureFilterInput::GetSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
return Snapshot{.texture = texture_, .transform = GetTransform(entity)};
}
std::optional<Rect> 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

View File

@@ -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<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |FilterInput|
Matrix GetLocalTransform(const Entity& entity) const override;
private:
TextureFilterInput(std::shared_ptr<Texture> texture);
std::shared_ptr<Texture> texture_;
friend FilterInput;
};
} // namespace impeller

View File

@@ -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"