[Impeller] implements incorporating the gaussian blur snapshot transform (flutter/engine#48426)

fixes https://github.com/flutter/flutter/issues/139085

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I signed the [CLA].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
This commit is contained in:
gaaclarke
2023-11-28 10:17:18 -08:00
committed by GitHub
parent b934c49920
commit 398c7780f2
3 changed files with 186 additions and 10 deletions

View File

@@ -6,7 +6,9 @@
#include "gmock/gmock.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h"
#include "impeller/entity/contents/texture_contents.h"
#include "impeller/entity/entity_playground.h"
#include "impeller/geometry/geometry_asserts.h"
#include "impeller/renderer/testing/mocks.h"
namespace impeller {
@@ -114,14 +116,108 @@ TEST_P(DirectionalGaussianBlurFilterContentsTest,
EXPECT_TRUE(result_coverage.has_value());
EXPECT_TRUE(contents_coverage.has_value());
if (result_coverage.has_value() && contents_coverage.has_value()) {
EXPECT_NEAR(result_coverage.value().GetLeft(),
contents_coverage.value().GetLeft(), kEhCloseEnough);
EXPECT_NEAR(result_coverage.value().GetTop(),
contents_coverage.value().GetTop(), kEhCloseEnough);
EXPECT_NEAR(result_coverage.value().GetRight(),
contents_coverage.value().GetRight(), kEhCloseEnough);
EXPECT_NEAR(result_coverage.value().GetBottom(),
contents_coverage.value().GetBottom(), kEhCloseEnough);
EXPECT_TRUE(RectNear(result_coverage.value(), contents_coverage.value()));
// The precision on this blur is kind of off, thus the tolerance.
EXPECT_NEAR(result_coverage.value().GetLeft(), -1.f, 0.1f);
EXPECT_NEAR(result_coverage.value().GetTop(), 0.f, 0.001f);
EXPECT_NEAR(result_coverage.value().GetRight(), 101.f, 0.1f);
EXPECT_NEAR(result_coverage.value().GetBottom(), 100.f, 0.001f);
}
}
}
TEST_P(DirectionalGaussianBlurFilterContentsTest,
TextureContentsWithDestinationRect) {
TextureDescriptor desc = {
.storage_mode = StorageMode::kDevicePrivate,
.format = PixelFormat::kB8G8R8A8UNormInt,
.size = ISize(100, 100),
};
std::shared_ptr<Texture> texture =
GetContentContext()->GetContext()->GetResourceAllocator()->CreateTexture(
desc);
auto texture_contents = std::make_shared<TextureContents>();
texture_contents->SetSourceRect(Rect::MakeSize(texture->GetSize()));
texture_contents->SetTexture(texture);
texture_contents->SetDestinationRect(Rect::MakeXYWH(
50, 40, texture->GetSize().width, texture->GetSize().height));
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
auto contents = std::make_unique<DirectionalGaussianBlurFilterContents>();
contents->SetSigma(Sigma{sigma_radius_1});
contents->SetDirection({1.0, 0.0});
contents->SetInputs({FilterInput::Make(texture_contents)});
std::shared_ptr<ContentContext> renderer = GetContentContext();
Entity entity;
std::optional<Entity> result =
contents->GetEntity(*renderer, entity, /*coverage_hint=*/{});
EXPECT_TRUE(result.has_value());
if (result.has_value()) {
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
std::optional<Rect> result_coverage = result.value().GetCoverage();
std::optional<Rect> contents_coverage = contents->GetCoverage(entity);
EXPECT_TRUE(result_coverage.has_value());
EXPECT_TRUE(contents_coverage.has_value());
if (result_coverage.has_value() && contents_coverage.has_value()) {
EXPECT_TRUE(RectNear(result_coverage.value(), contents_coverage.value()));
// The precision on this blur is kind of off, thus the tolerance.
EXPECT_NEAR(result_coverage.value().GetLeft(), 49.f, 0.1f);
EXPECT_NEAR(result_coverage.value().GetTop(), 40.f, 0.001f);
EXPECT_NEAR(result_coverage.value().GetRight(), 151.f, 0.1f);
EXPECT_NEAR(result_coverage.value().GetBottom(), 140.f, 0.001f);
}
}
}
TEST_P(DirectionalGaussianBlurFilterContentsTest,
TextureContentsWithDestinationRectScaled) {
TextureDescriptor desc = {
.storage_mode = StorageMode::kDevicePrivate,
.format = PixelFormat::kB8G8R8A8UNormInt,
.size = ISize(100, 100),
};
std::shared_ptr<Texture> texture =
GetContentContext()->GetContext()->GetResourceAllocator()->CreateTexture(
desc);
auto texture_contents = std::make_shared<TextureContents>();
texture_contents->SetSourceRect(Rect::MakeSize(texture->GetSize()));
texture_contents->SetTexture(texture);
texture_contents->SetDestinationRect(Rect::MakeXYWH(
50, 40, texture->GetSize().width, texture->GetSize().height));
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
auto contents = std::make_unique<DirectionalGaussianBlurFilterContents>();
contents->SetSigma(Sigma{sigma_radius_1});
contents->SetDirection({1.0, 0.0});
contents->SetInputs({FilterInput::Make(texture_contents)});
std::shared_ptr<ContentContext> renderer = GetContentContext();
Entity entity;
entity.SetTransform(Matrix::MakeScale({2.0, 2.0, 1.0}));
std::optional<Entity> result =
contents->GetEntity(*renderer, entity, /*coverage_hint=*/{});
EXPECT_TRUE(result.has_value());
if (result.has_value()) {
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
std::optional<Rect> result_coverage = result.value().GetCoverage();
std::optional<Rect> contents_coverage = contents->GetCoverage(entity);
EXPECT_TRUE(result_coverage.has_value());
EXPECT_TRUE(contents_coverage.has_value());
if (result_coverage.has_value() && contents_coverage.has_value()) {
// The precision on this blur is kind of off, thus the tolerance.
EXPECT_NEAR(result_coverage.value().GetLeft(), 98.f, 0.1f);
EXPECT_NEAR(result_coverage.value().GetTop(), 80.f, 0.001f);
EXPECT_NEAR(result_coverage.value().GetRight(), 302.f, 0.1f);
EXPECT_NEAR(result_coverage.value().GetBottom(), 280.f, 0.001f);
EXPECT_NEAR(contents_coverage.value().GetLeft(), 98.f, 0.1f);
EXPECT_NEAR(contents_coverage.value().GetTop(), 80.f, 0.001f);
EXPECT_NEAR(contents_coverage.value().GetRight(), 302.f, 0.1f);
EXPECT_NEAR(contents_coverage.value().GetBottom(), 280.f, 0.001f);
}
}
}

View File

@@ -183,7 +183,9 @@ std::optional<Rect> GaussianBlurFilterContents::GetFilterCoverage(
Scalar blur_radius = CalculateBlurRadius(sigma_);
Vector3 blur_radii =
effect_transform.Basis() * Vector3{blur_radius, blur_radius, 0.0};
(inputs[0]->GetTransform(entity).Basis() * effect_transform.Basis() *
Vector3{blur_radius, blur_radius, 0.0})
.Abs();
return input_coverage.value().Expand(Point(blur_radii.x, blur_radii.y));
}
@@ -257,7 +259,7 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
return Entity::FromSnapshot(
Snapshot{
.texture = pass3_out_texture,
.transform = entity.GetTransform() *
.transform = input_snapshot->transform *
Matrix::MakeTranslation({-padding.x, -padding.y, 0}) *
Matrix::MakeScale(padded_size /
Vector2(pass1_out_texture->GetSize())),

View File

@@ -6,6 +6,7 @@
#include "fml/status_or.h"
#include "gmock/gmock.h"
#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h"
#include "impeller/entity/contents/texture_contents.h"
#include "impeller/entity/entity_playground.h"
#include "impeller/geometry/geometry_asserts.h"
#include "impeller/renderer/testing/mocks.h"
@@ -194,6 +195,7 @@ TEST_P(GaussianBlurFilterContentsTest,
std::shared_ptr<ContentContext> renderer = GetContentContext();
Entity entity;
// Rotate around the top left corner, then push it over to (100, 100).
entity.SetTransform(Matrix::MakeTranslation({400, 100, 0}) *
Matrix::MakeRotationZ(Degrees(90.0)));
std::optional<Entity> result =
@@ -232,5 +234,81 @@ TEST_P(GaussianBlurFilterContentsTest, CalculateUVsSimple) {
}
}
TEST_P(GaussianBlurFilterContentsTest, TextureContentsWithDestinationRect) {
TextureDescriptor desc = {
.storage_mode = StorageMode::kDevicePrivate,
.format = PixelFormat::kB8G8R8A8UNormInt,
.size = ISize(100, 100),
};
std::shared_ptr<Texture> texture = MakeTexture(desc);
auto texture_contents = std::make_shared<TextureContents>();
texture_contents->SetSourceRect(Rect::MakeSize(texture->GetSize()));
texture_contents->SetTexture(texture);
texture_contents->SetDestinationRect(Rect::MakeXYWH(
50, 40, texture->GetSize().width, texture->GetSize().height));
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
auto contents = std::make_unique<GaussianBlurFilterContents>(sigma_radius_1);
contents->SetInputs({FilterInput::Make(texture_contents)});
std::shared_ptr<ContentContext> renderer = GetContentContext();
Entity entity;
std::optional<Entity> result =
contents->GetEntity(*renderer, entity, /*coverage_hint=*/{});
EXPECT_TRUE(result.has_value());
if (result.has_value()) {
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
std::optional<Rect> result_coverage = result.value().GetCoverage();
std::optional<Rect> contents_coverage = contents->GetCoverage(entity);
EXPECT_TRUE(result_coverage.has_value());
EXPECT_TRUE(contents_coverage.has_value());
if (result_coverage.has_value() && contents_coverage.has_value()) {
EXPECT_TRUE(RectNear(result_coverage.value(), contents_coverage.value()));
EXPECT_TRUE(RectNear(result_coverage.value(),
Rect::MakeLTRB(49.f, 39.f, 151.f, 141.f)));
}
}
}
TEST_P(GaussianBlurFilterContentsTest,
TextureContentsWithDestinationRectScaled) {
TextureDescriptor desc = {
.storage_mode = StorageMode::kDevicePrivate,
.format = PixelFormat::kB8G8R8A8UNormInt,
.size = ISize(100, 100),
};
std::shared_ptr<Texture> texture = MakeTexture(desc);
auto texture_contents = std::make_shared<TextureContents>();
texture_contents->SetSourceRect(Rect::MakeSize(texture->GetSize()));
texture_contents->SetTexture(texture);
texture_contents->SetDestinationRect(Rect::MakeXYWH(
50, 40, texture->GetSize().width, texture->GetSize().height));
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
auto contents = std::make_unique<GaussianBlurFilterContents>(sigma_radius_1);
contents->SetInputs({FilterInput::Make(texture_contents)});
std::shared_ptr<ContentContext> renderer = GetContentContext();
Entity entity;
entity.SetTransform(Matrix::MakeScale({2.0, 2.0, 1.0}));
std::optional<Entity> result =
contents->GetEntity(*renderer, entity, /*coverage_hint=*/{});
EXPECT_TRUE(result.has_value());
if (result.has_value()) {
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
std::optional<Rect> result_coverage = result.value().GetCoverage();
std::optional<Rect> contents_coverage = contents->GetCoverage(entity);
EXPECT_TRUE(result_coverage.has_value());
EXPECT_TRUE(contents_coverage.has_value());
if (result_coverage.has_value() && contents_coverage.has_value()) {
EXPECT_TRUE(RectNear(result_coverage.value(), contents_coverage.value()));
EXPECT_TRUE(RectNear(contents_coverage.value(),
Rect::MakeLTRB(98.f, 78.f, 302.f, 282.f)));
}
}
}
} // namespace testing
} // namespace impeller