diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index fd7804c0fa..021b6891b4 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -3213,7 +3213,7 @@ TEST_P(AiksTest, CaptureInactivatedByDefault) { // Regression test for https://github.com/flutter/flutter/issues/134678. TEST_P(AiksTest, ReleasesTextureOnTeardown) { - auto context = GetContext(); + auto context = MakeContext(); std::weak_ptr weak_texture; { diff --git a/engine/src/flutter/impeller/golden_tests/BUILD.gn b/engine/src/flutter/impeller/golden_tests/BUILD.gn index d17a59e03f..e26a1b614c 100644 --- a/engine/src/flutter/impeller/golden_tests/BUILD.gn +++ b/engine/src/flutter/impeller/golden_tests/BUILD.gn @@ -21,7 +21,10 @@ impeller_component("golden_playground_test") { if (is_mac) { sources += [ "golden_playground_test_mac.cc" ] - deps += [ ":metal_screenshot" ] + deps += [ + ":metal_screenshot", + "//third_party/abseil-cpp/absl/base:no_destructor", + ] } else { sources += [ "golden_playground_test_stub.cc" ] } 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 f411615b51..251e98a04e 100644 --- a/engine/src/flutter/impeller/golden_tests/golden_playground_test.h +++ b/engine/src/flutter/impeller/golden_tests/golden_playground_test.h @@ -51,6 +51,8 @@ class GoldenPlaygroundTest std::shared_ptr GetContext() const; + std::shared_ptr MakeContext() const; + Point GetContentScale() const; Scalar GetSecondsElapsed() const; 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 2077e7a2ba..c9b2b08673 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 @@ -15,8 +15,38 @@ #include "impeller/typographer/backends/skia/typographer_context_skia.h" #include "impeller/typographer/typographer_context.h" +#define GLFW_INCLUDE_NONE +#include "third_party/glfw/include/GLFW/glfw3.h" + +#include "third_party/abseil-cpp/absl/base/no_destructor.h" + namespace impeller { +namespace { +std::unique_ptr MakeVulkanPlayground(bool enable_validations) { + FML_CHECK(::glfwInit() == GLFW_TRUE); + PlaygroundSwitches playground_switches; + playground_switches.enable_vulkan_validation = enable_validations; + return PlaygroundImpl::Create(PlaygroundBackend::kVulkan, + playground_switches); +} + +// Returns a static instance to a playground that can be used across tests. +const std::unique_ptr& GetSharedVulkanPlayground( + bool enable_validations) { + if (enable_validations) { + static absl::NoDestructor> + vulkan_validation_playground( + MakeVulkanPlayground(/*enable_validations=*/true)); + return *vulkan_validation_playground; + } else { + static absl::NoDestructor> + vulkan_playground(MakeVulkanPlayground(/*enable_validations=*/false)); + return *vulkan_playground; + } +} +} // namespace + // If you add a new playground test to the aiks unittests and you do not want it // to also be a golden test, then add the test name here. static const std::vector kSkipTests = { @@ -96,9 +126,16 @@ bool SaveScreenshot(std::unique_ptr screenshot) { return screenshot->WriteToPNG( testing::WorkingDirectory::Instance()->GetFilenamePath(filename)); } + +bool ShouldTestHaveVulkanValidations() { + std::string test_name = GetTestName(); + return std::find(kVulkanValidationTests.begin(), kVulkanValidationTests.end(), + test_name) != kVulkanValidationTests.end(); +} } // namespace struct GoldenPlaygroundTest::GoldenPlaygroundTestImpl { + std::unique_ptr test_vulkan_playground; std::unique_ptr screenshotter; ISize window_size = ISize{1024, 768}; }; @@ -134,20 +171,17 @@ void GoldenPlaygroundTest::SetUp() { return; } - bool enable_vulkan_validations = false; - std::string test_name = GetTestName(); - if (std::find(kVulkanValidationTests.begin(), kVulkanValidationTests.end(), - test_name) != kVulkanValidationTests.end()) { - enable_vulkan_validations = true; - } - + bool enable_vulkan_validations = ShouldTestHaveVulkanValidations(); if (GetParam() == PlaygroundBackend::kMetal) { pimpl_->screenshotter = std::make_unique(); } else if (GetParam() == PlaygroundBackend::kVulkan) { - pimpl_->screenshotter = std::make_unique( - enable_vulkan_validations); + const std::unique_ptr& playground = + GetSharedVulkanPlayground(enable_vulkan_validations); + pimpl_->screenshotter = + std::make_unique(playground); } + std::string test_name = GetTestName(); if (std::find(kSkipTests.begin(), kSkipTests.end(), test_name) != kSkipTests.end()) { GTEST_SKIP_( @@ -217,6 +251,25 @@ std::shared_ptr GoldenPlaygroundTest::GetContext() const { return pimpl_->screenshotter->GetPlayground().GetContext(); } +std::shared_ptr GoldenPlaygroundTest::MakeContext() const { + if (GetParam() == PlaygroundBackend::kMetal) { + /// On Metal we create a context for each test. + return GetContext(); + } else if (GetParam() == PlaygroundBackend::kVulkan) { + bool enable_vulkan_validations = ShouldTestHaveVulkanValidations(); + FML_CHECK(!pimpl_->test_vulkan_playground) + << "We don't support creating multiple contexts for one test"; + pimpl_->test_vulkan_playground = + MakeVulkanPlayground(enable_vulkan_validations); + pimpl_->screenshotter = std::make_unique( + pimpl_->test_vulkan_playground); + return pimpl_->test_vulkan_playground->GetContext(); + } else { + FML_CHECK(false); + return nullptr; + } +} + Point GoldenPlaygroundTest::GetContentScale() const { return pimpl_->screenshotter->GetPlayground().GetContentScale(); } diff --git a/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.h b/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.h index b48ad61df9..83a0959975 100644 --- a/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.h +++ b/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.h @@ -18,7 +18,8 @@ namespace testing { /// playground backend. class VulkanScreenshotter : public Screenshotter { public: - explicit VulkanScreenshotter(bool enable_validations); + explicit VulkanScreenshotter( + const std::unique_ptr& playground); std::unique_ptr MakeScreenshot( AiksContext& aiks_context, @@ -29,7 +30,7 @@ class VulkanScreenshotter : public Screenshotter { PlaygroundImpl& GetPlayground() override { return *playground_; } private: - std::unique_ptr playground_; + const std::unique_ptr& playground_; }; } // namespace testing diff --git a/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.mm b/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.mm index 1daaee4cd8..afa3a0262a 100644 --- a/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.mm +++ b/engine/src/flutter/impeller/golden_tests/vulkan_screenshotter.mm @@ -64,12 +64,10 @@ std::unique_ptr ReadTexture( } } // namespace -VulkanScreenshotter::VulkanScreenshotter(bool enable_validations) { - FML_CHECK(::glfwInit() == GLFW_TRUE); - PlaygroundSwitches playground_switches; - playground_switches.enable_vulkan_validation = enable_validations; - playground_ = - PlaygroundImpl::Create(PlaygroundBackend::kVulkan, playground_switches); +VulkanScreenshotter::VulkanScreenshotter( + const std::unique_ptr& playground) + : playground_(playground) { + FML_CHECK(playground_); } std::unique_ptr VulkanScreenshotter::MakeScreenshot( diff --git a/engine/src/flutter/impeller/playground/playground.cc b/engine/src/flutter/impeller/playground/playground.cc index d018885377..df44559af8 100644 --- a/engine/src/flutter/impeller/playground/playground.cc +++ b/engine/src/flutter/impeller/playground/playground.cc @@ -87,6 +87,12 @@ std::shared_ptr Playground::GetContext() const { return context_; } +std::shared_ptr Playground::MakeContext() const { + // Playgrounds are already making a context for each test, so we can just + // return the `context_`. + return context_; +} + bool Playground::SupportsBackend(PlaygroundBackend backend) { switch (backend) { case PlaygroundBackend::kMetal: diff --git a/engine/src/flutter/impeller/playground/playground.h b/engine/src/flutter/impeller/playground/playground.h index e1dcb8af24..4292ddf9fb 100644 --- a/engine/src/flutter/impeller/playground/playground.h +++ b/engine/src/flutter/impeller/playground/playground.h @@ -75,6 +75,8 @@ class Playground { std::shared_ptr GetContext() const; + std::shared_ptr MakeContext() const; + bool OpenPlaygroundHere(const Renderer::RenderCallback& render_callback); bool OpenPlaygroundHere(SinglePassCallback pass_callback);