25#include "third_party/abseil-cpp/absl/base/no_destructor.h"
27#define GLFW_INCLUDE_NONE
28#include "third_party/glfw/include/GLFW/glfw3.h"
33std::unique_ptr<PlaygroundImpl> MakeVulkanPlayground(
bool enable_validations) {
35 PlaygroundSwitches playground_switches;
36 playground_switches.enable_vulkan_validation = enable_validations;
42const std::unique_ptr<PlaygroundImpl>& GetSharedVulkanPlayground(
43 bool enable_validations) {
44 if (enable_validations) {
45 static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
46 vulkan_validation_playground(
47 MakeVulkanPlayground(
true));
51 [&] { (*vulkan_validation_playground)->GetContext()->Shutdown(); });
52 return *vulkan_validation_playground;
54 static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
55 vulkan_playground(MakeVulkanPlayground(
false));
59 [&] { (*vulkan_playground)->GetContext()->Shutdown(); });
60 return *vulkan_playground;
64std::unique_ptr<PlaygroundImpl> MakeOpenGLESPlayground(
bool use_sdfs =
false) {
66 PlaygroundSwitches playground_switches;
67 playground_switches.use_angle =
true;
68 playground_switches.flags.use_sdfs = use_sdfs;
76const std::unique_ptr<PlaygroundImpl>& GetSharedOpenGLESPlayground(
79 static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
80 opengl_playground(MakeOpenGLESPlayground(
true));
82 [&] { (*opengl_playground)->GetContext()->Shutdown(); });
83 return *opengl_playground;
85 static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
86 opengl_playground(MakeOpenGLESPlayground(
false));
90 [&] { (*opengl_playground)->GetContext()->Shutdown(); });
91 return *opengl_playground;
97#define IMP_AIKSTEST(name) \
98 "impeller_Play_AiksTest_" #name "_Metal", \
99 "impeller_Play_AiksTest_" #name "_OpenGLES", \
100 "impeller_Play_AiksTest_" #name "_Vulkan"
109 "impeller_Play_AiksTest_CanRenderClippedRuntimeEffects_Vulkan",
113std::string GetTestName() {
114 std::string suite_name =
115 ::testing::UnitTest::GetInstance()->current_test_suite()->name();
116 std::string test_name =
117 ::testing::UnitTest::GetInstance()->current_test_info()->name();
118 std::stringstream ss;
119 ss <<
"impeller_" << suite_name <<
"_" << test_name;
120 std::string result = ss.str();
122 std::replace(result.begin(), result.end(),
'/',
'_');
126std::string GetGoldenFilename(
const std::string& postfix) {
127 return GetTestName() + postfix +
".png";
132 std::unique_ptr<testing::Screenshot> screenshot,
133 const std::string& postfix) {
134 if (!screenshot || !screenshot->GetBytes()) {
135 FML_LOG(ERROR) <<
"Failed to collect screenshot for test " << GetTestName();
138 std::string test_name = GetTestName();
139 std::string filename = GetGoldenFilename(postfix);
141 test_name, filename, screenshot->GetWidth(), screenshot->GetHeight());
142 if (!screenshot->WriteToPNG(
144 FML_LOG(ERROR) <<
"Failed to write screenshot to " << filename;
164 std::shared_ptr<TypographerContext> typographer_context) {
165 typographer_context_ = std::move(typographer_context);
169 ASSERT_FALSE(dlopen(
"/usr/local/lib/libMoltenVK.dylib", RTLD_NOLOAD));
173 context->DisposeThreadLocalCachedResources();
178bool DoesSupportWideGamutTests() {
188 std::filesystem::path testing_assets_path =
190 std::filesystem::path target_path = testing_assets_path.parent_path()
194 std::filesystem::path icd_path = target_path /
"vk_swiftshader_icd.json";
195 setenv(
"VK_ICD_FILENAMES", icd_path.c_str(), 1);
197 std::string test_name = GetTestName();
200 test_name.find(
"WideGamut_") != std::string::npos;
202 test_name.find(
"ExperimentAntialiasLines_") != std::string::npos;
203 switch (GetParam()) {
208 if (!DoesSupportWideGamutTests()) {
210 <<
"This metal device doesn't support wide gamut golden tests.";
212 pimpl_->screenshotter =
213 std::make_unique<testing::MetalScreenshotter>(switches);
217 GTEST_SKIP() <<
"Vulkan doesn't support wide gamut golden tests.";
221 <<
"Vulkan doesn't support antialiased lines golden tests.";
223 const std::unique_ptr<PlaygroundImpl>& playground =
224 GetSharedVulkanPlayground(
true);
225 pimpl_->screenshotter =
226 std::make_unique<testing::VulkanScreenshotter>(playground);
234 GTEST_SKIP() <<
"OpenGLES doesn't support wide gamut golden tests.";
238 <<
"OpenGLES doesn't support antialiased lines golden tests.";
240 const std::unique_ptr<PlaygroundImpl>& playground =
242 ::glfwMakeContextCurrent(
243 reinterpret_cast<GLFWwindow*
>(playground->GetWindowHandle()));
244 pimpl_->screenshotter =
245 std::make_unique<testing::VulkanScreenshotter>(playground);
253 <<
"GoldenPlaygroundTest doesn't support interactive playground tests "
258 "gpu_string",
GetContext()->DescribeGpuModel());
269 std::unique_ptr<testing::Screenshot> screenshot;
270 Point content_scale =
271 pimpl_->screenshotter->GetPlayground().GetContentScale();
273 ISize physical_window_size(
274 std::round(pimpl_->window_size.width * content_scale.
x),
275 std::round(pimpl_->window_size.height * content_scale.
y));
276 for (
int i = 0;
i < 2; ++
i) {
280 screenshot = pimpl_->screenshotter->MakeScreenshot(renderer,
texture);
286 const sk_sp<flutter::DisplayList>& list) {
294 Point content_scale =
295 pimpl_->screenshotter->GetPlayground().GetContentScale();
296 ISize size(std::round(pimpl_->window_size.width * content_scale.
x),
297 std::round(pimpl_->window_size.height * content_scale.
y));
299 std::unique_ptr<testing::Screenshot> screenshot;
302 for (
int i = 0;
i < 2; ++
i) {
304 context->GetResourceAllocator());
306 *
context, size, 1,
"Golden Render Pass",
309 if (!render_target.
IsValid()) {
313 context->CreateCommandBuffer();
328 if (!
context->GetCommandQueue()->Submit({command_buffer}).ok()) {
331 screenshot = pimpl_->screenshotter->MakeScreenshot(
339 ImGuiWindowFlags flags) {
344 const char* fixture_name,
345 bool enable_mipmapping)
const {
346 std::shared_ptr<fml::Mapping> mapping =
351 result->SetLabel(fixture_name);
357 const char* fixture_name,
358 bool enable_mipmapping)
const {
359 std::shared_ptr<Texture>
texture =
365 const char* asset_name)
const {
366 const std::shared_ptr<fml::Mapping> fixture =
368 if (!fixture || fixture->GetSize() == 0) {
369 return absl::NotFoundError(
"Asset not found or empty.");
375 if (!pimpl_->screenshotter) {
378 return pimpl_->screenshotter->GetPlayground().GetContext();
387 bool enable_vulkan_validations =
true;
388 FML_CHECK(!pimpl_->test_vulkan_playground)
389 <<
"We don't support creating multiple contexts for one test";
390 pimpl_->test_vulkan_playground =
391 MakeVulkanPlayground(enable_vulkan_validations);
392 pimpl_->screenshotter = std::make_unique<testing::VulkanScreenshotter>(
393 pimpl_->test_vulkan_playground);
394 return pimpl_->test_vulkan_playground->GetContext();
397 FML_CHECK(!pimpl_->test_opengl_playground)
398 <<
"We don't support creating multiple contexts for one test";
400 pimpl_->test_opengl_playground = MakeOpenGLESPlayground(use_sdfs);
401 pimpl_->screenshotter = std::make_unique<testing::VulkanScreenshotter>(
402 pimpl_->test_opengl_playground);
403 return pimpl_->test_opengl_playground->GetContext();
411 return pimpl_->screenshotter->GetPlayground().GetContentScale();
419 return pimpl_->window_size;
426void GoldenPlaygroundTest::GoldenPlaygroundTest::SetWindowSize(
ISize size) {
427 pimpl_->window_size = size;
431 const std::shared_ptr<Capabilities>& capabilities) {
432 return pimpl_->screenshotter->GetPlayground().SetCapabilities(capabilities);
436 const sk_sp<flutter::DisplayList>& list) {
438 Point content_scale =
439 pimpl_->screenshotter->GetPlayground().GetContentScale();
441 ISize physical_window_size(
442 std::round(pimpl_->window_size.width * content_scale.
x),
443 std::round(pimpl_->window_size.height * content_scale.
y));
444 return pimpl_->screenshotter->MakeScreenshot(
449 return pimpl_->screenshotter->GetPlayground().GetRuntimeStageBackend();
Wraps a closure that is invoked in the destructor unless released by the caller.
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
IRect GetWindowBounds() const
PlaygroundBackend GetBackend() const
ISize GetWindowSize() const
RuntimeStageBackend GetRuntimeStageBackend() const
sk_sp< flutter::DlImage > CreateDlImageForFixture(const char *fixture_name, bool enable_mipmapping=false) const
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
static bool SaveScreenshot(std::unique_ptr< testing::Screenshot > screenshot, const std::string &postfix="")
std::unique_ptr< testing::Screenshot > MakeScreenshot(const sk_sp< flutter::DisplayList > &list)
Scalar GetSecondsElapsed() const
bool OpenPlaygroundHere(Picture picture)
static bool ImGuiBegin(const char *name, bool *p_open, ImGuiWindowFlags flags)
std::function< sk_sp< flutter::DisplayList >()> AiksDlPlaygroundCallback
~GoldenPlaygroundTest() override
absl::StatusOr< RuntimeStage::Map > OpenAssetAsRuntimeStage(const char *asset_name) const
std::shared_ptr< Context > GetContext() const
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
std::function< bool(RenderPass &pass)> SinglePassCallback
static std::shared_ptr< Texture > CreateTextureForMapping(const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)
static std::unique_ptr< PlaygroundImpl > Create(PlaygroundBackend backend, PlaygroundSwitches switches)
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
virtual RenderTarget CreateOffscreen(const Context &context, ISize size, int mip_count, std::string_view label="Offscreen", RenderTarget::AttachmentConfig color_attachment_config=RenderTarget::kDefaultColorAttachmentConfig, std::optional< RenderTarget::AttachmentConfig > stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &existing_color_texture=nullptr, const std::shared_ptr< Texture > &existing_depth_stencil_texture=nullptr, std::optional< PixelFormat > target_pixel_format=std::nullopt)
std::shared_ptr< Texture > GetRenderTargetTexture() const
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
static absl::StatusOr< 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()
FlutterDesktopBinaryReply 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::shared_ptr< Texture > DisplayListToTexture(const sk_sp< flutter::DisplayList > &display_list, ISize size, AiksContext &context, bool reset_host_buffer, bool generate_mips, std::optional< PixelFormat > target_pixel_format)
Render the provided display list to a texture with the given size.
std::shared_ptr< ContextGLES > context
std::shared_ptr< RenderPass > render_pass
std::shared_ptr< CommandBuffer > command_buffer
bool antialiased_lines
When turned on DrawLine will use the experimental antialiased path.
bool use_sdfs
Use SDFs for rendering.
std::unique_ptr< PlaygroundImpl > test_vulkan_playground
std::unique_ptr< testing::Screenshotter > screenshotter
std::unique_ptr< PlaygroundImpl > test_opengl_playground
static constexpr TRect MakeSize(const TSize< U > &size)