From a8e57675d6b004263cbcce5db6cea3855444ff3d Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Wed, 13 Dec 2023 12:04:47 -0800 Subject: [PATCH] [Impeller] Made the new blur work on devices without the decal address mode (flutter/engine#48899) This also adds a new mechanism of override the device capabilities. fixes https://github.com/flutter/flutter/issues/139773 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../flutter/impeller/aiks/aiks_unittests.cc | 51 +++++++++++++++++++ .../filters/gaussian_blur_filter_contents.cc | 45 +++++++++------- .../golden_tests/golden_playground_test.h | 7 +++ .../golden_playground_test_mac.cc | 5 ++ .../golden_playground_test_stub.cc | 7 +++ .../golden_tests/metal_screenshotter.h | 2 +- .../backend/gles/playground_impl_gles.cc | 7 +++ .../backend/gles/playground_impl_gles.h | 3 ++ .../backend/metal/playground_impl_mtl.h | 8 ++- .../backend/metal/playground_impl_mtl.mm | 6 +++ .../backend/vulkan/playground_impl_vk.cc | 7 +++ .../backend/vulkan/playground_impl_vk.h | 3 ++ .../flutter/impeller/playground/playground.cc | 9 ++++ .../flutter/impeller/playground/playground.h | 8 +++ .../impeller/playground/playground_impl.h | 3 ++ .../renderer/backend/metal/context_mtl.h | 2 + .../renderer/backend/metal/context_mtl.mm | 5 ++ 17 files changed, 159 insertions(+), 19 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 3fdfec01ae..c1c46ecde9 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -35,6 +35,7 @@ #include "impeller/playground/widgets.h" #include "impeller/renderer/command_buffer.h" #include "impeller/renderer/snapshot.h" +#include "impeller/renderer/testing/mocks.h" #include "impeller/scene/material.h" #include "impeller/scene/node.h" #include "impeller/typographer/backends/skia/text_frame_skia.h" @@ -4504,5 +4505,55 @@ TEST_P(AiksTest, GaussianBlurAtPeripheryHorizontal) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +#define FLT_FORWARD(mock, real, method) \ + EXPECT_CALL(*mock, method()) \ + .WillRepeatedly(::testing::Return(real->method())); + +TEST_P(AiksTest, GaussianBlurWithoutDecalSupport) { + if (GetParam() != PlaygroundBackend::kMetal) { + GTEST_SKIP_( + "This backend doesn't yet support setting device capabilities."); + } + if (!WillRenderSomething()) { + // Sometimes these tests are run without playgrounds enabled which is + // pointless for this test since we are asserting that + // `SupportsDecalSamplerAddressMode` is called. + GTEST_SKIP_("This test requires playgrounds."); + } + + std::shared_ptr old_capabilities = + GetContext()->GetCapabilities(); + auto mock_capabilities = std::make_shared(); + EXPECT_CALL(*mock_capabilities, SupportsDecalSamplerAddressMode()) + .Times(::testing::AtLeast(1)) + .WillRepeatedly(::testing::Return(false)); + FLT_FORWARD(mock_capabilities, old_capabilities, GetDefaultColorFormat); + FLT_FORWARD(mock_capabilities, old_capabilities, GetDefaultStencilFormat); + FLT_FORWARD(mock_capabilities, old_capabilities, SupportsOffscreenMSAA); + FLT_FORWARD(mock_capabilities, old_capabilities, + SupportsImplicitResolvingMSAA); + FLT_FORWARD(mock_capabilities, old_capabilities, SupportsReadFromResolve); + FLT_FORWARD(mock_capabilities, old_capabilities, SupportsFramebufferFetch); + FLT_FORWARD(mock_capabilities, old_capabilities, SupportsSSBO); + FLT_FORWARD(mock_capabilities, old_capabilities, SupportsCompute); + FLT_FORWARD(mock_capabilities, old_capabilities, + SupportsTextureToTextureBlits); + ASSERT_TRUE(SetCapabilities(mock_capabilities).ok()); + + auto texture = std::make_shared(CreateTextureForFixture("boston.jpg")); + Canvas canvas; + canvas.Scale(GetContentScale() * 0.5); + canvas.DrawPaint({.color = Color::Black()}); + canvas.DrawImage( + texture, Point(200, 200), + { + .image_filter = ImageFilter::MakeBlur( + Sigma(20.0), Sigma(20.0), FilterContents::BlurStyle::kNormal, + Entity::TileMode::kDecal), + }); + + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + } // namespace testing } // namespace impeller 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 index 2df5f70249..857184c8b1 100644 --- 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 @@ -146,6 +146,7 @@ std::shared_ptr MakeBlurSubpass( const ContentContext& renderer, std::shared_ptr input_texture, const SamplerDescriptor& sampler_descriptor, + Entity::TileMode tile_mode, const GaussianBlurFragmentShader::BlurInfo& blur_info) { // TODO(gaaclarke): This blurs the whole image, but because we know the clip // region we could focus on just blurring that. @@ -161,7 +162,15 @@ std::shared_ptr MakeBlurSubpass( Command cmd; ContentContextOptions options = OptionsFromPass(pass); options.primitive_type = PrimitiveType::kTriangleStrip; - cmd.pipeline = renderer.GetGaussianBlurPipeline(options); + + if (tile_mode == Entity::TileMode::kDecal && + !renderer.GetDeviceCapabilities() + .SupportsDecalSamplerAddressMode()) { + cmd.pipeline = renderer.GetGaussianBlurDecalPipeline(options); + } else { + cmd.pipeline = renderer.GetGaussianBlurPipeline(options); + } + BindVertices(cmd, host_buffer, { {Point(0, 0), Point(0, 0)}, @@ -300,24 +309,26 @@ std::optional GaussianBlurFilterContents::RenderFilter( Vector2 pass1_pixel_size = 1.0 / Vector2(pass1_out_texture->GetSize()); - 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.y), - .blur_sigma = scaled_sigma * effective_scalar.y, - .blur_radius = blur_radius * effective_scalar.y, - .step_size = 1.0, - }); + std::shared_ptr pass2_out_texture = + MakeBlurSubpass(renderer, pass1_out_texture, + input_snapshot->sampler_descriptor, tile_mode_, + GaussianBlurFragmentShader::BlurInfo{ + .blur_uv_offset = Point(0.0, pass1_pixel_size.y), + .blur_sigma = scaled_sigma * effective_scalar.y, + .blur_radius = blur_radius * effective_scalar.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.x, 0.0), - .blur_sigma = scaled_sigma * effective_scalar.x, - .blur_radius = blur_radius * effective_scalar.x, - .step_size = 1.0, - }); + std::shared_ptr pass3_out_texture = + MakeBlurSubpass(renderer, pass2_out_texture, + input_snapshot->sampler_descriptor, tile_mode_, + GaussianBlurFragmentShader::BlurInfo{ + .blur_uv_offset = Point(pass1_pixel_size.x, 0.0), + .blur_sigma = scaled_sigma * effective_scalar.x, + .blur_radius = blur_radius * effective_scalar.x, + .step_size = 1.0, + }); SamplerDescriptor sampler_desc = MakeSamplerDescriptor( MinMagFilter::kLinear, SamplerAddressMode::kClampToEdge); diff --git a/engine/src/flutter/impeller/golden_tests/golden_playground_test.h b/engine/src/flutter/impeller/golden_tests/golden_playground_test.h index d28ee1f3ae..213c4d442c 100644 --- a/engine/src/flutter/impeller/golden_tests/golden_playground_test.h +++ b/engine/src/flutter/impeller/golden_tests/golden_playground_test.h @@ -58,6 +58,13 @@ class GoldenPlaygroundTest ISize GetWindowSize() const; + [[nodiscard]] fml::Status SetCapabilities( + const std::shared_ptr& capabilities); + + /// TODO(https://github.com/flutter/flutter/issues/139950): Remove this. + /// Returns true if `OpenPlaygroundHere` will actually render anything. + bool WillRenderSomething() const { return true; } + protected: void SetWindowSize(ISize size); diff --git a/engine/src/flutter/impeller/golden_tests/golden_playground_test_mac.cc b/engine/src/flutter/impeller/golden_tests/golden_playground_test_mac.cc index 686b195698..befe93b0a4 100644 --- a/engine/src/flutter/impeller/golden_tests/golden_playground_test_mac.cc +++ b/engine/src/flutter/impeller/golden_tests/golden_playground_test_mac.cc @@ -198,4 +198,9 @@ void GoldenPlaygroundTest::GoldenPlaygroundTest::SetWindowSize(ISize size) { pimpl_->window_size = size; } +fml::Status GoldenPlaygroundTest::SetCapabilities( + const std::shared_ptr& capabilities) { + return pimpl_->screenshotter->GetPlayground().SetCapabilities(capabilities); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/golden_tests/golden_playground_test_stub.cc b/engine/src/flutter/impeller/golden_tests/golden_playground_test_stub.cc index 7827d9c5e1..fc141eb70a 100644 --- a/engine/src/flutter/impeller/golden_tests/golden_playground_test_stub.cc +++ b/engine/src/flutter/impeller/golden_tests/golden_playground_test_stub.cc @@ -66,4 +66,11 @@ ISize GoldenPlaygroundTest::GetWindowSize() const { void GoldenPlaygroundTest::SetWindowSize(ISize size) {} +fml::Status GoldenPlaygroundTest::SetCapabilities( + const std::shared_ptr& capabilities) { + return fml::Status( + fml::StatusCode::kUnimplemented, + "GoldenPlaygroundTest-Stub doesn't support SetCapabilities."); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/golden_tests/metal_screenshotter.h b/engine/src/flutter/impeller/golden_tests/metal_screenshotter.h index 292053dd66..4d994cb27f 100644 --- a/engine/src/flutter/impeller/golden_tests/metal_screenshotter.h +++ b/engine/src/flutter/impeller/golden_tests/metal_screenshotter.h @@ -25,7 +25,7 @@ class MetalScreenshotter { 300}, bool scale_content = true); - const PlaygroundImpl& GetPlayground() const { return *playground_; } + PlaygroundImpl& GetPlayground() { return *playground_; } private: std::unique_ptr playground_; diff --git a/engine/src/flutter/impeller/playground/backend/gles/playground_impl_gles.cc b/engine/src/flutter/impeller/playground/backend/gles/playground_impl_gles.cc index ec09cc6d80..84fc103c7f 100644 --- a/engine/src/flutter/impeller/playground/backend/gles/playground_impl_gles.cc +++ b/engine/src/flutter/impeller/playground/backend/gles/playground_impl_gles.cc @@ -156,4 +156,11 @@ std::unique_ptr PlaygroundImplGLES::AcquireSurfaceFrame( ); } +fml::Status PlaygroundImplGLES::SetCapabilities( + const std::shared_ptr& capabilities) { + return fml::Status( + fml::StatusCode::kUnimplemented, + "PlaygroundImplGLES doesn't support setting the capabilities."); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/playground/backend/gles/playground_impl_gles.h b/engine/src/flutter/impeller/playground/backend/gles/playground_impl_gles.h index d1e328230c..4bcf8fd69e 100644 --- a/engine/src/flutter/impeller/playground/backend/gles/playground_impl_gles.h +++ b/engine/src/flutter/impeller/playground/backend/gles/playground_impl_gles.h @@ -16,6 +16,9 @@ class PlaygroundImplGLES final : public PlaygroundImpl { ~PlaygroundImplGLES(); + fml::Status SetCapabilities( + const std::shared_ptr& capabilities) override; + private: class ReactorWorker; diff --git a/engine/src/flutter/impeller/playground/backend/metal/playground_impl_mtl.h b/engine/src/flutter/impeller/playground/backend/metal/playground_impl_mtl.h index 937db7a753..9d8698d285 100644 --- a/engine/src/flutter/impeller/playground/backend/metal/playground_impl_mtl.h +++ b/engine/src/flutter/impeller/playground/backend/metal/playground_impl_mtl.h @@ -14,12 +14,18 @@ namespace impeller { +// Forward declared to avoid objc in a C++ header. +class ContextMTL; + class PlaygroundImplMTL final : public PlaygroundImpl { public: explicit PlaygroundImplMTL(PlaygroundSwitches switches); ~PlaygroundImplMTL(); + fml::Status SetCapabilities( + const std::shared_ptr& capabilities) override; + private: struct Data; @@ -29,7 +35,7 @@ class PlaygroundImplMTL final : public PlaygroundImpl { // To ensure that ObjC stuff doesn't leak into C++ TUs. std::unique_ptr data_; - std::shared_ptr context_; + std::shared_ptr context_; std::shared_ptr concurrent_loop_; std::shared_ptr is_gpu_disabled_sync_switch_; diff --git a/engine/src/flutter/impeller/playground/backend/metal/playground_impl_mtl.mm b/engine/src/flutter/impeller/playground/backend/metal/playground_impl_mtl.mm index e9ffbf64f6..421d6eba3c 100644 --- a/engine/src/flutter/impeller/playground/backend/metal/playground_impl_mtl.mm +++ b/engine/src/flutter/impeller/playground/backend/metal/playground_impl_mtl.mm @@ -123,4 +123,10 @@ std::unique_ptr PlaygroundImplMTL::AcquireSurfaceFrame( return SurfaceMTL::MakeFromMetalLayerDrawable(context, drawable); } +fml::Status PlaygroundImplMTL::SetCapabilities( + const std::shared_ptr& capabilities) { + context_->SetCapabilities(capabilities); + return fml::Status(); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc index e74541fcfd..d05004f4eb 100644 --- a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc +++ b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc @@ -182,4 +182,11 @@ void PlaygroundImplVK::InitGlobalVulkanInstance() { global_instance_ = std::move(instance_result.value); } +fml::Status PlaygroundImplVK::SetCapabilities( + const std::shared_ptr& capabilities) { + return fml::Status( + fml::StatusCode::kUnimplemented, + "PlaygroundImplVK doesn't support setting the capabilities."); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.h b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.h index eed479b859..78edc7f06d 100644 --- a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.h +++ b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.h @@ -17,6 +17,9 @@ class PlaygroundImplVK final : public PlaygroundImpl { ~PlaygroundImplVK(); + fml::Status SetCapabilities( + const std::shared_ptr& capabilities) override; + private: std::shared_ptr context_; diff --git a/engine/src/flutter/impeller/playground/playground.cc b/engine/src/flutter/impeller/playground/playground.cc index 8d2f591a56..1c6dbfa694 100644 --- a/engine/src/flutter/impeller/playground/playground.cc +++ b/engine/src/flutter/impeller/playground/playground.cc @@ -532,4 +532,13 @@ bool Playground::ShouldKeepRendering() const { return true; } +fml::Status Playground::SetCapabilities( + const std::shared_ptr& capabilities) { + return impl_->SetCapabilities(capabilities); +} + +bool Playground::WillRenderSomething() const { + return switches_.enable_playground; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/playground/playground.h b/engine/src/flutter/impeller/playground/playground.h index c9de5cc9cc..57e6b882eb 100644 --- a/engine/src/flutter/impeller/playground/playground.h +++ b/engine/src/flutter/impeller/playground/playground.h @@ -10,6 +10,7 @@ #include "flutter/fml/closure.h" #include "flutter/fml/macros.h" +#include "flutter/fml/status.h" #include "flutter/fml/time/time_delta.h" #include "impeller/core/texture.h" #include "impeller/geometry/point.h" @@ -88,6 +89,13 @@ class Playground { virtual std::string GetWindowTitle() const = 0; + [[nodiscard]] fml::Status SetCapabilities( + const std::shared_ptr& capabilities); + + /// TODO(https://github.com/flutter/flutter/issues/139950): Remove this. + /// Returns true if `OpenPlaygroundHere` will actually render anything. + bool WillRenderSomething() const; + protected: const PlaygroundSwitches switches_; diff --git a/engine/src/flutter/impeller/playground/playground_impl.h b/engine/src/flutter/impeller/playground/playground_impl.h index 499859f625..68598d3985 100644 --- a/engine/src/flutter/impeller/playground/playground_impl.h +++ b/engine/src/flutter/impeller/playground/playground_impl.h @@ -33,6 +33,9 @@ class PlaygroundImpl { Vector2 GetContentScale() const; + virtual fml::Status SetCapabilities( + const std::shared_ptr& capabilities) = 0; + protected: const PlaygroundSwitches switches_; diff --git a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h index a3ce8d7fa3..d7561f52c3 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h @@ -83,6 +83,8 @@ class ContextMTL final : public Context, // |Context| const std::shared_ptr& GetCapabilities() const override; + void SetCapabilities(const std::shared_ptr& capabilities); + // |Context| bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override; diff --git a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm index 3c0055e6b9..90054aa21d 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm @@ -376,6 +376,11 @@ const std::shared_ptr& ContextMTL::GetCapabilities() const { return device_capabilities_; } +void ContextMTL::SetCapabilities( + const std::shared_ptr& capabilities) { + device_capabilities_ = capabilities; +} + // |Context| bool ContextMTL::UpdateOffscreenLayerPixelFormat(PixelFormat format) { device_capabilities_ = InferMetalCapabilities(device_, format);