Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
test_swiftshader_utils.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
6
7#include <cstdlib>
8#include <mutex>
9
11#include "flutter/fml/file.h"
12#include "flutter/fml/logging.h"
13#include "flutter/fml/paths.h"
14
15#if FML_OS_WIN
16#include <Windows.h>
17#endif // FML_OS_WIN
18
19namespace flutter::testing {
20
21namespace {
22
23void FindSwiftShaderICDAtKnownPaths() {
24 static constexpr const char* kSwiftShaderICDJSON = "vk_swiftshader_icd.json";
25 static constexpr const char* kVulkanICDFileNamesEnvVariableKey =
26 "VK_ICD_FILENAMES";
27 const auto executable_directory_path =
29 FML_CHECK(executable_directory_path.first);
30 auto icd_directory = executable_directory_path.second;
31 if (icd_directory.ends_with("exe.unstripped")) {
32 icd_directory = fml::paths::GetDirectoryName(icd_directory);
33 }
34 const auto icd_directory_fd = fml::OpenDirectory(icd_directory.c_str(), false,
36 FML_CHECK(icd_directory_fd.is_valid());
37 if (fml::FileExists(icd_directory_fd, kSwiftShaderICDJSON)) {
38 const auto icd_path =
39 fml::paths::JoinPaths({icd_directory, kSwiftShaderICDJSON});
40#if FML_OS_WIN
41 const auto success =
42 ::SetEnvironmentVariableA(kVulkanICDFileNamesEnvVariableKey, //
43 icd_path.c_str() //
44 ) != 0;
45#else // FML_OS_WIN
46 const auto success = ::setenv(kVulkanICDFileNamesEnvVariableKey, //
47 icd_path.c_str(), //
48 1 // overwrite
49 ) == 0;
50#endif // FML_OS_WIN
51 FML_CHECK(success)
52 << "Could not set the environment variable to use SwiftShader.";
53 } else {
54 FML_CHECK(false)
55 << "Was asked to use SwiftShader but could not find the installable "
56 "client driver (ICD) for the locally built SwiftShader.";
57 }
58}
59
60} // namespace
61
62void SetupSwiftshaderOnce(bool use_swiftshader) {
63 static bool swiftshader_preference = false;
64 static std::once_flag sOnceInitializer;
65 std::call_once(sOnceInitializer, [use_swiftshader]() {
66 if (use_swiftshader) {
67 FindSwiftShaderICDAtKnownPaths();
68 swiftshader_preference = use_swiftshader;
69 }
70 });
71 FML_CHECK(swiftshader_preference == use_swiftshader)
72 << "The option to use SwiftShader in a process can only be set once and "
73 "may not be changed later.";
74}
75
76} // namespace flutter::testing
#define FML_CHECK(condition)
Definition logging.h:104
void SetupSwiftshaderOnce(bool use_swiftshader)
Find and setup the installable client driver for a locally built SwiftShader at known paths....
std::string JoinPaths(std::initializer_list< std::string > components)
Definition paths.cc:14
std::pair< bool, std::string > GetExecutableDirectoryPath()
Definition paths.cc:55
std::string GetDirectoryName(const std::string &path)
fml::UniqueFD OpenDirectory(const char *path, bool create_if_necessary, FilePermission permission)
Definition file_posix.cc:97
bool FileExists(const fml::UniqueFD &base_directory, const char *path)