From 6f471e902747d5722ad34b67780bbef825b3ac91 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:34:22 -0800 Subject: [PATCH] [Impeller] Implements gaussian blur that scales down before applying the blur (flutter/engine#47576) A partial implementation of the new gaussian blur effect. This should perform enough of the code to start getting some performance numbers. Known outstanding problems: 1) The edges of the blur are clipped. I have notes on how I plan on expanding render space in the PR. 1) Animating the sigma causes some "jumping around artifacts" resulting from the downsampling (maybe the discrete nature of texture pixel size?) 1) Coverage hints are ignored. I think depth tests will make that not much of an issue. 1) We aren't ping ponging textures yet 1) The snapshot's transform is ignored. issue: https://github.com/flutter/flutter/issues/131580 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../flutter/ci/licenses_golden/excluded_files | 1 + .../ci/licenses_golden/licenses_flutter | 4 + engine/src/flutter/impeller/entity/BUILD.gn | 3 + ...rectional_gaussian_blur_filter_contents.cc | 1 + .../contents/filters/filter_contents.cc | 15 + .../filters/gaussian_blur_filter_contents.cc | 283 ++++++++++++++++++ .../filters/gaussian_blur_filter_contents.h | 58 ++++ ...gaussian_blur_filter_contents_unittests.cc | 242 +++++++++++++++ .../impeller/entity/entity_unittests.cc | 18 +- .../shaders/gaussian_blur/gaussian_blur.glsl | 3 +- .../impeller/geometry/geometry_asserts.h | 6 +- engine/src/flutter/impeller/geometry/matrix.h | 9 + engine/src/flutter/impeller/geometry/point.h | 1 + engine/src/flutter/impeller/geometry/rect.h | 5 + .../impeller/geometry/rect_unittests.cc | 14 + engine/src/flutter/impeller/tools/malioc.json | 10 +- 16 files changed, 658 insertions(+), 15 deletions(-) create mode 100644 engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc create mode 100644 engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h create mode 100644 engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index 8cb9f9e4ed..4ec705af54 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -140,6 +140,7 @@ ../../../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/gaussian_blur_filter_contents_unittests.cc ../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc ../../../flutter/impeller/entity/contents/test ../../../flutter/impeller/entity/contents/vertices_contents_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index c679184313..6f405bc057 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -3062,6 +3062,8 @@ ORIGIN: ../../../flutter/impeller/entity/contents/filters/directional_gaussian_b ORIGIN: ../../../flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/contents/filters/filter_contents.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/contents/filters/filter_contents.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/contents_filter_input.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/contents_filter_input.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc + ../../../flutter/LICENSE @@ -5829,6 +5831,8 @@ FILE: ../../../flutter/impeller/entity/contents/filters/directional_gaussian_blu FILE: ../../../flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h FILE: ../../../flutter/impeller/entity/contents/filters/filter_contents.cc FILE: ../../../flutter/impeller/entity/contents/filters/filter_contents.h +FILE: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +FILE: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h FILE: ../../../flutter/impeller/entity/contents/filters/inputs/contents_filter_input.cc FILE: ../../../flutter/impeller/entity/contents/filters/inputs/contents_filter_input.h FILE: ../../../flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 89d3071050..8922c21b27 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -134,6 +134,8 @@ impeller_component("entity") { "contents/filters/directional_gaussian_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", @@ -255,6 +257,7 @@ impeller_component("entity_unittests") { sources = [ "contents/checkerboard_contents_unittests.cc", "contents/filters/directional_gaussian_blur_filter_contents_unittests.cc", + "contents/filters/gaussian_blur_filter_contents_unittests.cc", "contents/filters/inputs/filter_input_unittests.cc", "contents/vertices_contents_unittests.cc", "entity_pass_target_unittests.cc", diff --git a/engine/src/flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.cc index e7bdf15efa..47172e3ead 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.cc @@ -185,6 +185,7 @@ std::optional DirectionalGaussianBlurFilterContents::RenderFilter( auto r = Radius{transformed_blur_radius_length}; frag_info.blur_sigma = Sigma{r}.sigma; frag_info.blur_radius = std::round(r.radius); + frag_info.step_size = 2.0; // The blur direction is in input UV space. frag_info.blur_uv_offset = 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 52349fa244..3414a24c96 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -17,6 +17,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/contents/filters/border_mask_blur_filter_contents.h" #include "impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h" +#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h" #include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/contents/filters/local_matrix_filter_contents.h" #include "impeller/entity/contents/filters/matrix_filter_contents.h" @@ -55,6 +56,20 @@ std::shared_ptr FilterContents::MakeGaussianBlur( Sigma sigma_y, BlurStyle blur_style, Entity::TileMode tile_mode) { + constexpr bool use_new_filter = +#ifdef IMPELLER_ENABLE_NEW_GAUSSIAN_FILTER + true; +#else + false; +#endif + + // TODO(https://github.com/flutter/flutter/issues/131580): Remove once the new + // blur handles all cases. + if (use_new_filter) { + auto blur = std::make_shared(sigma_x.sigma); + blur->SetInputs({input}); + return blur; + } std::shared_ptr x_blur = MakeDirectionalGaussianBlur( /*input=*/input, /*sigma=*/sigma_x, diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc new file mode 100644 index 0000000000..02b353a69e --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -0,0 +1,283 @@ +// 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/gaussian_blur_filter_contents.h" + +#include "impeller/entity/contents/content_context.h" +#include "impeller/entity/texture_fill.frag.h" +#include "impeller/entity/texture_fill.vert.h" +#include "impeller/renderer/command.h" +#include "impeller/renderer/render_pass.h" +#include "impeller/renderer/sampler_library.h" + +namespace impeller { + +using GaussianBlurVertexShader = GaussianBlurPipeline::VertexShader; +using GaussianBlurFragmentShader = GaussianBlurPipeline::FragmentShader; + +namespace { +SamplerDescriptor MakeSamplerDescriptor(MinMagFilter filter, + SamplerAddressMode address_mode) { + SamplerDescriptor sampler_desc; + sampler_desc.min_filter = filter; + sampler_desc.mag_filter = filter; + sampler_desc.width_address_mode = address_mode; + sampler_desc.height_address_mode = address_mode; + return sampler_desc; +} + +template +void BindVertices(Command& cmd, + HostBuffer& host_buffer, + std::initializer_list&& vertices) { + VertexBufferBuilder vtx_builder; + vtx_builder.AddVertices(vertices); + auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer); + cmd.BindVertices(vtx_buffer); +} + +std::shared_ptr MakeDownsampleSubpass( + const ContentContext& renderer, + std::shared_ptr input_texture, + const SamplerDescriptor& sampler_descriptor, + const Quad& uvs, + const ISize& subpass_size) { + ContentContext::SubpassCallback subpass_callback = + [&](const ContentContext& renderer, RenderPass& pass) { + HostBuffer& host_buffer = pass.GetTransientsBuffer(); + + Command cmd; + DEBUG_COMMAND_INFO(cmd, "Gaussian blur downsample"); + auto pipeline_options = OptionsFromPass(pass); + pipeline_options.primitive_type = PrimitiveType::kTriangleStrip; + cmd.pipeline = renderer.GetTexturePipeline(pipeline_options); + + TextureFillVertexShader::FrameInfo frame_info; + frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1)); + frame_info.texture_sampler_y_coord_scale = 1.0; + frame_info.alpha = 1.0; + + BindVertices(cmd, host_buffer, + { + {Point(0, 0), uvs[0]}, + {Point(1, 0), uvs[1]}, + {Point(0, 1), uvs[2]}, + {Point(1, 1), uvs[3]}, + }); + + TextureFillVertexShader::BindFrameInfo( + cmd, host_buffer.EmplaceUniform(frame_info)); + TextureFillFragmentShader::BindTextureSampler( + cmd, input_texture, + renderer.GetContext()->GetSamplerLibrary()->GetSampler( + sampler_descriptor)); + + pass.AddCommand(std::move(cmd)); + + return true; + }; + std::shared_ptr out_texture = renderer.MakeSubpass( + "Gaussian Blur Filter", subpass_size, subpass_callback); + return out_texture; +} + +std::shared_ptr MakeBlurSubpass( + const ContentContext& renderer, + std::shared_ptr input_texture, + const SamplerDescriptor& sampler_descriptor, + const GaussianBlurFragmentShader::BlurInfo& blur_info) { + ISize subpass_size = input_texture->GetSize(); + ContentContext::SubpassCallback subpass_callback = + [&](const ContentContext& renderer, RenderPass& pass) { + GaussianBlurVertexShader::FrameInfo frame_info{ + .mvp = Matrix::MakeOrthographic(ISize(1, 1)), + .texture_sampler_y_coord_scale = 1.0}; + + HostBuffer& host_buffer = pass.GetTransientsBuffer(); + + Command cmd; + ContentContextOptions options = OptionsFromPass(pass); + options.primitive_type = PrimitiveType::kTriangleStrip; + cmd.pipeline = renderer.GetGaussianBlurPipeline(options); + BindVertices(cmd, host_buffer, + { + {Point(0, 0), Point(0, 0)}, + {Point(1, 0), Point(1, 0)}, + {Point(0, 1), Point(0, 1)}, + {Point(1, 1), Point(1, 1)}, + }); + + GaussianBlurFragmentShader::BindTextureSampler( + cmd, input_texture, + renderer.GetContext()->GetSamplerLibrary()->GetSampler( + sampler_descriptor)); + GaussianBlurVertexShader::BindFrameInfo( + cmd, host_buffer.EmplaceUniform(frame_info)); + GaussianBlurFragmentShader::BindBlurInfo( + cmd, host_buffer.EmplaceUniform(blur_info)); + pass.AddCommand(std::move(cmd)); + + return true; + }; + std::shared_ptr out_texture = renderer.MakeSubpass( + "Gaussian Blur Filter", subpass_size, subpass_callback); + return out_texture; +} + +/// Given a desired |scalar|, will return the scalar that gets close but leaves +/// |size| in integer sizes. +Vector2 CalculateIntegerScale(Scalar scalar, ISize size) { + ISize new_size(size.width / scalar, size.height / scalar); + return Vector2(size.width / static_cast(new_size.width), + size.height / static_cast(new_size.height)); +} + +/// Calculate how much to scale down the texture depending on the blur radius. +/// This curve was taken from |DirectionalGaussianBlurFilterContents|. +Scalar CalculateScale(Scalar radius) { + constexpr Scalar decay = 4.0; // Larger is more gradual. + constexpr Scalar limit = 0.95; // The maximum percentage of the scaledown. + const Scalar curve = + std::min(1.0, decay / (std::max(1.0f, radius) + decay - 1.0)); + return (curve - 1) * limit + 1; +}; + +} // namespace + +GaussianBlurFilterContents::GaussianBlurFilterContents(Scalar sigma) + : sigma_(sigma) {} + +std::optional GaussianBlurFilterContents::GetFilterSourceCoverage( + const Matrix& effect_transform, + const Rect& output_limit) const { + Scalar blur_radius = CalculateBlurRadius(sigma_); + Vector3 blur_radii = + effect_transform.Basis() * Vector3{blur_radius, blur_radius, 0.0}; + return output_limit.Expand(Point(blur_radii.x, blur_radii.y)); +} + +std::optional GaussianBlurFilterContents::GetFilterCoverage( + const FilterInput::Vector& inputs, + const Entity& entity, + const Matrix& effect_transform) const { + if (inputs.empty()) { + return {}; + } + + std::optional input_coverage = inputs[0]->GetCoverage(entity); + if (!input_coverage.has_value()) { + return {}; + } + + Scalar blur_radius = CalculateBlurRadius(sigma_); + Vector3 blur_radii = + effect_transform.Basis() * Vector3{blur_radius, blur_radius, 0.0}; + return input_coverage.value().Expand(Point(blur_radii.x, blur_radii.y)); +} + +std::optional GaussianBlurFilterContents::RenderFilter( + const FilterInput::Vector& inputs, + const ContentContext& renderer, + const Entity& entity, + const Matrix& effect_transform, + const Rect& coverage, + const std::optional& coverage_hint) const { + if (inputs.empty()) { + return std::nullopt; + } + + std::optional input_snapshot = + inputs[0]->GetSnapshot("GaussianBlur", renderer, entity, + /*coverage_limit=*/coverage_hint); + if (!input_snapshot.has_value()) { + return std::nullopt; + } + + if (sigma_ < kEhCloseEnough) { + return Entity::FromSnapshot(input_snapshot.value(), entity.GetBlendMode(), + entity.GetClipDepth()); // No blur to render. + } + + Scalar blur_radius = CalculateBlurRadius(sigma_); + Scalar desired_scale = 1.0 / CalculateScale(blur_radius); + Vector2 downsample = + CalculateIntegerScale(desired_scale, input_snapshot->texture->GetSize()); + + // TODO(gaaclarke): This isn't taking into account the blur radius to expand + // the rendered size, so blurred objects are clipped. In + // order for that to be implemented correctly we'll need to + // start adjusting the geometry coordinates in the downsample + // step so that there is a border of transparency around it + // before the blur steps. + ISize subpass_size = + ISize(input_snapshot->texture->GetSize().width / downsample.x, + input_snapshot->texture->GetSize().height / downsample.y); + + Quad uvs = + CalculateUVs(inputs[0], entity, input_snapshot->texture->GetSize()); + + std::shared_ptr pass1_out_texture = MakeDownsampleSubpass( + renderer, input_snapshot->texture, input_snapshot->sampler_descriptor, + uvs, subpass_size); + + Size pass1_pixel_size(1.0 / pass1_out_texture->GetSize().width, + 1.0 / pass1_out_texture->GetSize().height); + + std::shared_ptr pass2_out_texture = MakeBlurSubpass( + renderer, pass1_out_texture, input_snapshot->sampler_descriptor, + GaussianBlurFragmentShader::BlurInfo{ + .blur_uv_offset = Point(0.0, pass1_pixel_size.height), + .blur_sigma = sigma_ / downsample.y, + .blur_radius = blur_radius / downsample.y, + .step_size = 1.0, + }); + + // TODO(gaaclarke): Make this pass reuse the texture from pass1. + std::shared_ptr pass3_out_texture = MakeBlurSubpass( + renderer, pass2_out_texture, input_snapshot->sampler_descriptor, + GaussianBlurFragmentShader::BlurInfo{ + .blur_uv_offset = Point(pass1_pixel_size.width, 0.0), + .blur_sigma = sigma_ / downsample.x, + .blur_radius = blur_radius / downsample.x, + .step_size = 1.0, + }); + + SamplerDescriptor sampler_desc = MakeSamplerDescriptor( + MinMagFilter::kLinear, SamplerAddressMode::kClampToEdge); + + return Entity::FromSnapshot( + Snapshot{ + .texture = pass3_out_texture, + .transform = + entity.GetTransformation() * + Matrix::MakeScale( + {input_snapshot->texture->GetSize().width / + static_cast(pass1_out_texture->GetSize().width), + input_snapshot->texture->GetSize().height / + static_cast(pass1_out_texture->GetSize().height), + 1.0}), + .sampler_descriptor = sampler_desc, + .opacity = input_snapshot->opacity}, + entity.GetBlendMode(), entity.GetClipDepth()); +} + +Scalar GaussianBlurFilterContents::CalculateBlurRadius(Scalar sigma) { + return static_cast(Sigma(sigma)).radius; +} + +Quad GaussianBlurFilterContents::CalculateUVs( + const std::shared_ptr& filter_input, + const Entity& entity, + const ISize& texture_size) { + Matrix input_transform = filter_input->GetLocalTransform(entity); + Rect snapshot_rect = + Rect::MakeXYWH(0, 0, texture_size.width, texture_size.height); + Quad coverage_quad = snapshot_rect.GetTransformedPoints(input_transform); + + Matrix uv_transform = Matrix::MakeScale( + {1.0f / texture_size.width, 1.0f / texture_size.height, 1.0f}); + return uv_transform.Transform(coverage_quad); +} + +} // 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 new file mode 100644 index 0000000000..0b85fb17ca --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h @@ -0,0 +1,58 @@ +// 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 "impeller/entity/contents/filters/filter_contents.h" + +namespace impeller { + +/// Performs a bidirectional Gaussian blur. +/// +/// This is accomplished by rendering multiple passes in multiple directions. +/// Note: This will replace `DirectionalGaussianBlurFilterContents`. +class GaussianBlurFilterContents final : public FilterContents { + public: + explicit GaussianBlurFilterContents(Scalar sigma = 0.0f); + + Scalar GetSigma() const { return sigma_; } + + // |FilterContents| + std::optional GetFilterSourceCoverage( + const Matrix& effect_transform, + const Rect& output_limit) const override; + + // |FilterContents| + std::optional GetFilterCoverage( + const FilterInput::Vector& inputs, + const Entity& entity, + const Matrix& effect_transform) const override; + + /// Given a sigma (standard deviation) calculate the blur radius (1/2 the + /// kernel size). + static Scalar CalculateBlurRadius(Scalar sigma); + + /// Calculate the UV coordinates for rendering the filter_input. + /// @param filter_input The FilterInput that should be rendered. + /// @param entity The associated entity for the filter_input. + /// @param texture_size The size of the texture_size the uvs will be used for. + static Quad CalculateUVs(const std::shared_ptr& filter_input, + const Entity& entity, + const ISize& pass_size); + + private: + // |FilterContents| + std::optional RenderFilter( + const FilterInput::Vector& input_textures, + const ContentContext& renderer, + const Entity& entity, + const Matrix& effect_transform, + const Rect& coverage, + const std::optional& coverage_hint) const override; + + const Scalar sigma_ = 0.0; +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc new file mode 100644 index 0000000000..93330a62fa --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc @@ -0,0 +1,242 @@ +// 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 "fml/status_or.h" +#include "gmock/gmock.h" +#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h" +#include "impeller/entity/entity_playground.h" +#include "impeller/geometry/geometry_asserts.h" +#include "impeller/renderer/testing/mocks.h" + +namespace impeller { +namespace testing { + +namespace { + +Scalar CalculateSigmaForBlurRadius(Scalar blur_radius) { + // See Sigma.h + return (blur_radius / kKernelRadiusPerSigma) + 0.5; +} + +} // namespace + +class GaussianBlurFilterContentsTest : public EntityPlayground { + public: + std::shared_ptr MakeTexture(const TextureDescriptor& desc) { + return GetContentContext() + ->GetContext() + ->GetResourceAllocator() + ->CreateTexture(desc); + } +}; +INSTANTIATE_PLAYGROUND_SUITE(GaussianBlurFilterContentsTest); + +TEST(GaussianBlurFilterContentsTest, Create) { + GaussianBlurFilterContents contents; + ASSERT_EQ(contents.GetSigma(), 0.0); +} + +TEST(GaussianBlurFilterContentsTest, CoverageEmpty) { + GaussianBlurFilterContents contents; + FilterInput::Vector inputs = {}; + Entity entity; + std::optional coverage = + contents.GetFilterCoverage(inputs, entity, /*effect_transform=*/Matrix()); + ASSERT_FALSE(coverage.has_value()); +} + +TEST(GaussianBlurFilterContentsTest, CoverageSimple) { + GaussianBlurFilterContents contents; + FilterInput::Vector inputs = { + FilterInput::Make(Rect::MakeLTRB(10, 10, 110, 110))}; + Entity entity; + std::optional coverage = + contents.GetFilterCoverage(inputs, entity, /*effect_transform=*/Matrix()); + ASSERT_EQ(coverage, Rect::MakeLTRB(10, 10, 110, 110)); +} + +TEST(GaussianBlurFilterContentsTest, CoverageWithSigma) { + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); + GaussianBlurFilterContents contents(/*sigma=*/sigma_radius_1); + FilterInput::Vector inputs = { + FilterInput::Make(Rect::MakeLTRB(100, 100, 200, 200))}; + Entity entity; + std::optional coverage = + contents.GetFilterCoverage(inputs, entity, /*effect_transform=*/Matrix()); + ASSERT_EQ(coverage, Rect::MakeLTRB(99, 99, 201, 201)); +} + +TEST_P(GaussianBlurFilterContentsTest, CoverageWithTexture) { + TextureDescriptor desc = { + .format = PixelFormat::kB8G8R8A8UNormInt, + .size = ISize(100, 100), + }; + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); + GaussianBlurFilterContents contents(/*sigma=*/sigma_radius_1); + std::shared_ptr texture = + GetContentContext()->GetContext()->GetResourceAllocator()->CreateTexture( + desc); + FilterInput::Vector inputs = {FilterInput::Make(texture)}; + Entity entity; + entity.SetTransformation(Matrix::MakeTranslation({100, 100, 0})); + std::optional coverage = + contents.GetFilterCoverage(inputs, entity, /*effect_transform=*/Matrix()); + ASSERT_EQ(coverage, Rect::MakeLTRB(99, 99, 201, 201)); +} + +TEST_P(GaussianBlurFilterContentsTest, CoverageWithEffectTransform) { + TextureDescriptor desc = { + .format = PixelFormat::kB8G8R8A8UNormInt, + .size = ISize(100, 100), + }; + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); + GaussianBlurFilterContents contents(/*sigma=*/sigma_radius_1); + std::shared_ptr texture = + GetContentContext()->GetContext()->GetResourceAllocator()->CreateTexture( + desc); + FilterInput::Vector inputs = {FilterInput::Make(texture)}; + Entity entity; + entity.SetTransformation(Matrix::MakeTranslation({100, 100, 0})); + std::optional coverage = contents.GetFilterCoverage( + inputs, entity, /*effect_transform=*/Matrix::MakeScale({2.0, 2.0, 1.0})); + ASSERT_EQ(coverage, Rect::MakeLTRB(100 - 2, 100 - 2, 200 + 2, 200 + 2)); +} + +TEST(GaussianBlurFilterContentsTest, FilterSourceCoverage) { + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); + auto contents = std::make_unique(sigma_radius_1); + std::optional 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 - 2, 200 + 2, 200 + 2)); +} + +TEST_P(GaussianBlurFilterContentsTest, RenderCoverageMatchesGetCoverage) { + TextureDescriptor desc = { + .format = PixelFormat::kB8G8R8A8UNormInt, + .size = ISize(100, 100), + }; + std::shared_ptr texture = MakeTexture(desc); + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); + auto contents = std::make_unique(sigma_radius_1); + contents->SetInputs({FilterInput::Make(texture)}); + std::shared_ptr renderer = GetContentContext(); + + Entity entity; + std::optional 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 result_coverage = result.value().GetCoverage(); + std::optional 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()) { + // TODO(gaaclarke): This test won't pass until the blur_radius is used to + // expand the coverage. See note inside of + // gaussian_blur_filter_contents.cc. + // EXPECT_TRUE(RectNear(result_coverage.value(), + // contents_coverage.value())); + EXPECT_TRUE( + RectNear(result_coverage.value(), Rect::MakeLTRB(0, 0, 100, 100))); + } + } +} + +TEST_P(GaussianBlurFilterContentsTest, + RenderCoverageMatchesGetCoverageTranslate) { + TextureDescriptor desc = { + .format = PixelFormat::kB8G8R8A8UNormInt, + .size = ISize(100, 100), + }; + std::shared_ptr texture = MakeTexture(desc); + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); + auto contents = std::make_unique(sigma_radius_1); + contents->SetInputs({FilterInput::Make(texture)}); + std::shared_ptr renderer = GetContentContext(); + + Entity entity; + entity.SetTransformation(Matrix::MakeTranslation({100, 200, 0})); + std::optional 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 result_coverage = result.value().GetCoverage(); + std::optional 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()) { + // TODO(gaaclarke): This test won't pass until the blur_radius is used to + // expand the coverage. See note inside of + // gaussian_blur_filter_contents.cc. + // EXPECT_TRUE(RectNear(result_coverage.value(), + // contents_coverage.value())); + EXPECT_TRUE(RectNear(contents_coverage.value(), + Rect::MakeLTRB(99, 199, 201, 301))); + EXPECT_TRUE(RectNear(result_coverage.value(), + Rect::MakeLTRB(100, 200, 200, 300))); + } + } +} + +TEST_P(GaussianBlurFilterContentsTest, + RenderCoverageMatchesGetCoverageRotated) { + TextureDescriptor desc = { + .format = PixelFormat::kB8G8R8A8UNormInt, + .size = ISize(400, 300), + }; + std::shared_ptr texture = MakeTexture(desc); + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); + auto contents = std::make_unique(sigma_radius_1); + contents->SetInputs({FilterInput::Make(texture)}); + std::shared_ptr renderer = GetContentContext(); + + Entity entity; + entity.SetTransformation(Matrix::MakeTranslation({400, 100, 0}) * + Matrix::MakeRotationZ(Degrees(90.0))); + std::optional 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 result_coverage = result.value().GetCoverage(); + std::optional 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()) { + // TODO(gaaclarke): This test won't pass until the blur_radius is used to + // expand the coverage. See note inside of + // gaussian_blur_filter_contents.cc. + // EXPECT_TRUE(RectNear(result_coverage.value(), + // contents_coverage.value())); + EXPECT_TRUE(RectNear(contents_coverage.value(), + Rect::MakeLTRB(99, 99, 401, 501))); + EXPECT_TRUE(RectNear(result_coverage.value(), + Rect::MakeLTRB(100, 100, 400, 500))); + } + } +} + +TEST_P(GaussianBlurFilterContentsTest, CalculateUVsSimple) { + TextureDescriptor desc = { + .format = PixelFormat::kB8G8R8A8UNormInt, + .size = ISize(100, 100), + }; + std::shared_ptr texture = MakeTexture(desc); + auto filter_input = FilterInput::Make(texture); + Entity entity; + Quad uvs = GaussianBlurFilterContents::CalculateUVs(filter_input, entity, + ISize(100, 100)); + std::optional uvs_bounds = Rect::MakePointBounds(uvs); + EXPECT_TRUE(uvs_bounds.has_value()); + if (uvs_bounds.has_value()) { + EXPECT_TRUE(RectNear(uvs_bounds.value(), Rect::MakeXYWH(0, 0, 1, 1))); + } +} + +} // namespace testing +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 20665eb569..b2aaee9ae2 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -1120,14 +1120,18 @@ TEST_P(EntityTest, GaussianBlurFilter) { // Renders a green bounding rect of the target filter. Entity bounds_entity; - bounds_entity.SetContents(SolidColorContents::Make( - PathBuilder{} - .AddRect(target_contents->GetCoverage(entity).value()) - .TakePath(), - bounds_color)); - bounds_entity.SetTransformation(Matrix()); + std::optional target_contents_coverage = + target_contents->GetCoverage(entity); + if (target_contents_coverage.has_value()) { + bounds_entity.SetContents(SolidColorContents::Make( + PathBuilder{} + .AddRect(target_contents->GetCoverage(entity).value()) + .TakePath(), + bounds_color)); + bounds_entity.SetTransformation(Matrix()); - bounds_entity.Render(context, pass); + bounds_entity.Render(context, pass); + } return true; }; diff --git a/engine/src/flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.glsl b/engine/src/flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.glsl index 1144ab085c..ebf135a482 100644 --- a/engine/src/flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.glsl +++ b/engine/src/flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.glsl @@ -29,6 +29,7 @@ uniform BlurInfo { // radius is used to limit how much of the function is integrated. float blur_sigma; float16_t blur_radius; + float16_t step_size; } blur_info; @@ -54,7 +55,7 @@ void main() { // just uses 0.5 as an optimization until the gaussian coefficients are // calculated and passed in from the cpu. for (float16_t i = -blur_info.blur_radius; i <= blur_info.blur_radius; - i += 2.0hf) { + i += blur_info.step_size) { // Use the 32 bit Gaussian function because the 16 bit variation results in // quality loss/visible banding. Also, 16 bit variation internally breaks // down at a moderately high (but still reasonable) blur sigma of >255 when diff --git a/engine/src/flutter/impeller/geometry/geometry_asserts.h b/engine/src/flutter/impeller/geometry/geometry_asserts.h index 9f860e30ba..de38b445fb 100644 --- a/engine/src/flutter/impeller/geometry/geometry_asserts.h +++ b/engine/src/flutter/impeller/geometry/geometry_asserts.h @@ -59,7 +59,8 @@ inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b) { NumberNear(a.GetSize().height, b.GetSize().height); return equal ? ::testing::AssertionSuccess() - : ::testing::AssertionFailure() << "Rects are not equal."; + : ::testing::AssertionFailure() + << "Rects are not equal (" << a << " " << b << ")"; } inline ::testing::AssertionResult ColorNear(impeller::Color a, @@ -76,7 +77,8 @@ inline ::testing::AssertionResult PointNear(impeller::Point a, auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y); return equal ? ::testing::AssertionSuccess() - : ::testing::AssertionFailure() << "Points are not equal."; + : ::testing::AssertionFailure() + << "Points are not equal (" << a << " " << b << ")."; } inline ::testing::AssertionResult Vector3Near(impeller::Vector3 a, diff --git a/engine/src/flutter/impeller/geometry/matrix.h b/engine/src/flutter/impeller/geometry/matrix.h index 53214b2738..2a4e222c17 100644 --- a/engine/src/flutter/impeller/geometry/matrix.h +++ b/engine/src/flutter/impeller/geometry/matrix.h @@ -445,6 +445,15 @@ struct Matrix { return Vector2(v.x * m[0] + v.y * m[4], v.x * m[1] + v.y * m[5]); } + constexpr Quad Transform(const Quad& quad) const { + return { + *this * quad[0], + *this * quad[1], + *this * quad[2], + *this * quad[3], + }; + } + template static constexpr Matrix MakeOrthographic(TSize size) { // Per assumptions about NDC documented above. diff --git a/engine/src/flutter/impeller/geometry/point.h b/engine/src/flutter/impeller/geometry/point.h index ea01002e8a..f8a00c1252 100644 --- a/engine/src/flutter/impeller/geometry/point.h +++ b/engine/src/flutter/impeller/geometry/point.h @@ -309,6 +309,7 @@ using IPoint = TPoint; using IPoint32 = TPoint; using UintPoint32 = TPoint; using Vector2 = Point; +using Quad = std::array; } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/rect.h b/engine/src/flutter/impeller/geometry/rect.h index 4c5fbcefff..60ae26fb60 100644 --- a/engine/src/flutter/impeller/geometry/rect.h +++ b/engine/src/flutter/impeller/geometry/rect.h @@ -48,6 +48,11 @@ struct TRect { return TRect(0.0, 0.0, size.width, size.height); } + template + constexpr static std::optional MakePointBounds(const U& value) { + return MakePointBounds(value.begin(), value.end()); + } + template constexpr static std::optional MakePointBounds(const PointIter first, const PointIter last) { diff --git a/engine/src/flutter/impeller/geometry/rect_unittests.cc b/engine/src/flutter/impeller/geometry/rect_unittests.cc index 53a0c9fea2..1d52802cf9 100644 --- a/engine/src/flutter/impeller/geometry/rect_unittests.cc +++ b/engine/src/flutter/impeller/geometry/rect_unittests.cc @@ -234,5 +234,19 @@ TEST(SizeTest, IRectIsEmpty) { EXPECT_TRUE(IRect::MakeXYWH(1, 2, -1, 7).IsEmpty()); } +TEST(RectTest, MakePointBoundsQuad) { + Quad quad = { + Point(10, 10), + Point(20, 10), + Point(10, 20), + Point(20, 20), + }; + std::optional bounds = Rect::MakePointBounds(quad); + EXPECT_TRUE(bounds.has_value()); + if (bounds.has_value()) { + EXPECT_TRUE(RectNear(bounds.value(), Rect::MakeLTRB(10, 10, 20, 20))); + } +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/tools/malioc.json b/engine/src/flutter/impeller/tools/malioc.json index 1aa9b4e8a8..02dc5c1e59 100644 --- a/engine/src/flutter/impeller/tools/malioc.json +++ b/engine/src/flutter/impeller/tools/malioc.json @@ -3338,7 +3338,7 @@ "arithmetic" ], "shortest_path_cycles": [ - 1.649999976158142, + 1.3200000524520874, 1.0, 0.0 ], @@ -3346,14 +3346,14 @@ "arithmetic" ], "total_cycles": [ - 5.0, + 4.666666507720947, 1.0, 1.0 ] }, "thread_occupancy": 100, "uniform_registers_used": 1, - "work_registers_used": 4 + "work_registers_used": 3 } } } @@ -3456,7 +3456,7 @@ "arithmetic" ], "shortest_path_cycles": [ - 1.649999976158142, + 1.3200000524520874, 1.0, 0.0 ], @@ -3464,7 +3464,7 @@ "arithmetic" ], "total_cycles": [ - 3.6666667461395264, + 3.3333332538604736, 1.0, 1.0 ]