Debian Codegraph @

tests/framework/util/manifest_builders.cpp

raw
1 /*
2 * Copyright (c) 2025 The Khronos Group Inc.
3 * Copyright (c) 2025 Valve Corporation
4 * Copyright (c) 2025 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 */
26
27 #include "manifest_builders.h"
28
29 #include "json_writer.h"
30
31 template <typename T>
32 void print_object_of_t(JsonWriter& writer, const char* object_name, std::vector<T> const& vec) {
33 if (vec.size() == 0) return;
34 writer.StartKeyedObject(object_name);
35 for (auto& element : vec) {
36 element.get_manifest_str(writer);
37 }
38 writer.EndObject();
39 }
40
41 template <typename T>
42 void print_array_of_t(JsonWriter& writer, const char* object_name, std::vector<T> const& vec) {
43 if (vec.size() == 0) return;
44 writer.StartKeyedArray(object_name);
45 for (auto& element : vec) {
46 element.get_manifest_str(writer);
47 }
48 writer.EndArray();
49 }
50 void print_vector_of_strings(JsonWriter& writer, const char* object_name, std::vector<std::string> const& strings) {
51 if (strings.size() == 0) return;
52 writer.StartKeyedArray(object_name);
53 for (auto const& str : strings) {
54 writer.AddString(std::filesystem::path(str).native());
55 }
56 writer.EndArray();
57 }
58 void print_vector_of_strings(JsonWriter& writer, const char* object_name, std::vector<std::filesystem::path> const& paths) {
59 if (paths.size() == 0) return;
60 writer.StartKeyedArray(object_name);
61 for (auto const& path : paths) {
62 writer.AddString(path.native());
63 }
64 writer.EndArray();
65 }
66
67 std::string version_to_string(uint32_t version) {
68 std::string out = std::to_string(VK_API_VERSION_MAJOR(version)) + "." + std::to_string(VK_API_VERSION_MINOR(version)) + "." +
69 std::to_string(VK_API_VERSION_PATCH(version));
70 if (VK_API_VERSION_VARIANT(version) != 0) out += std::to_string(VK_API_VERSION_VARIANT(version)) + "." + out;
71 return out;
72 }
73
74 std::string to_text(bool b) { return b ? std::string("true") : std::string("false"); }
75
76 std::string ManifestICD::get_manifest_str() const {
77 JsonWriter writer;
78 writer.StartObject();
79 writer.AddKeyedString("file_format_version", file_format_version.get_version_str());
80 writer.StartKeyedObject("ICD");
81 writer.AddKeyedString("library_path", lib_path.native());
82 writer.AddKeyedString("api_version", version_to_string(api_version));
83 writer.AddKeyedBool("is_portability_driver", is_portability_driver);
84 if (!library_arch.empty()) writer.AddKeyedString("library_arch", library_arch);
85 writer.EndObject();
86 writer.EndObject();
87 return writer.output;
88 }
89
90 void ManifestLayer::LayerDescription::FunctionOverride::get_manifest_str(JsonWriter& writer) const {
91 writer.AddKeyedString(vk_func, override_name);
92 }
93
94 void ManifestLayer::LayerDescription::Extension::get_manifest_str(JsonWriter& writer) const {
95 writer.StartObject();
96 writer.AddKeyedString("name", name);
97 writer.AddKeyedString("spec_version", std::to_string(spec_version));
98 writer.AddKeyedString("spec_version", std::to_string(spec_version));
99 print_vector_of_strings(writer, "entrypoints", entrypoints);
100 writer.EndObject();
101 }
102
103 void ManifestLayer::LayerDescription::get_manifest_str(JsonWriter& writer) const {
104 writer.AddKeyedString("name", name);
105 writer.AddKeyedString("type", get_type_str(type));
106 if (!lib_path.empty()) {
107 writer.AddKeyedString("library_path", lib_path.native());
108 }
109 writer.AddKeyedString("api_version", version_to_string(api_version));
110 writer.AddKeyedString("implementation_version", std::to_string(implementation_version));
111 writer.AddKeyedString("description", description);
112 print_object_of_t(writer, "functions", functions);
113 print_array_of_t(writer, "instance_extensions", instance_extensions);
114 print_array_of_t(writer, "device_extensions", device_extensions);
115 if (!enable_environment.empty()) {
116 writer.StartKeyedObject("enable_environment");
117 writer.AddKeyedString(enable_environment, "1");
118 writer.EndObject();
119 }
120 if (!disable_environment.empty()) {
121 writer.StartKeyedObject("disable_environment");
122 writer.AddKeyedString(disable_environment, "1");
123 writer.EndObject();
124 }
125 print_vector_of_strings(writer, "component_layers", component_layers);
126 print_vector_of_strings(writer, "blacklisted_layers", blacklisted_layers);
127 print_vector_of_strings(writer, "override_paths", override_paths);
128 print_vector_of_strings(writer, "app_keys", app_keys);
129 print_object_of_t(writer, "pre_instance_functions", pre_instance_functions);
130 if (!library_arch.empty()) {
131 writer.AddKeyedString("library_arch", library_arch);
132 }
133 }
134
135 VkLayerProperties ManifestLayer::LayerDescription::get_layer_properties() const {
136 VkLayerProperties properties{};
137 name.copy(properties.layerName, VK_MAX_EXTENSION_NAME_SIZE);
138 description.copy(properties.description, VK_MAX_EXTENSION_NAME_SIZE);
139 properties.implementationVersion = implementation_version;
140 properties.specVersion = api_version;
141 return properties;
142 }
143
144 std::string ManifestLayer::get_manifest_str() const {
145 JsonWriter writer;
146 writer.StartObject();
147 writer.AddKeyedString("file_format_version", file_format_version.get_version_str());
148 if (layers.size() == 1) {
149 writer.StartKeyedObject("layer");
150 layers.at(0).get_manifest_str(writer);
151 writer.EndObject();
152 } else {
153 writer.StartKeyedArray("layers");
154 for (size_t i = 0; i < layers.size(); i++) {
155 writer.StartObject();
156 layers.at(i).get_manifest_str(writer);
157 writer.EndObject();
158 }
159 writer.EndArray();
160 }
161 writer.EndObject();
162 return writer.output;
163 }