[Impeller] Allow playgrounds to use SwiftShader via a flag. (flutter/engine#50298)

Specifying the `--use_swiftshader` flag will find an setup the SwiftShader ICD in the process. Allows switching between two Vulkan implementation (system and SwiftShader) quickly using a flag.

Following up on https://github.com/flutter/flutter/issues/127070
This commit is contained in:
Chinmay Garde
2024-02-02 14:07:11 -08:00
committed by GitHub
parent 8a98a0be33
commit 0c03fdf90c
7 changed files with 134 additions and 33 deletions

View File

@@ -15,6 +15,10 @@ impeller_component("playground") {
"switches.h",
"widgets.cc",
"widgets.h",
# Swiftshader is Vulkan backend specific but the utilities are not.
"backend/vulkan/swiftshader_utilities.cc",
"backend/vulkan/swiftshader_utilities.h",
]
deps = [ "../renderer/backend" ]

View File

@@ -0,0 +1,68 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/impeller/playground/backend/vulkan/swiftshader_utilities.h"
#include <cstdlib>
#include "flutter/fml/build_config.h"
#include "flutter/fml/file.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/paths.h"
#if FML_OS_WIN
#include <Windows.h>
#endif // FML_OS_WIN
namespace impeller {
static void FindSwiftShaderICDAtKnownPaths() {
static constexpr const char* kSwiftShaderICDJSON = "vk_swiftshader_icd.json";
static constexpr const char* kVulkanICDFileNamesEnvVariableKey =
"VK_ICD_FILENAMES";
const auto executable_directory_path =
fml::paths::GetExecutableDirectoryPath();
FML_CHECK(executable_directory_path.first);
const auto executable_directory =
fml::OpenDirectory(executable_directory_path.second.c_str(), false,
fml::FilePermission::kRead);
FML_CHECK(executable_directory.is_valid());
if (fml::FileExists(executable_directory, kSwiftShaderICDJSON)) {
const auto icd_path = fml::paths::JoinPaths(
{executable_directory_path.second, kSwiftShaderICDJSON});
#if FML_OS_WIN
const auto success =
::SetEnvironmentVariableA(kVulkanICDFileNamesEnvVariableKey, //
icd_path.c_str() //
) != 0;
#else // FML_OS_WIN
const auto success = ::setenv(kVulkanICDFileNamesEnvVariableKey, //
icd_path.c_str(), //
1 // overwrite
) == 0;
#endif // FML_OS_WIN
FML_CHECK(success)
<< "Could not set the environment variable to use SwiftShader.";
} else {
FML_CHECK(false)
<< "Was asked to use SwiftShader but could not find the installable "
"client driver (ICD) for the locally built SwiftShader.";
}
}
void SetupSwiftshaderOnce(bool use_swiftshader) {
static bool swiftshader_preference = false;
static std::once_flag sOnceInitializer;
std::call_once(sOnceInitializer, [use_swiftshader]() {
if (use_swiftshader) {
FindSwiftShaderICDAtKnownPaths();
swiftshader_preference = use_swiftshader;
}
});
FML_CHECK(swiftshader_preference == use_swiftshader)
<< "The option to use SwiftShader in a process can only be set once and "
"may not be changed later.";
}
} // namespace impeller

View File

@@ -0,0 +1,26 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_IMPELLER_PLAYGROUND_BACKEND_VULKAN_SWIFTSHADER_UTILITIES_H_
#define FLUTTER_IMPELLER_PLAYGROUND_BACKEND_VULKAN_SWIFTSHADER_UTILITIES_H_
namespace impeller {
//------------------------------------------------------------------------------
/// @brief Find and setup the installable client driver for a locally built
/// SwiftShader at known paths. The option to use SwiftShader can
/// only be used once in the process. While calling this method
/// multiple times is fine, specifying a different use_swiftshader
/// value will trip an assertion.
///
/// @warning This call must be made before any Vulkan contexts are created in
/// the process.
///
/// @param[in] use_swiftshader If SwiftShader should be used.
///
void SetupSwiftshaderOnce(bool use_swiftshader);
} // namespace impeller
#endif // FLUTTER_IMPELLER_PLAYGROUND_BACKEND_VULKAN_SWIFTSHADER_UTILITIES_H_

View File

@@ -21,6 +21,7 @@
#include "impeller/core/allocator.h"
#include "impeller/core/formats.h"
#include "impeller/image/compressed_image.h"
#include "impeller/playground/backend/vulkan/swiftshader_utilities.h"
#include "impeller/playground/imgui/imgui_impl_impeller.h"
#include "impeller/playground/playground.h"
#include "impeller/playground/playground_impl.h"
@@ -48,38 +49,36 @@ std::string PlaygroundBackendToString(PlaygroundBackend backend) {
FML_UNREACHABLE();
}
struct Playground::GLFWInitializer {
GLFWInitializer() {
// This guard is a hack to work around a problem where glfwCreateWindow
// hangs when opening a second window after GLFW has been reinitialized (for
// example, when flipping through multiple playground tests).
//
// Explanation:
// * glfwCreateWindow calls [NSApp run], which begins running the event
// loop on the current thread.
// * GLFW then immediately stops the loop when
// applicationDidFinishLaunching is fired.
// * applicationDidFinishLaunching is only ever fired once during the
// application's lifetime, so subsequent calls to [NSApp run] will always
// hang with this setup.
// * glfwInit resets the flag that guards against [NSApp run] being
// called a second time, which causes the subsequent `glfwCreateWindow`
// to hang indefinitely in the event loop, because
// applicationDidFinishLaunching is never fired.
static std::once_flag sOnceInitializer;
std::call_once(sOnceInitializer, []() {
::glfwSetErrorCallback([](int code, const char* description) {
FML_LOG(ERROR) << "GLFW Error '" << description << "' (" << code
<< ").";
});
FML_CHECK(::glfwInit() == GLFW_TRUE);
static void InitializeGLFWOnce() {
// This guard is a hack to work around a problem where glfwCreateWindow
// hangs when opening a second window after GLFW has been reinitialized (for
// example, when flipping through multiple playground tests).
//
// Explanation:
// * glfwCreateWindow calls [NSApp run], which begins running the event
// loop on the current thread.
// * GLFW then immediately stops the loop when
// applicationDidFinishLaunching is fired.
// * applicationDidFinishLaunching is only ever fired once during the
// application's lifetime, so subsequent calls to [NSApp run] will always
// hang with this setup.
// * glfwInit resets the flag that guards against [NSApp run] being
// called a second time, which causes the subsequent `glfwCreateWindow`
// to hang indefinitely in the event loop, because
// applicationDidFinishLaunching is never fired.
static std::once_flag sOnceInitializer;
std::call_once(sOnceInitializer, []() {
::glfwSetErrorCallback([](int code, const char* description) {
FML_LOG(ERROR) << "GLFW Error '" << description << "' (" << code << ").";
});
}
};
FML_CHECK(::glfwInit() == GLFW_TRUE);
});
}
Playground::Playground(PlaygroundSwitches switches)
: switches_(switches),
glfw_initializer_(std::make_unique<GLFWInitializer>()) {}
Playground::Playground(PlaygroundSwitches switches) : switches_(switches) {
InitializeGLFWOnce();
SetupSwiftshaderOnce(switches_.use_swiftshader);
}
Playground::~Playground() = default;

View File

@@ -121,10 +121,7 @@ class Playground {
void SetWindowSize(ISize size);
private:
struct GLFWInitializer;
fml::TimeDelta start_time_;
std::unique_ptr<GLFWInitializer> glfw_initializer_;
std::unique_ptr<PlaygroundImpl> impl_;
std::shared_ptr<Context> context_;
std::unique_ptr<Renderer> renderer_;

View File

@@ -19,6 +19,7 @@ PlaygroundSwitches::PlaygroundSwitches(const fml::CommandLine& args) {
enable_playground = true;
}
enable_vulkan_validation = args.HasOption("enable_vulkan_validation");
use_swiftshader = args.HasOption("use_swiftshader");
}
} // namespace impeller

View File

@@ -20,6 +20,12 @@ struct PlaygroundSwitches {
// rendered in the playground.
std::optional<std::chrono::milliseconds> timeout;
bool enable_vulkan_validation = false;
//----------------------------------------------------------------------------
/// Seek a SwiftShader library in known locations and use it when running
/// Vulkan. It is a fatal error to provide this option and not have the test
/// find a SwiftShader implementation.
///
bool use_swiftshader = false;
bool use_angle = false;
PlaygroundSwitches();