Flutter Engine
The Flutter Engine
swiftshader_utilities.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 "flutter/impeller/playground/backend/vulkan/swiftshader_utilities.h"
6
7#include <cstdlib>
8
9#include "flutter/fml/build_config.h"
10#include "flutter/fml/file.h"
11#include "flutter/fml/logging.h"
12#include "flutter/fml/paths.h"
13
14#if FML_OS_WIN
15#include <Windows.h>
16#endif // FML_OS_WIN
17
18namespace impeller {
19
21 static constexpr const char* kSwiftShaderICDJSON = "vk_swiftshader_icd.json";
22 static constexpr const char* kVulkanICDFileNamesEnvVariableKey =
23 "VK_ICD_FILENAMES";
24 const auto executable_directory_path =
26 FML_CHECK(executable_directory_path.first);
27 const auto executable_directory =
28 fml::OpenDirectory(executable_directory_path.second.c_str(), false,
30 FML_CHECK(executable_directory.is_valid());
31 if (fml::FileExists(executable_directory, kSwiftShaderICDJSON)) {
32 const auto icd_path = fml::paths::JoinPaths(
33 {executable_directory_path.second, kSwiftShaderICDJSON});
34#if FML_OS_WIN
35 const auto success =
36 ::SetEnvironmentVariableA(kVulkanICDFileNamesEnvVariableKey, //
37 icd_path.c_str() //
38 ) != 0;
39#else // FML_OS_WIN
40 const auto success = ::setenv(kVulkanICDFileNamesEnvVariableKey, //
41 icd_path.c_str(), //
42 1 // overwrite
43 ) == 0;
44#endif // FML_OS_WIN
45 FML_CHECK(success)
46 << "Could not set the environment variable to use SwiftShader.";
47 } else {
48 FML_CHECK(false)
49 << "Was asked to use SwiftShader but could not find the installable "
50 "client driver (ICD) for the locally built SwiftShader.";
51 }
52}
53
54void SetupSwiftshaderOnce(bool use_swiftshader) {
55 static bool swiftshader_preference = false;
56 static std::once_flag sOnceInitializer;
57 std::call_once(sOnceInitializer, [use_swiftshader]() {
58 if (use_swiftshader) {
60 swiftshader_preference = use_swiftshader;
61 }
62 });
63 FML_CHECK(swiftshader_preference == use_swiftshader)
64 << "The option to use SwiftShader in a process can only be set once and "
65 "may not be changed later.";
66}
67
68} // namespace impeller
#define FML_CHECK(condition)
Definition: logging.h:85
std::string JoinPaths(std::initializer_list< std::string > components)
Definition: paths.cc:14
std::pair< bool, std::string > GetExecutableDirectoryPath()
Definition: paths.cc:55
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)
Definition: file_posix.cc:183
static void FindSwiftShaderICDAtKnownPaths()
void SetupSwiftshaderOnce(bool use_swiftshader)
Find and setup the installable client driver for a locally built SwiftShader at known paths....