From e98902235d1a04a4d20bc2dc9f7fde470711ebd1 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:04:12 -0800 Subject: [PATCH] Reland: [Impeller] Turned on new blur. (#48472) (flutter/engine#49642) This is a reland of the new gaussian blur. Changes since revert: 1) Textures are now recycled with ping ponging to reduce memory usage 1) Mipmaps are generated to diminish the shimmering that happens with animated blurs (in metal) --- This new blur should perform faster since it scales down the image before blurring it. Jonah did early testing of it and found it to be faster. Scrolling around with the blur perf bug it seems faster. It also has a wider test bed and is hopefully easier to maintain since it contains all of its logic for both directions. testing: There are existing blur tests and we've backfilled more as we've added features to this blur. fixes https://github.com/flutter/flutter/issues/131580 fixes https://github.com/flutter/flutter/issues/138259 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/ https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../flutter/impeller/aiks/aiks_unittests.cc | 34 ++++++++++++---- ...irectional_gaussian_blur_filter_contents.h | 3 ++ .../contents/filters/filter_contents.cc | 40 ++----------------- .../flutter/impeller/entity/entity_pass.cc | 8 ++++ 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 03cf5495b6..6d92b8585a 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -3758,10 +3758,13 @@ TEST_P(AiksTest, GaussianBlurSetsMipCountOnPass) { canvas.Restore(); Picture picture = canvas.EndRecordingAsPicture(); - EXPECT_EQ(1, picture.pass->GetRequiredMipCount()); + EXPECT_EQ(4, picture.pass->GetRequiredMipCount()); } TEST_P(AiksTest, GaussianBlurAllocatesCorrectMipCountRenderTarget) { + size_t blur_required_mip_count = + GetParam() == PlaygroundBackend::kMetal ? 4 : 1; + Canvas canvas; canvas.DrawCircle({100, 100}, 50, {.color = Color::CornflowerBlue()}); canvas.SaveLayer({}, std::nullopt, @@ -3782,11 +3785,14 @@ TEST_P(AiksTest, GaussianBlurAllocatesCorrectMipCountRenderTarget) { max_mip_count = std::max(it->texture->GetTextureDescriptor().mip_count, max_mip_count); } - EXPECT_EQ(max_mip_count, 1lu); + EXPECT_EQ(max_mip_count, blur_required_mip_count); } TEST_P(AiksTest, GaussianBlurMipMapNestedLayer) { fml::testing::LogCapture log_capture; + size_t blur_required_mip_count = + GetParam() == PlaygroundBackend::kMetal ? 4 : 1; + Canvas canvas; canvas.DrawPaint({.color = Color::Wheat()}); canvas.SaveLayer({.blend_mode = BlendMode::kMultiply}); @@ -3809,12 +3815,19 @@ TEST_P(AiksTest, GaussianBlurMipMapNestedLayer) { max_mip_count = std::max(it->texture->GetTextureDescriptor().mip_count, max_mip_count); } - EXPECT_EQ(max_mip_count, 1lu); - EXPECT_EQ(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError), - std::string::npos); + EXPECT_EQ(max_mip_count, blur_required_mip_count); + if (GetParam() == PlaygroundBackend::kMetal) { + EXPECT_EQ(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError), + std::string::npos); + } else { + EXPECT_NE(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError), + std::string::npos); + } } TEST_P(AiksTest, GaussianBlurMipMapImageFilter) { + size_t blur_required_mip_count = + GetParam() == PlaygroundBackend::kMetal ? 4 : 1; fml::testing::LogCapture log_capture; Canvas canvas; canvas.SaveLayer( @@ -3835,9 +3848,14 @@ TEST_P(AiksTest, GaussianBlurMipMapImageFilter) { max_mip_count = std::max(it->texture->GetTextureDescriptor().mip_count, max_mip_count); } - EXPECT_EQ(max_mip_count, 1lu); - EXPECT_EQ(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError), - std::string::npos); + EXPECT_EQ(max_mip_count, blur_required_mip_count); + if (GetParam() == PlaygroundBackend::kMetal) { + EXPECT_EQ(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError), + std::string::npos); + } else { + EXPECT_NE(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError), + std::string::npos); + } } } // namespace testing diff --git a/engine/src/flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h index 2ea5df044f..548d1d409c 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h @@ -43,6 +43,9 @@ namespace impeller { /// - `FilterContents::MakeGaussianBlur` /// - //flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.glsl /// +///\deprecated Previously 2 of these were chained to do 2D blurs, use +/// \ref GaussianBlurFilterContents instead since it has better +/// performance. class DirectionalGaussianBlurFilterContents final : public FilterContents { public: DirectionalGaussianBlurFilterContents(); 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 235a53134c..6ed61b0235 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -50,11 +50,7 @@ std::shared_ptr FilterContents::MakeDirectionalGaussianBlur( return blur; } -#ifdef IMPELLER_ENABLE_NEW_GAUSSIAN_FILTER const int32_t FilterContents::kBlurFilterRequiredMipCount = 4; -#else -const int32_t FilterContents::kBlurFilterRequiredMipCount = 1; -#endif std::shared_ptr FilterContents::MakeGaussianBlur( const FilterInput::Ref& input, @@ -62,38 +58,10 @@ 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, sigma_y.sigma, tile_mode); - blur->SetInputs({input}); - return blur; - } - std::shared_ptr x_blur = MakeDirectionalGaussianBlur( - /*input=*/input, - /*sigma=*/sigma_x, - /*direction=*/Point(1, 0), - /*blur_style=*/BlurStyle::kNormal, - /*tile_mode=*/tile_mode, - /*is_second_pass=*/false, - /*secondary_sigma=*/{}); - std::shared_ptr y_blur = MakeDirectionalGaussianBlur( - /*input=*/FilterInput::Make(x_blur), - /*sigma=*/sigma_y, - /*direction=*/Point(0, 1), - /*blur_style=*/blur_style, - /*tile_mode=*/tile_mode, - /*is_second_pass=*/true, - /*secondary_sigma=*/sigma_x); - return y_blur; + auto blur = std::make_shared( + sigma_x.sigma, sigma_y.sigma, tile_mode); + blur->SetInputs({input}); + return blur; } std::shared_ptr FilterContents::MakeBorderMaskBlur( diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index 9323692ffa..bff88bb587 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -256,6 +256,14 @@ static EntityPassTarget CreateRenderTarget(ContentContext& renderer, /// What's important is the `StorageMode` of the textures, which cannot be /// changed for the lifetime of the textures. + if (context->GetBackendType() != Context::BackendType::kMetal) { + // TODO(https://github.com/flutter/flutter/issues/141495): Implement mip map + // generation on vulkan. + // TODO(https://github.com/flutter/flutter/issues/141732): Implement mip map + // generation on opengles. + mip_count = 1; + } + RenderTarget target; if (context->GetCapabilities()->SupportsOffscreenMSAA()) { target = RenderTarget::CreateOffscreenMSAA(