Flutter Impeller
golden_playground_test_mac.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <dlfcn.h>
6 #include <filesystem>
7 #include <memory>
8 
9 #include "display_list/display_list.h"
11 
15 #include "flutter/third_party/abseil-cpp/absl/base/no_destructor.h"
16 #include "fml/closure.h"
22 
23 #define GLFW_INCLUDE_NONE
24 #include "third_party/glfw/include/GLFW/glfw3.h"
25 
26 namespace impeller {
27 
28 namespace {
29 std::unique_ptr<PlaygroundImpl> MakeVulkanPlayground(bool enable_validations) {
30  FML_CHECK(::glfwInit() == GLFW_TRUE);
31  PlaygroundSwitches playground_switches;
32  playground_switches.enable_vulkan_validation = enable_validations;
34  playground_switches);
35 }
36 
37 // Returns a static instance to a playground that can be used across tests.
38 const std::unique_ptr<PlaygroundImpl>& GetSharedVulkanPlayground(
39  bool enable_validations) {
40  if (enable_validations) {
41  static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
42  vulkan_validation_playground(
43  MakeVulkanPlayground(/*enable_validations=*/true));
44  // TODO(142237): This can be removed when the thread local storage is
45  // removed.
46  static fml::ScopedCleanupClosure context_cleanup(
47  [&] { (*vulkan_validation_playground)->GetContext()->Shutdown(); });
48  return *vulkan_validation_playground;
49  } else {
50  static absl::NoDestructor<std::unique_ptr<PlaygroundImpl>>
51  vulkan_playground(MakeVulkanPlayground(/*enable_validations=*/false));
52  // TODO(142237): This can be removed when the thread local storage is
53  // removed.
54  static fml::ScopedCleanupClosure context_cleanup(
55  [&] { (*vulkan_playground)->GetContext()->Shutdown(); });
56  return *vulkan_playground;
57  }
58 }
59 
60 } // namespace
61 
62 #define IMP_AIKSTEST(name) \
63  "impeller_Play_AiksTest_" #name "_Metal", \
64  "impeller_Play_AiksTest_" #name "_OpenGLES", \
65  "impeller_Play_AiksTest_" #name "_Vulkan"
66 
67 // If you add a new playground test to the aiks unittests and you do not want it
68 // to also be a golden test, then add the test name here.
69 static const std::vector<std::string> kSkipTests = {
70  // TextRotated is flakey and we can't seem to get it to stabilize on Skia
71  // Gold.
72  IMP_AIKSTEST(TextRotated),
73  // Runtime stage based tests get confused with a Metal context.
74  "impeller_Play_AiksTest_CanRenderClippedRuntimeEffects_Vulkan",
75 };
76 
77 namespace {
78 std::string GetTestName() {
79  std::string suite_name =
80  ::testing::UnitTest::GetInstance()->current_test_suite()->name();
81  std::string test_name =
82  ::testing::UnitTest::GetInstance()->current_test_info()->name();
83  std::stringstream ss;
84  ss << "impeller_" << suite_name << "_" << test_name;
85  std::string result = ss.str();
86  // Make sure there are no slashes in the test name.
87  std::replace(result.begin(), result.end(), '/', '_');
88  return result;
89 }
90 
91 std::string GetGoldenFilename(const std::string& postfix) {
92  return GetTestName() + postfix + ".png";
93 }
94 } // namespace
95 
97  std::unique_ptr<testing::Screenshot> screenshot,
98  const std::string& postfix) {
99  if (!screenshot || !screenshot->GetBytes()) {
100  FML_LOG(ERROR) << "Failed to collect screenshot for test " << GetTestName();
101  return false;
102  }
103  std::string test_name = GetTestName();
104  std::string filename = GetGoldenFilename(postfix);
106  test_name, filename, screenshot->GetWidth(), screenshot->GetHeight());
107  if (!screenshot->WriteToPNG(
109  FML_LOG(ERROR) << "Failed to write screenshot to " << filename;
110  return false;
111  }
112  return true;
113 }
114 
116  std::unique_ptr<PlaygroundImpl> test_vulkan_playground;
117  std::unique_ptr<PlaygroundImpl> test_opengl_playground;
118  std::unique_ptr<testing::Screenshotter> screenshotter;
119  ISize window_size = ISize{1024, 768};
120 };
121 
123  : typographer_context_(TypographerContextSkia::Make()),
125 
127 
129  std::shared_ptr<TypographerContext> typographer_context) {
130  typographer_context_ = std::move(typographer_context);
131 };
132 
134  ASSERT_FALSE(dlopen("/usr/local/lib/libMoltenVK.dylib", RTLD_NOLOAD));
135 }
136 
137 namespace {
138 bool DoesSupportWideGamutTests() {
139 #ifdef __arm64__
140  return true;
141 #else
142  return false;
143 #endif
144 }
145 } // namespace
146 
148  std::filesystem::path testing_assets_path =
149  flutter::testing::GetTestingAssetsPath();
150  std::filesystem::path target_path = testing_assets_path.parent_path()
151  .parent_path()
152  .parent_path()
153  .parent_path();
154  std::filesystem::path icd_path = target_path / "vk_swiftshader_icd.json";
155  setenv("VK_ICD_FILENAMES", icd_path.c_str(), 1);
156 
157  std::string test_name = GetTestName();
158  PlaygroundSwitches switches;
159  switches.enable_wide_gamut =
160  test_name.find("WideGamut_") != std::string::npos;
161  switches.flags.antialiased_lines =
162  test_name.find("ExperimentAntialiasLines_") != std::string::npos;
163  switch (GetParam()) {
165  if (!DoesSupportWideGamutTests()) {
166  GTEST_SKIP()
167  << "This metal device doesn't support wide gamut golden tests.";
168  }
169  pimpl_->screenshotter =
170  std::make_unique<testing::MetalScreenshotter>(switches);
171  break;
173  if (switches.enable_wide_gamut) {
174  GTEST_SKIP() << "Vulkan doesn't support wide gamut golden tests.";
175  }
176  if (switches.flags.antialiased_lines) {
177  GTEST_SKIP()
178  << "Vulkan doesn't support antialiased lines golden tests.";
179  }
180  const std::unique_ptr<PlaygroundImpl>& playground =
181  GetSharedVulkanPlayground(/*enable_validations=*/true);
182  pimpl_->screenshotter =
183  std::make_unique<testing::VulkanScreenshotter>(playground);
184  break;
185  }
187  if (switches.enable_wide_gamut) {
188  GTEST_SKIP() << "OpenGLES doesn't support wide gamut golden tests.";
189  }
190  if (switches.flags.antialiased_lines) {
191  GTEST_SKIP()
192  << "OpenGLES doesn't support antialiased lines golden tests.";
193  }
194  FML_CHECK(::glfwInit() == GLFW_TRUE);
195  PlaygroundSwitches playground_switches;
196  playground_switches.use_angle = true;
197  pimpl_->test_opengl_playground = PlaygroundImpl::Create(
198  PlaygroundBackend::kOpenGLES, playground_switches);
199  pimpl_->screenshotter = std::make_unique<testing::VulkanScreenshotter>(
200  pimpl_->test_opengl_playground);
201  break;
202  }
203  }
204 
205  if (std::find(kSkipTests.begin(), kSkipTests.end(), test_name) !=
206  kSkipTests.end()) {
207  GTEST_SKIP()
208  << "GoldenPlaygroundTest doesn't support interactive playground tests "
209  "yet.";
210  }
211 
213  "gpu_string", GetContext()->DescribeGpuModel());
214 }
215 
217  return GetParam();
218 }
219 
221  const AiksDlPlaygroundCallback& callback) {
222  AiksContext renderer(GetContext(), typographer_context_);
223 
224  std::unique_ptr<testing::Screenshot> screenshot;
225  for (int i = 0; i < 2; ++i) {
226  auto display_list = callback();
227  auto texture =
228  DisplayListToTexture(display_list, pimpl_->window_size, renderer);
229  screenshot = pimpl_->screenshotter->MakeScreenshot(renderer, texture);
230  }
231  return SaveScreenshot(std::move(screenshot));
232 }
233 
235  const sk_sp<flutter::DisplayList>& list) {
236  return OpenPlaygroundHere([&list]() { return list; });
237 }
238 
239 bool GoldenPlaygroundTest::ImGuiBegin(const char* name,
240  bool* p_open,
241  ImGuiWindowFlags flags) {
242  return false;
243 }
244 
246  const char* fixture_name,
247  bool enable_mipmapping) const {
248  std::shared_ptr<fml::Mapping> mapping =
249  flutter::testing::OpenFixtureAsMapping(fixture_name);
250  auto result = Playground::CreateTextureForMapping(GetContext(), mapping,
251  enable_mipmapping);
252  if (result) {
253  result->SetLabel(fixture_name);
254  }
255  return result;
256 }
257 
259  const char* fixture_name,
260  bool enable_mipmapping) const {
261  std::shared_ptr<Texture> texture =
262  CreateTextureForFixture(fixture_name, enable_mipmapping);
263  return DlImageImpeller::Make(texture);
264 }
265 
267  const char* asset_name) const {
268  const std::shared_ptr<fml::Mapping> fixture =
269  flutter::testing::OpenFixtureAsMapping(asset_name);
270  if (!fixture || fixture->GetSize() == 0) {
271  return {};
272  }
273  return RuntimeStage::DecodeRuntimeStages(fixture);
274 }
275 
276 std::shared_ptr<Context> GoldenPlaygroundTest::GetContext() const {
277  return pimpl_->screenshotter->GetPlayground().GetContext();
278 }
279 
280 std::shared_ptr<Context> GoldenPlaygroundTest::MakeContext() const {
281  if (GetParam() == PlaygroundBackend::kMetal) {
282  /// On Metal we create a context for each test.
283  return GetContext();
284  } else if (GetParam() == PlaygroundBackend::kVulkan) {
285  bool enable_vulkan_validations = true;
286  FML_CHECK(!pimpl_->test_vulkan_playground)
287  << "We don't support creating multiple contexts for one test";
288  pimpl_->test_vulkan_playground =
289  MakeVulkanPlayground(enable_vulkan_validations);
290  pimpl_->screenshotter = std::make_unique<testing::VulkanScreenshotter>(
291  pimpl_->test_vulkan_playground);
292  return pimpl_->test_vulkan_playground->GetContext();
293  } else {
294  /// On OpenGL we create a context for each test.
295  return GetContext();
296  }
297 }
298 
300  return pimpl_->screenshotter->GetPlayground().GetContentScale();
301 }
302 
304  return 0.0f;
305 }
306 
308  return pimpl_->window_size;
309 }
310 
311 void GoldenPlaygroundTest::GoldenPlaygroundTest::SetWindowSize(ISize size) {
312  pimpl_->window_size = size;
313 }
314 
316  const std::shared_ptr<Capabilities>& capabilities) {
317  return pimpl_->screenshotter->GetPlayground().SetCapabilities(capabilities);
318 }
319 
320 std::unique_ptr<testing::Screenshot> GoldenPlaygroundTest::MakeScreenshot(
321  const sk_sp<flutter::DisplayList>& list) {
322  AiksContext renderer(GetContext(), typographer_context_);
323 
324  return pimpl_->screenshotter->MakeScreenshot(
325  renderer, DisplayListToTexture(list, pimpl_->window_size, renderer));
326 }
327 
328 } // namespace impeller
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
sk_sp< flutter::DlImage > CreateDlImageForFixture(const char *fixture_name, bool enable_mipmapping=false) 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)
RuntimeStage::Map OpenAssetAsRuntimeStage(const char *asset_name) const
bool OpenPlaygroundHere(Picture picture)
static bool ImGuiBegin(const char *name, bool *p_open, ImGuiWindowFlags flags)
std::function< sk_sp< flutter::DisplayList >()> AiksDlPlaygroundCallback
std::shared_ptr< Context > GetContext() const
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
static std::shared_ptr< Texture > CreateTextureForMapping(const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)
Definition: playground.cc:432
static std::unique_ptr< PlaygroundImpl > Create(PlaygroundBackend backend, PlaygroundSwitches switches)
std::map< RuntimeStageBackend, std::shared_ptr< RuntimeStage > > Map
Definition: runtime_stage.h:24
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()
#define IMP_AIKSTEST(name)
std::shared_ptr< Texture > DisplayListToTexture(const sk_sp< flutter::DisplayList > &display_list, ISize size, AiksContext &context, bool reset_host_buffer, bool generate_mips)
Render the provided display list to a texture with the given size.
static const std::vector< std::string > kSkipTests
float Scalar
Definition: scalar.h:18
PlaygroundBackend
Definition: playground.h:27
bool antialiased_lines
When turned on DrawLine will use the experimental antialiased path.
Definition: flags.h:14