[Impeller] Share vulkan playground across goldens (flutter/engine#49981)
fixes https://github.com/flutter/flutter/issues/142052 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -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<Texture> weak_texture;
|
||||
|
||||
{
|
||||
|
||||
@@ -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" ]
|
||||
}
|
||||
|
||||
@@ -51,6 +51,8 @@ class GoldenPlaygroundTest
|
||||
|
||||
std::shared_ptr<Context> GetContext() const;
|
||||
|
||||
std::shared_ptr<Context> MakeContext() const;
|
||||
|
||||
Point GetContentScale() const;
|
||||
|
||||
Scalar GetSecondsElapsed() const;
|
||||
|
||||
@@ -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<PlaygroundImpl> 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<PlaygroundImpl>& GetSharedVulkanPlayground(
|
||||
bool enable_validations) {
|
||||
if (enable_validations) {
|
||||
static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
|
||||
vulkan_validation_playground(
|
||||
MakeVulkanPlayground(/*enable_validations=*/true));
|
||||
return *vulkan_validation_playground;
|
||||
} else {
|
||||
static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
|
||||
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<std::string> kSkipTests = {
|
||||
@@ -96,9 +126,16 @@ bool SaveScreenshot(std::unique_ptr<testing::Screenshot> 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<PlaygroundImpl> test_vulkan_playground;
|
||||
std::unique_ptr<testing::Screenshotter> 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<testing::MetalScreenshotter>();
|
||||
} else if (GetParam() == PlaygroundBackend::kVulkan) {
|
||||
pimpl_->screenshotter = std::make_unique<testing::VulkanScreenshotter>(
|
||||
enable_vulkan_validations);
|
||||
const std::unique_ptr<PlaygroundImpl>& playground =
|
||||
GetSharedVulkanPlayground(enable_vulkan_validations);
|
||||
pimpl_->screenshotter =
|
||||
std::make_unique<testing::VulkanScreenshotter>(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<Context> GoldenPlaygroundTest::GetContext() const {
|
||||
return pimpl_->screenshotter->GetPlayground().GetContext();
|
||||
}
|
||||
|
||||
std::shared_ptr<Context> 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<testing::VulkanScreenshotter>(
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@ namespace testing {
|
||||
/// playground backend.
|
||||
class VulkanScreenshotter : public Screenshotter {
|
||||
public:
|
||||
explicit VulkanScreenshotter(bool enable_validations);
|
||||
explicit VulkanScreenshotter(
|
||||
const std::unique_ptr<PlaygroundImpl>& playground);
|
||||
|
||||
std::unique_ptr<Screenshot> MakeScreenshot(
|
||||
AiksContext& aiks_context,
|
||||
@@ -29,7 +30,7 @@ class VulkanScreenshotter : public Screenshotter {
|
||||
PlaygroundImpl& GetPlayground() override { return *playground_; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<PlaygroundImpl> playground_;
|
||||
const std::unique_ptr<PlaygroundImpl>& playground_;
|
||||
};
|
||||
|
||||
} // namespace testing
|
||||
|
||||
@@ -64,12 +64,10 @@ std::unique_ptr<Screenshot> 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<PlaygroundImpl>& playground)
|
||||
: playground_(playground) {
|
||||
FML_CHECK(playground_);
|
||||
}
|
||||
|
||||
std::unique_ptr<Screenshot> VulkanScreenshotter::MakeScreenshot(
|
||||
|
||||
@@ -87,6 +87,12 @@ std::shared_ptr<Context> Playground::GetContext() const {
|
||||
return context_;
|
||||
}
|
||||
|
||||
std::shared_ptr<Context> 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:
|
||||
|
||||
@@ -75,6 +75,8 @@ class Playground {
|
||||
|
||||
std::shared_ptr<Context> GetContext() const;
|
||||
|
||||
std::shared_ptr<Context> MakeContext() const;
|
||||
|
||||
bool OpenPlaygroundHere(const Renderer::RenderCallback& render_callback);
|
||||
|
||||
bool OpenPlaygroundHere(SinglePassCallback pass_callback);
|
||||
|
||||
Reference in New Issue
Block a user