| 1 |
/* |
| 2 |
* Copyright (c) 2021 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2021 Valve Corporation |
| 4 |
* Copyright (c) 2021 LunarG, Inc. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and/or associated documentation files (the "Materials"), to |
| 8 |
* deal in the Materials without restriction, including without limitation the |
| 9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 |
* sell copies of the Materials, and to permit persons to whom the Materials are |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice(s) and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Materials. |
| 15 |
* |
| 16 |
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
* |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 |
* USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 |
* |
| 25 |
* Author: Charles Giessen <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "shim.h" |
| 29 |
|
| 30 |
#include "env_var_wrapper.h" |
| 31 |
|
| 32 |
std::vector<std::string> parse_env_var_list(std::string const& var) { |
| 33 |
std::vector<std::string> items; |
| 34 |
size_t start = 0; |
| 35 |
size_t len = 0; |
| 36 |
for (size_t i = 0; i < var.size(); i++) { |
| 37 |
#if defined(_WIN32) |
| 38 |
if (var[i] == ';') { |
| 39 |
#elif TESTING_COMMON_UNIX_PLATFORMS |
| 40 |
if (var[i] == ':') { |
| 41 |
#endif |
| 42 |
if (len != 0) { |
| 43 |
// only push back non empty strings |
| 44 |
items.push_back(var.substr(start, len)); |
| 45 |
} |
| 46 |
start = i + 1; |
| 47 |
len = 0; |
| 48 |
} else { |
| 49 |
len++; |
| 50 |
} |
| 51 |
} |
| 52 |
items.push_back(var.substr(start, len)); |
| 53 |
|
| 54 |
return items; |
| 55 |
} |
| 56 |
|
| 57 |
#if defined(_WIN32) |
| 58 |
|
| 59 |
D3DKMT_Adapter& D3DKMT_Adapter::add_driver_manifest_path(std::filesystem::path const& src) { return add_path(src, driver_paths); } |
| 60 |
D3DKMT_Adapter& D3DKMT_Adapter::add_implicit_layer_manifest_path(std::filesystem::path const& src) { |
| 61 |
return add_path(src, implicit_layer_paths); |
| 62 |
} |
| 63 |
D3DKMT_Adapter& D3DKMT_Adapter::add_explicit_layer_manifest_path(std::filesystem::path const& src) { |
| 64 |
return add_path(src, explicit_layer_paths); |
| 65 |
} |
| 66 |
|
| 67 |
D3DKMT_Adapter& D3DKMT_Adapter::add_path(std::filesystem::path src, std::vector<std::wstring>& dest) { |
| 68 |
dest.push_back(src.native()); |
| 69 |
return *this; |
| 70 |
} |
| 71 |
|
| 72 |
std::string category_path_name(ManifestCategory category) { |
| 73 |
if (category == ManifestCategory::implicit_layer) return "ImplicitLayers"; |
| 74 |
if (category == ManifestCategory::explicit_layer) |
| 75 |
return "ExplicitLayers"; |
| 76 |
else |
| 77 |
return "Drivers"; |
| 78 |
} |
| 79 |
|
| 80 |
void PlatformShim::reset() { |
| 81 |
hkey_current_user_explicit_layers.clear(); |
| 82 |
hkey_current_user_implicit_layers.clear(); |
| 83 |
hkey_local_machine_explicit_layers.clear(); |
| 84 |
hkey_local_machine_implicit_layers.clear(); |
| 85 |
hkey_local_machine_drivers.clear(); |
| 86 |
hkey_local_machine_settings.clear(); |
| 87 |
hkey_current_user_settings.clear(); |
| 88 |
} |
| 89 |
|
| 90 |
void PlatformShim::add_manifest_to_registry(ManifestCategory category, std::filesystem::path const& path) { |
| 91 |
if (category == ManifestCategory::settings) { |
| 92 |
hkey_local_machine_settings.emplace_back(path); |
| 93 |
} else if (category == ManifestCategory::implicit_layer) { |
| 94 |
hkey_local_machine_implicit_layers.emplace_back(path); |
| 95 |
} else if (category == ManifestCategory::explicit_layer) { |
| 96 |
hkey_local_machine_explicit_layers.emplace_back(path); |
| 97 |
} else { |
| 98 |
hkey_local_machine_drivers.emplace_back(path); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void PlatformShim::add_unsecured_manifest_to_registry(ManifestCategory category, std::filesystem::path const& path) { |
| 103 |
if (category == ManifestCategory::settings) { |
| 104 |
hkey_current_user_settings.emplace_back(path); |
| 105 |
} else if (category == ManifestCategory::implicit_layer) { |
| 106 |
hkey_current_user_implicit_layers.emplace_back(path); |
| 107 |
} else if (category == ManifestCategory::explicit_layer) { |
| 108 |
hkey_current_user_explicit_layers.emplace_back(path); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void PlatformShim::add_dxgi_adapter(GpuType gpu_preference, DXGI_ADAPTER_DESC1 desc1) { |
| 113 |
uint32_t next_index = static_cast<uint32_t>(dxgi_adapters.size()); |
| 114 |
dxgi_adapters.emplace(next_index, DXGIAdapter{gpu_preference, desc1, next_index}); |
| 115 |
} |
| 116 |
|
| 117 |
void PlatformShim::add_d3dkmt_adapter(D3DKMT_Adapter const& adapter) { d3dkmt_adapters.push_back(adapter); } |
| 118 |
|
| 119 |
void PlatformShim::set_app_package_path(std::filesystem::path const& path) { app_package_path = path; } |
| 120 |
|
| 121 |
// TODO: |
| 122 |
void PlatformShim::add_CM_Device_ID([[maybe_unused]] std::wstring const& id, [[maybe_unused]] std::filesystem::path const& icd_path, |
| 123 |
[[maybe_unused]] std::filesystem::path const& layer_path) { |
| 124 |
// // append a null byte as separator if there is already id's in the list |
| 125 |
// if (CM_device_ID_list.size() != 0) { |
| 126 |
// CM_device_ID_list += L'\0'; // I'm sure this wont cause issues with std::string down the line... /s |
| 127 |
// } |
| 128 |
// CM_device_ID_list += id; |
| 129 |
// std::string id_str(id.length(), '\0'); |
| 130 |
// size_t size_written{}; |
| 131 |
// wcstombs_s(&size_written, &id_str[0], id_str.length(), id.c_str(), id.length()); |
| 132 |
|
| 133 |
// std::string device_path = std::string(pnp_registry_path) + "\\" + id_str; |
| 134 |
// CM_device_ID_registry_keys.push_back(device_path.c_str()); |
| 135 |
// add_key_value_string(id_key, "VulkanDriverName", icd_path.c_str()); |
| 136 |
// add_key_value_string(id_key, "VulkanLayerName", layer_path.c_str()); |
| 137 |
// // TODO: decide how to handle 32 bit |
| 138 |
// // add_key_value_string(id_key, "VulkanDriverNameWoW", icd_path.c_str()); |
| 139 |
// // add_key_value_string(id_key, "VulkanLayerName", layer_path.c_str()); |
| 140 |
} |
| 141 |
|
| 142 |
#elif TESTING_COMMON_UNIX_PLATFORMS |
| 143 |
|
| 144 |
#include <dirent.h> |
| 145 |
#include <unistd.h> |
| 146 |
|
| 147 |
std::string category_path_name(ManifestCategory category) { |
| 148 |
if (category == ManifestCategory::settings) return "settings.d"; |
| 149 |
if (category == ManifestCategory::implicit_layer) return "implicit_layer.d"; |
| 150 |
if (category == ManifestCategory::explicit_layer) |
| 151 |
return "explicit_layer.d"; |
| 152 |
else |
| 153 |
return "icd.d"; |
| 154 |
} |
| 155 |
|
| 156 |
void PlatformShim::add_system_library(std::string const& filename, std::filesystem::path const& actual_path) { |
| 157 |
system_library_redirection_map[filename] = actual_path; |
| 158 |
} |
| 159 |
|
| 160 |
std::filesystem::path PlatformShim::get_system_library(std::string const& filename) { |
| 161 |
return system_library_redirection_map.count(filename) == 1 ? system_library_redirection_map.at(filename) |
| 162 |
: std::filesystem::path{}; |
| 163 |
} |
| 164 |
#endif |