9#include "flutter/impeller/golden_tests/golden_playground_test.h"
11#include "flutter/impeller/aiks/picture.h"
12#include "flutter/impeller/golden_tests/golden_digest.h"
13#include "flutter/impeller/golden_tests/metal_screenshotter.h"
14#include "flutter/impeller/golden_tests/vulkan_screenshotter.h"
15#include "flutter/third_party/abseil-cpp/absl/base/no_destructor.h"
19#define GLFW_INCLUDE_NONE
20#include "third_party/glfw/include/GLFW/glfw3.h"
25std::unique_ptr<PlaygroundImpl> MakeVulkanPlayground(
bool enable_validations) {
27 PlaygroundSwitches playground_switches;
28 playground_switches.enable_vulkan_validation = enable_validations;
34const std::unique_ptr<PlaygroundImpl>& GetSharedVulkanPlayground(
35 bool enable_validations) {
36 if (enable_validations) {
37 static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
38 vulkan_validation_playground(
39 MakeVulkanPlayground(
true));
43 [&] { (*vulkan_validation_playground)->GetContext()->Shutdown(); });
44 return *vulkan_validation_playground;
46 static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
47 vulkan_playground(MakeVulkanPlayground(
false));
51 [&] { (*vulkan_playground)->GetContext()->Shutdown(); });
52 return *vulkan_playground;
57#define IMP_AIKSTEST(name) \
58 "impeller_Play_AiksTest_" #name "_Metal", \
59 "impeller_Play_AiksTest_" #name "_OpenGLES", \
60 "impeller_Play_AiksTest_" #name "_Vulkan"
69 "impeller_Play_AiksTest_CanRenderClippedRuntimeEffects_Vulkan",
73std::string GetTestName() {
74 std::string suite_name =
75 ::testing::UnitTest::GetInstance()->current_test_suite()->name();
76 std::string test_name =
77 ::testing::UnitTest::GetInstance()->current_test_info()->name();
79 ss <<
"impeller_" << suite_name <<
"_" << test_name;
80 std::string
result = ss.str();
86std::string GetGoldenFilename() {
87 return GetTestName() +
".png";
90bool SaveScreenshot(std::unique_ptr<testing::Screenshot> screenshot) {
91 if (!screenshot || !screenshot->GetBytes()) {
92 FML_LOG(
ERROR) <<
"Failed to collect screenshot for test " << GetTestName();
95 std::string test_name = GetTestName();
96 std::string filename = GetGoldenFilename();
98 test_name, filename, screenshot->GetWidth(), screenshot->GetHeight());
99 if (!screenshot->WriteToPNG(
101 FML_LOG(
ERROR) <<
"Failed to write screenshot to " << filename;
123 std::shared_ptr<TypographerContext> typographer_context) {
124 typographer_context_ = std::move(typographer_context);
128 ASSERT_FALSE(dlopen(
"/usr/local/lib/libMoltenVK.dylib", RTLD_NOLOAD));
132bool DoesSupportWideGamutTests() {
142 std::filesystem::path testing_assets_path =
144 std::filesystem::path target_path = testing_assets_path.parent_path()
148 std::filesystem::path icd_path = target_path /
"vk_swiftshader_icd.json";
149 setenv(
"VK_ICD_FILENAMES", icd_path.c_str(), 1);
151 std::string test_name = GetTestName();
152 bool enable_wide_gamut = test_name.find(
"WideGamut_") != std::string::npos;
153 switch (GetParam()) {
155 if (!DoesSupportWideGamutTests()) {
157 "This metal device doesn't support wide gamut golden tests.");
159 pimpl_->screenshotter =
160 std::make_unique<testing::MetalScreenshotter>(enable_wide_gamut);
163 if (enable_wide_gamut) {
164 GTEST_SKIP_(
"Vulkan doesn't support wide gamut golden tests.");
166 const std::unique_ptr<PlaygroundImpl>& playground =
167 GetSharedVulkanPlayground(
true);
168 pimpl_->screenshotter =
169 std::make_unique<testing::VulkanScreenshotter>(playground);
173 if (enable_wide_gamut) {
174 GTEST_SKIP_(
"OpenGLES doesn't support wide gamut golden tests.");
181 pimpl_->screenshotter = std::make_unique<testing::VulkanScreenshotter>(
182 pimpl_->test_opengl_playground);
190 "GoldenPlaygroundTest doesn't support interactive playground tests "
195 "gpu_string",
GetContext()->DescribeGpuModel());
205 auto screenshot = pimpl_->screenshotter->MakeScreenshot(renderer, picture,
206 pimpl_->window_size);
207 return SaveScreenshot(std::move(screenshot));
215 std::optional<Picture> picture;
216 std::unique_ptr<testing::Screenshot> screenshot;
217 for (
int i = 0; i < 2; ++i) {
219 if (!picture.has_value()) {
222 screenshot = pimpl_->screenshotter->MakeScreenshot(
223 renderer, picture.value(), pimpl_->window_size);
226 return SaveScreenshot(std::move(screenshot));
231 ImGuiWindowFlags
flags) {
236 const char* fixture_name,
237 bool enable_mipmapping)
const {
238 std::shared_ptr<fml::Mapping> mapping =
243 result->SetLabel(fixture_name);
249 const char* asset_name)
const {
250 const std::shared_ptr<fml::Mapping> fixture =
252 if (!fixture || fixture->GetSize() == 0) {
259 return pimpl_->screenshotter->GetPlayground().GetContext();
267 bool enable_vulkan_validations =
true;
268 FML_CHECK(!pimpl_->test_vulkan_playground)
269 <<
"We don't support creating multiple contexts for one test";
270 pimpl_->test_vulkan_playground =
271 MakeVulkanPlayground(enable_vulkan_validations);
272 pimpl_->screenshotter = std::make_unique<testing::VulkanScreenshotter>(
273 pimpl_->test_vulkan_playground);
274 return pimpl_->test_vulkan_playground->GetContext();
282 return pimpl_->screenshotter->GetPlayground().GetContentScale();
290 return pimpl_->window_size;
293void GoldenPlaygroundTest::GoldenPlaygroundTest::SetWindowSize(
ISize size) {
294 pimpl_->window_size = size;
298 const std::shared_ptr<Capabilities>& capabilities) {
299 return pimpl_->screenshotter->GetPlayground().SetCapabilities(capabilities);
static std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap *src, const SkYUVAPixmaps *srcYUVA, const SkColorSpace *srcYUVAColorSpace, const SkJpegEncoder::Options &options)
Wraps a closure that is invoked in the destructor unless released by the caller.
PlaygroundBackend GetBackend() const
ISize GetWindowSize() const
std::function< std::optional< Picture >(AiksContext &renderer)> AiksPlaygroundCallback
Point GetContentScale() const
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
void SetTypographerContext(std::shared_ptr< TypographerContext > typographer_context)
std::shared_ptr< Context > MakeContext() const
Scalar GetSecondsElapsed() const
RuntimeStage::Map OpenAssetAsRuntimeStage(const char *asset_name) const
bool OpenPlaygroundHere(Picture picture)
static bool ImGuiBegin(const char *name, bool *p_open, ImGuiWindowFlags flags)
~GoldenPlaygroundTest() override
std::shared_ptr< Context > GetContext() const
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
static std::unique_ptr< PlaygroundImpl > Create(PlaygroundBackend backend, PlaygroundSwitches switches)
static std::shared_ptr< Texture > CreateTextureForMapping(const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)
std::map< RuntimeStageBackend, std::shared_ptr< RuntimeStage > > Map
static Map DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
static GoldenDigest * Instance()
void AddDimension(const std::string &name, const std::string &value)
void AddImage(const std::string &test_name, const std::string &filename, int32_t width, int32_t height)
std::string GetFilenamePath(const std::string &filename) const
static WorkingDirectory * Instance()
FlutterSemanticsFlag flags
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_LOG(severity)
#define FML_CHECK(condition)
#define IMP_AIKSTEST(name)
const char * GetTestingAssetsPath()
Returns the directory containing assets shared across all tests.
std::unique_ptr< fml::Mapping > OpenFixtureAsMapping(const std::string &fixture_name)
Opens a fixture of the given file name and returns a mapping to its contents.
static const std::vector< std::string > kSkipTests
std::unique_ptr< PlaygroundImpl > test_vulkan_playground
std::unique_ptr< testing::Screenshotter > screenshotter
std::unique_ptr< PlaygroundImpl > test_opengl_playground