[Impeller] Limit subpass textures and backdrop blurs to the current clip (flutter/engine#42039)

Part of https://github.com/flutter/flutter/issues/126696.

* Generalize the FilterContents coverage hint as a Contents property.
* Incorporates the stencil coverage when creating subpass textures.
* Set a coverage hint for backdrop filters.
* Incorporate the coverage hint in the 2-pass Gaussian blur.
This commit is contained in:
Brandon DeRosier
2023-05-15 12:18:08 -07:00
committed by GitHub
parent 06b709ff52
commit 7c0bad1009
37 changed files with 308 additions and 162 deletions

View File

@@ -2174,5 +2174,52 @@ TEST_P(AiksTest, CanRenderDestructiveSaveLayer) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, CanRenderBackdropBlurInteractive) {
auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
auto [a, b] = IMPELLER_PLAYGROUND_LINE(Point(50, 50), Point(300, 200), 30,
Color::White(), Color::White());
Canvas canvas;
canvas.DrawCircle({100, 100}, 50, {.color = Color::CornflowerBlue()});
canvas.DrawCircle({300, 200}, 100, {.color = Color::GreenYellow()});
canvas.DrawCircle({140, 170}, 75, {.color = Color::DarkMagenta()});
canvas.DrawCircle({180, 120}, 100, {.color = Color::OrangeRed()});
canvas.ClipRRect(Rect::MakeLTRB(a.x, a.y, b.x, b.y), 20);
canvas.SaveLayer({.blend_mode = BlendMode::kSource}, std::nullopt,
[](const FilterInput::Ref& input,
const Matrix& effect_transform, bool is_subpass) {
return FilterContents::MakeGaussianBlur(
input, Sigma(20.0), Sigma(20.0),
FilterContents::BlurStyle::kNormal,
Entity::TileMode::kClamp, effect_transform);
});
canvas.Restore();
return renderer.Render(canvas.EndRecordingAsPicture(), render_target);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(AiksTest, CanRenderBackdropBlur) {
Canvas canvas;
canvas.DrawCircle({100, 100}, 50, {.color = Color::CornflowerBlue()});
canvas.DrawCircle({300, 200}, 100, {.color = Color::GreenYellow()});
canvas.DrawCircle({140, 170}, 75, {.color = Color::DarkMagenta()});
canvas.DrawCircle({180, 120}, 100, {.color = Color::OrangeRed()});
canvas.ClipRRect(Rect::MakeLTRB(75, 50, 375, 275), 20);
canvas.SaveLayer({.blend_mode = BlendMode::kSource}, std::nullopt,
[](const FilterInput::Ref& input,
const Matrix& effect_transform, bool is_subpass) {
return FilterContents::MakeGaussianBlur(
input, Sigma(30.0), Sigma(30.0),
FilterContents::BlurStyle::kNormal,
Entity::TileMode::kClamp, effect_transform);
});
canvas.Restore();
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
} // namespace testing
} // namespace impeller

View File

@@ -124,6 +124,14 @@ bool Contents::ShouldRender(const Entity& entity,
return stencil_coverage->IntersectsWithRect(coverage.value());
}
void Contents::SetCoverageHint(std::optional<Rect> coverage_hint) {
coverage_hint_ = coverage_hint;
}
const std::optional<Rect>& Contents::GetCoverageHint() const {
return coverage_hint_;
}
std::optional<Size> Contents::GetColorSourceSize() const {
return color_source_size_;
};

View File

@@ -56,6 +56,14 @@ class Contents {
/// @brief Get the screen space bounding rectangle that this contents affects.
virtual std::optional<Rect> GetCoverage(const Entity& entity) const = 0;
/// @brief Hint that specifies the coverage area of this Contents that will
/// actually be used during rendering. This is for optimization
/// purposes only and can not be relied on as a clip. May optionally
/// affect the result of `GetCoverage()`.
void SetCoverageHint(std::optional<Rect> coverage_hint);
const std::optional<Rect>& GetCoverageHint() const;
/// @brief Whether this Contents only emits opaque source colors from the
/// fragment stage. This value does not account for any entity
/// properties (e.g. the blend mode), clips/visibility culling, or
@@ -110,6 +118,7 @@ class Contents {
virtual void SetInheritedOpacity(Scalar opacity);
private:
std::optional<Rect> coverage_hint_;
std::optional<Size> color_source_size_;
FML_DISALLOW_COPY_AND_ASSIGN(Contents);

View File

@@ -649,7 +649,8 @@ std::optional<Entity> BlendFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
if (inputs.empty()) {
return std::nullopt;
}

View File

@@ -32,11 +32,13 @@ class BlendFilterContents : public ColorFilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
/// @brief Optimized advanced blend that avoids a second subpass when there is
/// only a single input and a foreground color.

View File

@@ -53,7 +53,8 @@ std::optional<Entity> BorderMaskBlurFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
using VS = BorderMaskBlurPipeline::VertexShader;
using FS = BorderMaskBlurPipeline::FragmentShader;

View File

@@ -29,11 +29,13 @@ class BorderMaskBlurFilterContents final : public FilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
Sigma sigma_x_;
Sigma sigma_y_;

View File

@@ -29,7 +29,8 @@ std::optional<Entity> ColorMatrixFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
using VS = ColorMatrixColorFilterPipeline::VertexShader;
using FS = ColorMatrixColorFilterPipeline::FragmentShader;

View File

@@ -24,11 +24,13 @@ class ColorMatrixFilterContents final : public ColorFilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
ColorMatrix matrix_;

View File

@@ -153,10 +153,6 @@ void FilterContents::SetInputs(FilterInput::Vector inputs) {
inputs_ = std::move(inputs);
}
void FilterContents::SetCoverageCrop(std::optional<Rect> coverage_crop) {
coverage_crop_ = coverage_crop;
}
void FilterContents::SetEffectTransform(Matrix effect_transform) {
effect_transform_ = effect_transform;
}
@@ -171,7 +167,7 @@ bool FilterContents::Render(const ContentContext& renderer,
// Run the filter.
auto maybe_entity = GetEntity(renderer, entity);
auto maybe_entity = GetEntity(renderer, entity, GetCoverageHint());
if (!maybe_entity.has_value()) {
return true;
}
@@ -181,8 +177,8 @@ bool FilterContents::Render(const ContentContext& renderer,
std::optional<Rect> FilterContents::GetLocalCoverage(
const Entity& local_entity) const {
auto coverage = GetFilterCoverage(inputs_, local_entity, effect_transform_);
if (coverage_crop_.has_value() && coverage.has_value()) {
coverage = coverage->Intersection(coverage_crop_.value());
if (GetCoverageHint().has_value() && coverage.has_value()) {
coverage = coverage->Intersection(*GetCoverageHint());
}
return coverage;
@@ -223,8 +219,10 @@ std::optional<Rect> FilterContents::GetFilterCoverage(
return result;
}
std::optional<Entity> FilterContents::GetEntity(const ContentContext& renderer,
const Entity& entity) const {
std::optional<Entity> FilterContents::GetEntity(
const ContentContext& renderer,
const Entity& entity,
const std::optional<Rect>& coverage_hint) const {
Entity entity_with_local_transform = entity;
entity_with_local_transform.SetTransformation(
GetTransform(entity.GetTransformation()));
@@ -235,7 +233,7 @@ std::optional<Entity> FilterContents::GetEntity(const ContentContext& renderer,
}
return RenderFilter(inputs_, renderer, entity_with_local_transform,
effect_transform_, coverage.value());
effect_transform_, coverage.value(), coverage_hint);
}
std::optional<Snapshot> FilterContents::RenderToSnapshot(
@@ -247,12 +245,13 @@ std::optional<Snapshot> FilterContents::RenderToSnapshot(
const std::string& label) const {
// Resolve the render instruction (entity) from the filter and render it to a
// snapshot.
if (std::optional<Entity> result = GetEntity(renderer, entity);
if (std::optional<Entity> result =
GetEntity(renderer, entity, coverage_limit);
result.has_value()) {
return result->GetContents()->RenderToSnapshot(
renderer, // renderer
result.value(), // entity
std::nullopt, // coverage_limit
coverage_limit, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
label); // label

View File

@@ -102,16 +102,15 @@ class FilterContents : public Contents {
/// particular filter's implementation.
void SetInputs(FilterInput::Vector inputs);
/// @brief Screen space bounds to use for cropping the filter output.
void SetCoverageCrop(std::optional<Rect> coverage_crop);
/// @brief Sets the transform which gets appended to the effect of this
/// filter. Note that this is in addition to the entity's transform.
void SetEffectTransform(Matrix effect_transform);
/// @brief Create an Entity that renders this filter's output.
std::optional<Entity> GetEntity(const ContentContext& renderer,
const Entity& entity) const;
std::optional<Entity> GetEntity(
const ContentContext& renderer,
const Entity& entity,
const std::optional<Rect>& coverage_hint) const;
// |Contents|
bool Render(const ContentContext& renderer,
@@ -141,16 +140,17 @@ class FilterContents : public Contents {
const Matrix& effect_transform) const;
/// @brief Converts zero or more filter inputs into a render instruction.
virtual std::optional<Entity> RenderFilter(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const = 0;
virtual std::optional<Entity> RenderFilter(
const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const = 0;
std::optional<Rect> GetLocalCoverage(const Entity& local_entity) const;
FilterInput::Vector inputs_;
std::optional<Rect> coverage_crop_;
Matrix effect_transform_;
FML_DISALLOW_COPY_AND_ASSIGN(FilterContents);

View File

@@ -86,10 +86,13 @@ std::optional<Entity> DirectionalGaussianBlurFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
using VS = GaussianBlurAlphaDecalPipeline::VertexShader;
using FS = GaussianBlurAlphaDecalPipeline::FragmentShader;
bool is_first_pass = !source_override_;
//----------------------------------------------------------------------------
/// Handle inputs.
///
@@ -98,10 +101,20 @@ std::optional<Entity> DirectionalGaussianBlurFilterContents::RenderFilter(
return std::nullopt;
}
auto radius = Radius{blur_sigma_}.radius;
// Input 0 snapshot.
auto input_snapshot =
inputs[0]->GetSnapshot("GaussianBlur", renderer, entity);
std::optional<Rect> expanded_coverage_hint;
if (coverage_hint.has_value()) {
auto r = Size(radius, radius).Abs();
expanded_coverage_hint =
is_first_pass ? Rect(coverage_hint.value().origin - r,
Size(coverage_hint.value().size + r * 2))
: coverage_hint;
}
auto input_snapshot = inputs[0]->GetSnapshot("GaussianBlur", renderer, entity,
expanded_coverage_hint);
if (!input_snapshot.has_value()) {
return std::nullopt;
}
@@ -112,8 +125,6 @@ std::optional<Entity> DirectionalGaussianBlurFilterContents::RenderFilter(
entity.GetStencilDepth()); // No blur to render.
}
auto radius = Radius{blur_sigma_}.radius;
auto transform = entity.GetTransformation() * effect_transform.Basis();
auto transformed_blur_radius =
transform.TransformDirection(blur_direction_ * radius);
@@ -146,11 +157,21 @@ std::optional<Entity> DirectionalGaussianBlurFilterContents::RenderFilter(
pass_texture_rect.origin.x -= transformed_blur_radius_length;
pass_texture_rect.size.width += transformed_blur_radius_length * 2;
// Crop the pass texture with the rotated coverage hint if one was given.
if (expanded_coverage_hint.has_value()) {
auto maybe_pass_texture_rect = pass_texture_rect.Intersection(
expanded_coverage_hint->TransformBounds(texture_rotate));
if (!maybe_pass_texture_rect.has_value()) {
return std::nullopt;
}
pass_texture_rect = *maybe_pass_texture_rect;
}
// Source override snapshot.
auto source = source_override_ ? source_override_ : inputs[0];
auto source_snapshot =
source->GetSnapshot("GaussianBlur(Override)", renderer, entity);
auto source_snapshot = source->GetSnapshot("GaussianBlur(Override)", renderer,
entity, GetCoverageHint());
if (!source_snapshot.has_value()) {
return std::nullopt;
}
@@ -172,8 +193,9 @@ std::optional<Entity> DirectionalGaussianBlurFilterContents::RenderFilter(
/// Render to texture.
///
ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
RenderPass& pass) {
ContentContext::SubpassCallback subpass_callback = [&](const ContentContext&
renderer,
RenderPass& pass) {
auto& host_buffer = pass.GetTransientsBuffer();
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
@@ -294,8 +316,8 @@ std::optional<Entity> DirectionalGaussianBlurFilterContents::RenderFilter(
{
scale.x = scale_curve(transformed_blur_radius_length);
Scalar y_radius = std::abs(pass_transform.GetDirectionScale(Vector2(
0, source_override_ ? Radius{secondary_blur_sigma_}.radius : 1)));
Scalar y_radius = std::abs(pass_transform.GetDirectionScale(
Vector2(0, !is_first_pass ? Radius{secondary_blur_sigma_}.radius : 1)));
scale.y = scale_curve(y_radius);
}
@@ -303,7 +325,7 @@ std::optional<Entity> DirectionalGaussianBlurFilterContents::RenderFilter(
ISize floored_size = ISize(scaled_size.x, scaled_size.y);
auto out_texture = renderer.MakeSubpass("Directional Gaussian Blur Filter",
floored_size, callback);
floored_size, subpass_callback);
if (!out_texture) {
return std::nullopt;
@@ -339,7 +361,7 @@ std::optional<Rect> DirectionalGaussianBlurFilterContents::GetFilterCoverage(
auto transform = inputs[0]->GetTransform(entity) * effect_transform.Basis();
auto transformed_blur_vector =
transform.TransformDirection(blur_direction_* Radius{blur_sigma_}.radius)
transform.TransformDirection(blur_direction_ * Radius{blur_sigma_}.radius)
.Abs();
auto extent = coverage->size + transformed_blur_vector * 2;
return Rect(coverage->origin - transformed_blur_vector,

View File

@@ -37,11 +37,13 @@ class DirectionalGaussianBlurFilterContents final : public FilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
Sigma blur_sigma_;
Sigma secondary_blur_sigma_;
Vector2 blur_direction_;

View File

@@ -24,14 +24,15 @@ FilterInput::Variant ContentsFilterInput::GetInput() const {
std::optional<Snapshot> ContentsFilterInput::GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity) const {
const Entity& entity,
std::optional<Rect> coverage_limit) const {
if (!snapshot_.has_value()) {
snapshot_ = contents_->RenderToSnapshot(
renderer, // renderer
entity, // entity
std::nullopt, // coverage_limit
std::nullopt, // sampler_descriptor
msaa_enabled_, // msaa_enabled
renderer, // renderer
entity, // entity
coverage_limit, // coverage_limit
std::nullopt, // sampler_descriptor
msaa_enabled_, // msaa_enabled
SPrintF("Contents to %s Filter Snapshot", label.c_str())); // label
}
return snapshot_;

View File

@@ -16,9 +16,11 @@ class ContentsFilterInput final : public FilterInput {
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(const std::string& label,
const ContentContext& renderer,
const Entity& entity) const override;
std::optional<Snapshot> GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

View File

@@ -24,14 +24,15 @@ FilterInput::Variant FilterContentsFilterInput::GetInput() const {
std::optional<Snapshot> FilterContentsFilterInput::GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity) const {
const Entity& entity,
std::optional<Rect> coverage_limit) const {
if (!snapshot_.has_value()) {
snapshot_ = filter_->RenderToSnapshot(
renderer, // renderer
entity, // entity
std::nullopt, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
renderer, // renderer
entity, // entity
coverage_limit, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
SPrintF("Filter to %s Filter Snapshot", label.c_str())); // label
}
return snapshot_;

View File

@@ -16,9 +16,11 @@ class FilterContentsFilterInput final : public FilterInput {
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(const std::string& label,
const ContentContext& renderer,
const Entity& entity) const override;
std::optional<Snapshot> GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

View File

@@ -45,9 +45,11 @@ class FilterInput {
virtual Variant GetInput() const = 0;
virtual std::optional<Snapshot> GetSnapshot(const std::string& label,
const ContentContext& renderer,
const Entity& entity) const = 0;
virtual std::optional<Snapshot> GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit = std::nullopt) const = 0;
std::optional<Rect> GetLocalCoverage(const Entity& entity) const;

View File

@@ -23,7 +23,8 @@ FilterInput::Variant TextureFilterInput::GetInput() const {
std::optional<Snapshot> TextureFilterInput::GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity) const {
const Entity& entity,
std::optional<Rect> coverage_limit) const {
auto snapshot =
Snapshot{.texture = texture_, .transform = GetTransform(entity)};
if (texture_->GetMipCount() > 1) {

View File

@@ -18,9 +18,11 @@ class TextureFilterInput final : public FilterInput {
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(const std::string& label,
const ContentContext& renderer,
const Entity& entity) const override;
std::optional<Snapshot> GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

View File

@@ -23,7 +23,8 @@ std::optional<Entity> LinearToSrgbFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
if (inputs.empty()) {
return std::nullopt;
}

View File

@@ -17,11 +17,13 @@ class LinearToSrgbFilterContents final : public ColorFilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
FML_DISALLOW_COPY_AND_ASSIGN(LinearToSrgbFilterContents);
};

View File

@@ -24,7 +24,8 @@ std::optional<Entity> LocalMatrixFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
return Entity::FromSnapshot(
inputs[0]->GetSnapshot("LocalMatrix", renderer, entity),
entity.GetBlendMode(), entity.GetStencilDepth());

View File

@@ -22,11 +22,13 @@ class LocalMatrixFilterContents final : public FilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
Matrix matrix_;

View File

@@ -27,7 +27,8 @@ std::optional<Entity> MatrixFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
auto snapshot = inputs[0]->GetSnapshot("Matrix", renderer, entity);
if (!snapshot.has_value()) {
return std::nullopt;

View File

@@ -29,11 +29,13 @@ class MatrixFilterContents final : public FilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
Matrix matrix_;
SamplerDescriptor sampler_descriptor_ = {};

View File

@@ -39,7 +39,8 @@ std::optional<Entity> DirectionalMorphologyFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
using VS = MorphologyFilterPipeline::VertexShader;
using FS = MorphologyFilterPipeline::FragmentShader;

View File

@@ -31,11 +31,13 @@ class DirectionalMorphologyFilterContents final : public FilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
Radius radius_;
Vector2 direction_;

View File

@@ -23,7 +23,8 @@ std::optional<Entity> SrgbToLinearFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
if (inputs.empty()) {
return std::nullopt;
}

View File

@@ -17,11 +17,13 @@ class SrgbToLinearFilterContents final : public ColorFilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
FML_DISALLOW_COPY_AND_ASSIGN(SrgbToLinearFilterContents);
};

View File

@@ -42,7 +42,8 @@ std::optional<Entity> YUVToRGBFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
if (inputs.size() < 2) {
return std::nullopt;
}

View File

@@ -18,11 +18,13 @@ class YUVToRGBFilterContents final : public FilterContents {
private:
// |FilterContents|
std::optional<Entity> RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
std::optional<Entity> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const override;
YUVColorSpace yuv_color_space_ = YUVColorSpace::kBT601LimitedRange;

View File

@@ -75,7 +75,7 @@ size_t EntityPass::GetSubpassesDepth() const {
}
std::optional<Rect> EntityPass::GetElementsCoverage(
std::optional<Rect> coverage_crop) const {
std::optional<Rect> coverage_limit) const {
std::optional<Rect> result;
for (const auto& element : elements_) {
std::optional<Rect> coverage;
@@ -83,12 +83,12 @@ std::optional<Rect> EntityPass::GetElementsCoverage(
if (auto entity = std::get_if<Entity>(&element)) {
coverage = entity->GetCoverage();
if (coverage.has_value() && coverage_crop.has_value()) {
coverage = coverage->Intersection(coverage_crop.value());
if (coverage.has_value() && coverage_limit.has_value()) {
coverage = coverage->Intersection(coverage_limit.value());
}
} else if (auto subpass =
std::get_if<std::unique_ptr<EntityPass>>(&element)) {
coverage = GetSubpassCoverage(*subpass->get(), coverage_crop);
coverage = GetSubpassCoverage(*subpass->get(), coverage_limit);
} else {
FML_UNREACHABLE();
}
@@ -107,8 +107,8 @@ std::optional<Rect> EntityPass::GetElementsCoverage(
std::optional<Rect> EntityPass::GetSubpassCoverage(
const EntityPass& subpass,
std::optional<Rect> coverage_clip) const {
auto entities_coverage = subpass.GetElementsCoverage(coverage_clip);
std::optional<Rect> coverage_limit) const {
auto entities_coverage = subpass.GetElementsCoverage(coverage_limit);
// The entities don't cover anything. There is nothing to do.
if (!entities_coverage.has_value()) {
return std::nullopt;
@@ -434,50 +434,56 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
pass_context.EndPass();
}
auto subpass_coverage =
GetSubpassCoverage(*subpass, Rect::MakeSize(root_pass_size));
if (subpass->cover_whole_screen_) {
subpass_coverage =
Rect(global_pass_position, Size(pass_context.GetPassTarget()
.GetRenderTarget()
.GetRenderTargetSize()));
}
if (backdrop_filter_contents) {
auto backdrop_coverage = backdrop_filter_contents->GetCoverage(Entity{});
if (backdrop_coverage.has_value()) {
backdrop_coverage->origin += global_pass_position;
subpass_coverage =
subpass_coverage.has_value()
? subpass_coverage->Union(backdrop_coverage.value())
: backdrop_coverage;
}
if (stencil_coverage_stack.empty() ||
!stencil_coverage_stack.back().coverage.has_value()) {
// The current clip is empty. This means the pass texture won't be
// visible, so skip it.
return EntityPass::EntityResult::Skip();
}
// The maximum coverage of the subpass. Subpasses textures should never
// extend outside the parent pass texture or the current clip coverage.
auto coverage_limit =
Rect(global_pass_position, Size(pass_context.GetPassTarget()
.GetRenderTarget()
.GetRenderTargetSize()))
.Intersection(*stencil_coverage_stack.back().coverage);
if (!coverage_limit.has_value()) {
return EntityPass::EntityResult::Skip();
}
coverage_limit =
coverage_limit->Intersection(Rect::MakeSize(root_pass_size));
if (!coverage_limit.has_value()) {
return EntityPass::EntityResult::Skip();
}
auto subpass_coverage = (subpass->flood_clip_ || backdrop_filter_contents)
? coverage_limit
: GetSubpassCoverage(*subpass, coverage_limit);
if (!subpass_coverage.has_value()) {
// The subpass doesn't contain anything visible, so skip it.
return EntityPass::EntityResult::Skip();
}
subpass_coverage =
subpass_coverage->Intersection(Rect::MakeSize(root_pass_size));
if (!subpass_coverage.has_value() ||
ISize(subpass_coverage->size).IsEmpty()) {
// The subpass doesn't contain anything visible, so skip it.
auto subpass_size = ISize(subpass_coverage->size);
if (subpass_size.IsEmpty()) {
return EntityPass::EntityResult::Skip();
}
auto subpass_target =
CreateRenderTarget(renderer, //
ISize(subpass_coverage->size), //
subpass->GetTotalPassReads(renderer) > 0, //
clear_color_.Premultiply());
auto subpass_target = CreateRenderTarget(
renderer, // renderer
subpass_size, // size
subpass->GetTotalPassReads(renderer) > 0, // readable
clear_color_.Premultiply()); // clear_color
if (!subpass_target.IsValid()) {
VALIDATION_LOG << "Subpass render target is invalid.";
return EntityPass::EntityResult::Failure();
}
FML_LOG(ERROR) << "Origin: " << subpass_coverage->origin
<< " Size: " << subpass_coverage->size;
// Stencil textures aren't shared between EntityPasses (as much of the
// time they are transient).
if (!subpass->OnRender(renderer, // renderer
@@ -682,10 +688,21 @@ bool EntityPass::OnRender(
return false;
}
// Tell the backdrop contents which portion of the rendered output will
// actually be used. The contents may optionally use this hint to avoid
// unnecessary rendering work.
if (!stencil_coverage_stack.empty() &&
stencil_coverage_stack.back().coverage.has_value()) {
auto coverage_hint = Rect(
stencil_coverage_stack.back().coverage->origin - global_pass_position,
stencil_coverage_stack.back().coverage->size);
backdrop_filter_contents->SetCoverageHint(coverage_hint);
}
Entity backdrop_entity;
backdrop_entity.SetContents(std::move(backdrop_filter_contents));
backdrop_entity.SetTransformation(
Matrix::MakeTranslation(Vector3(local_pass_position)));
Matrix::MakeTranslation(Vector3(-local_pass_position)));
backdrop_entity.SetStencilDepth(stencil_depth_floor);
render_element(backdrop_entity);
@@ -758,7 +775,7 @@ bool EntityPass::OnRender(
FilterInput::Make(result.entity.GetContents())};
auto contents = ColorFilterContents::MakeBlend(
result.entity.GetBlendMode(), inputs);
contents->SetCoverageCrop(result.entity.GetCoverage());
contents->SetCoverageHint(result.entity.GetCoverage());
result.entity.SetContents(std::move(contents));
result.entity.SetBlendMode(BlendMode::kSource);
}
@@ -876,7 +893,7 @@ void EntityPass::SetStencilDepth(size_t stencil_depth) {
void EntityPass::SetBlendMode(BlendMode blend_mode) {
blend_mode_ = blend_mode;
cover_whole_screen_ = Entity::IsBlendModeDestructive(blend_mode);
flood_clip_ = Entity::IsBlendModeDestructive(blend_mode);
}
void EntityPass::SetClearColor(Color clear_color) {

View File

@@ -96,10 +96,10 @@ class EntityPass {
std::optional<Rect> GetSubpassCoverage(
const EntityPass& subpass,
std::optional<Rect> coverage_crop) const;
std::optional<Rect> coverage_limit) const;
std::optional<Rect> GetElementsCoverage(
std::optional<Rect> coverage_crop) const;
std::optional<Rect> coverage_limit) const;
private:
struct EntityResult {
@@ -209,7 +209,7 @@ class EntityPass {
Matrix xformation_;
size_t stencil_depth_ = 0u;
BlendMode blend_mode_ = BlendMode::kSourceOver;
bool cover_whole_screen_ = false;
bool flood_clip_ = false;
Color clear_color_ = Color::BlackTransparent();
bool enable_offscreen_debug_checkerboard_ = false;

View File

@@ -202,7 +202,7 @@ TEST_P(EntityTest, FilterCoverageRespectsCropRect) {
// With the crop rect.
{
auto expected = Rect::MakeLTRB(50, 50, 100, 100);
filter->SetCoverageCrop(expected);
filter->SetCoverageHint(expected);
auto actual = filter->GetCoverage({});
ASSERT_TRUE(actual.has_value());

View File

@@ -82,6 +82,8 @@ struct TSize {
};
}
constexpr TSize Abs() const { return {std::fabs(width), std::fabs(height)}; }
constexpr TSize Floor() const {
return {std::floor(width), std::floor(height)};
}

View File

@@ -23,6 +23,8 @@ static const std::vector<std::string> kSkipTests = {
"impeller_Play_AiksTest_CanRenderRadialGradient_Vulkan",
"impeller_Play_AiksTest_CanRenderRadialGradientManyColors_Metal",
"impeller_Play_AiksTest_CanRenderRadialGradientManyColors_Vulkan",
"impeller_Play_AiksTest_CanRenderBackdropBlurInteractive_Metal",
"impeller_Play_AiksTest_CanRenderBackdropBlurInteractive_Vulkan",
"impeller_Play_AiksTest_TextFrameSubpixelAlignment_Metal",
"impeller_Play_AiksTest_TextFrameSubpixelAlignment_Vulkan",
"impeller_Play_AiksTest_ColorWheel_Metal",