[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] Share vulkan playground across goldens #49981

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

{
Expand Down
5 changes: 4 additions & 1 deletion impeller/golden_tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
}
Expand Down
2 changes: 2 additions & 0 deletions impeller/golden_tests/golden_playground_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class GoldenPlaygroundTest

std::shared_ptr<Context> GetContext() const;

std::shared_ptr<Context> MakeContext() const;

Point GetContentScale() const;

Scalar GetSecondsElapsed() const;
Expand Down
71 changes: 62 additions & 9 deletions impeller/golden_tests/golden_playground_test_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -95,9 +125,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};
};
Expand Down Expand Up @@ -133,20 +170,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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this into the if GetParam() == kVulkan block

With the other changes below, the Vulkan block can be reduced to something like:

PlaygroundImpl& playground = ShouldTestHaveVulkanValidations()
    ? GoldenPlaygroundTestImpl::GetVulkanValidationPlayground()
    : GoldenPlaygroundTestImpl::GetVulkanPlayground();

pimpl_->screenshotter = std::make_unique<testing::VulkanScreenshotter>(playground);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly looks like this now, the branch is in the GetSharedVulkanPlayground function though.

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_(
Expand Down Expand Up @@ -216,6 +250,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();
}
Expand Down
5 changes: 3 additions & 2 deletions impeller/golden_tests/vulkan_screenshotter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hold this as a PlaygroundImpl& instead of a reference to a unique_ptr

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to do that because that would require converting from a pointer to a reference which is removing validation for null pointers.

};

} // namespace testing
Expand Down
10 changes: 4 additions & 6 deletions impeller/golden_tests/vulkan_screenshotter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@
}
} // 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(
Expand Down
6 changes: 6 additions & 0 deletions impeller/playground/playground.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions impeller/playground/playground.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down