Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] scales blur coverage to match rendered output #47621

Merged
merged 17 commits into from
Nov 6, 2023
1 change: 1 addition & 0 deletions ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
../../../flutter/impeller/display_list/skia_conversions_unittests.cc
../../../flutter/impeller/docs
../../../flutter/impeller/entity/contents/checkerboard_contents_unittests.cc
../../../flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents_unittests.cc
../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc
../../../flutter/impeller/entity/entity_unittests.cc
../../../flutter/impeller/entity/geometry/geometry_unittests.cc
Expand Down
1 change: 1 addition & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ impeller_component("entity_unittests") {

sources = [
"contents/checkerboard_contents_unittests.cc",
"contents/filters/directional_gaussian_blur_filter_contents_unittests.cc",
"contents/filters/inputs/filter_input_unittests.cc",
"entity_playground.cc",
"entity_playground.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ 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{ScaleSigma(blur_sigma_)}.radius)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, missed this!

.Abs();
return coverage->Expand(transformed_blur_vector);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// 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 "flutter/testing/testing.h"
#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/entity_playground.h"
#include "impeller/renderer/testing/mocks.h"

namespace impeller {
namespace testing {

using ::testing::Return;

namespace {

Scalar CalculateSigmaForBlurRadius(Scalar blur_radius) {
// See Sigma.h
return (blur_radius / kKernelRadiusPerSigma) + 0.5;
}
} // namespace

class DirectionalGaussianBlurFilterContentsTest : public EntityPlayground {
public:
// Stubs in the minimal support to make rendering pass.
void SetupMinimalMockContext() {
// This mocking code was removed since it wasn't strictly needed yet. If it
// is needed you can find it here:
// https://gist.github.com/gaaclarke/c2f6bf5fc6ecb10678da03789abc5843.
}
};

INSTANTIATE_PLAYGROUND_SUITE(DirectionalGaussianBlurFilterContentsTest);

TEST_P(DirectionalGaussianBlurFilterContentsTest, CoverageWithEffectTransform) {
TextureDescriptor desc = {
.format = PixelFormat::kB8G8R8A8UNormInt,
.size = ISize(100, 100),
};
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});
std::shared_ptr<Texture> texture =
GetContentContext()->GetContext()->GetResourceAllocator()->CreateTexture(
desc);
FilterInput::Vector inputs = {FilterInput::Make(texture)};
Entity entity;
entity.SetTransformation(Matrix::MakeTranslation({100, 100, 0}));
std::optional<Rect> coverage = contents->GetFilterCoverage(
inputs, entity, /*effect_transform=*/Matrix::MakeScale({2.0, 2.0, 1.0}));
EXPECT_TRUE(coverage.has_value());
if (coverage.has_value()) {
EXPECT_NEAR(coverage->GetLeft(), 100 - 2,
0.5); // Higher tolerance for sigma scaling.
EXPECT_NEAR(coverage->GetTop(), 100, 0.01);
EXPECT_NEAR(coverage->GetRight(), 200 + 2,
0.5); // Higher tolerance for sigma scaling.
EXPECT_NEAR(coverage->GetBottom(), 200, 0.01);
}
}

TEST(DirectionalGaussianBlurFilterContentsTest, FilterSourceCoverage) {
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});
std::optional<Rect> coverage = contents->GetFilterSourceCoverage(
/*effect_transform=*/Matrix::MakeScale({2.0, 2.0, 1.0}),
/*output_limit=*/Rect::MakeLTRB(100, 100, 200, 200));
ASSERT_EQ(coverage, Rect::MakeLTRB(100 - 2, 100, 200 + 2, 200));
}

TEST_P(DirectionalGaussianBlurFilterContentsTest, RenderNoCoverage) {
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});
std::shared_ptr<ContentContext> renderer = GetContentContext();
Entity entity;
Rect coverage_hint = Rect::MakeLTRB(0, 0, 0, 0);
std::optional<Entity> result =
contents->GetEntity(*renderer, entity, coverage_hint);
ASSERT_FALSE(result.has_value());
}

TEST_P(DirectionalGaussianBlurFilterContentsTest,
RenderCoverageMatchesGetCoverage) {
TextureDescriptor desc = {
.format = PixelFormat::kB8G8R8A8UNormInt,
.size = ISize(100, 100),
};
std::shared_ptr<Texture> texture =
GetContentContext()->GetContext()->GetResourceAllocator()->CreateTexture(
desc);
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)});
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()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an Expect_rect_near macro

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll problem happen if the new blur has better test coverage like i hope.

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);
}
}
}

} // namespace testing
} // namespace impeller
113 changes: 113 additions & 0 deletions impeller/renderer/testing/mocks.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these!

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
#include "impeller/core/texture.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/context.h"
#include "impeller/renderer/pipeline_library.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/render_target.h"
#include "impeller/renderer/sampler_library.h"
#include "impeller/renderer/shader_function.h"
#include "impeller/typographer/typographer_context.h"

namespace impeller {
namespace testing {
Expand Down Expand Up @@ -164,5 +169,113 @@ class MockTexture : public Texture {
(override));
};

class MockTypographerContext : public TypographerContext {
public:
MOCK_METHOD(std::shared_ptr<GlyphAtlas>,
CreateGlyphAtlas,
(Context & context,
GlyphAtlas::Type type,
std::shared_ptr<GlyphAtlasContext> atlas_context,
const FontGlyphMap& font_glyph_map),
(const, override));
MOCK_METHOD(std::shared_ptr<GlyphAtlasContext>,
CreateGlyphAtlasContext,
(),
(const, override));
};

class MockRenderTargetAllocator : public RenderTargetAllocator {
public:
MockRenderTargetAllocator(std::shared_ptr<Allocator> allocator)
: RenderTargetAllocator(allocator) {}
MOCK_METHOD(std::shared_ptr<Texture>,
CreateTexture,
(const TextureDescriptor& desc),
(override));
};

class MockCapabilities : public Capabilities {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to MockCapabilities, there is a CapabilitiesBuilder/StandardCapabilities that lets you set whatever you want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just removed all this work for now. They aren't used. I figured it didn't hurt and may potentially save time in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll probably end up landing most of this with the other blur anyways where it is actually used. We'll see.

public:
MOCK_METHOD(bool, SupportsOffscreenMSAA, (), (const, override));
MOCK_METHOD(bool, SupportsImplicitResolvingMSAA, (), (const, override));
MOCK_METHOD(bool, SupportsSSBO, (), (const, override));
MOCK_METHOD(bool, SupportsBufferToTextureBlits, (), (const, override));
MOCK_METHOD(bool, SupportsTextureToTextureBlits, (), (const, override));
MOCK_METHOD(bool, SupportsFramebufferFetch, (), (const, override));
MOCK_METHOD(bool, SupportsCompute, (), (const, override));
MOCK_METHOD(bool, SupportsComputeSubgroups, (), (const, override));
MOCK_METHOD(bool, SupportsReadFromOnscreenTexture, (), (const, override));
MOCK_METHOD(bool, SupportsReadFromResolve, (), (const, override));
MOCK_METHOD(bool, SupportsDecalSamplerAddressMode, (), (const, override));
MOCK_METHOD(bool, SupportsDeviceTransientTextures, (), (const, override));
MOCK_METHOD(PixelFormat, GetDefaultColorFormat, (), (const, override));
MOCK_METHOD(PixelFormat, GetDefaultStencilFormat, (), (const, override));
MOCK_METHOD(PixelFormat, GetDefaultDepthStencilFormat, (), (const, override));
};

class MockRenderPass : public RenderPass {
public:
MockRenderPass(std::weak_ptr<const Context> context,
const RenderTarget& target)
: RenderPass(context, target) {}
MOCK_METHOD(bool, IsValid, (), (const, override));
MOCK_METHOD(void, OnSetLabel, (std::string), (override));
MOCK_METHOD(bool, OnEncodeCommands, (const Context&), (const, override));
};

class MockSamplerLibrary : public SamplerLibrary {
public:
MOCK_METHOD(std::shared_ptr<const Sampler>,
GetSampler,
(SamplerDescriptor),
(override));
};

class MockShaderLibrary : public ShaderLibrary {
public:
MOCK_METHOD(bool, IsValid, (), (const, override));
MOCK_METHOD(std::shared_ptr<const ShaderFunction>,
GetFunction,
(std::string_view name, ShaderStage stage),
(override));
MOCK_METHOD(void,
UnregisterFunction,
(std::string name, ShaderStage stage),
(override));
};

class MockShaderFunction : public ShaderFunction {
public:
MockShaderFunction(UniqueID parent_library_id,
std::string name,
ShaderStage stage)
: ShaderFunction(parent_library_id, name, stage) {}
};

class MockPipelineLibrary : public PipelineLibrary {
public:
MOCK_METHOD(bool, IsValid, (), (const, override));
MOCK_METHOD(PipelineFuture<PipelineDescriptor>,
GetPipeline,
(PipelineDescriptor descriptor),
(override));
MOCK_METHOD(PipelineFuture<ComputePipelineDescriptor>,
GetPipeline,
(ComputePipelineDescriptor descriptor),
(override));
MOCK_METHOD(void,
RemovePipelinesWithEntryPoint,
(std::shared_ptr<const ShaderFunction> function),
(override));
};

template <typename T>
class MockPipeline : public Pipeline<T> {
public:
MockPipeline(std::weak_ptr<PipelineLibrary> library, T desc)
: Pipeline<T>(library, desc) {}
MOCK_METHOD(bool, IsValid, (), (const, override));
};

} // namespace testing
} // namespace impeller