| /* | |
| * Copyright (C) 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include "DiscoveredLayerLibraries.h" | |
| #include <android-base/strings.h> | |
| #include <log/log.h> | |
| #include <cstdlib> | |
| #include "LayerEnvironment.h" | |
| namespace vulkan { | |
| namespace layers { | |
| void DiscoveredLayerLibraries::DiscoverLibraries() { | |
| // Temporarily workaround an issue by clearing any pre-populated knowledge | |
| // of system layers, done by Zygote (b/512640763). | |
| // | |
| // TODO: longer term, modify the code to reuse the SL that Zygote | |
| // pre-populated, and inherit them into this process. | |
| system_libs_.clear(); | |
| DiscoverImplicitLibraries(); | |
| DiscoverExplicitLibraries(); | |
| DiscoverSystemLibraries(); | |
| } | |
| void DiscoveredLayerLibraries::DiscoverImplicitLibraries() { | |
| // Implicit layers are only discoverable if one of the following is true: | |
| // 1) Android is built debuggable (i.e. "userdebug" or "eng" build) | |
| // 2) The application APK is built debuggable | |
| // 3) The application APK's manifest indicates that it is "injectable" | |
| bool isDebuggable = env_.IsDebuggable(); | |
| std::string prognameProp = env_.GetProperty("debug.vulkan.progname"); | |
| if (isDebuggable || | |
| (env_.IsOSDebuggable() && prognameProp == env_.GetProcessName())) { | |
| // Scan the graphics mainline module's directory: | |
| DiscoverLibrariesInPathList(kGraphicsMainlineModuleDir, implicit_libs_, | |
| LayerType::Implicit); | |
| // Scan the old kSystemDebugLayerLibraryDir directory: | |
| DiscoverLibrariesInPathList(kSystemDebugLayerLibraryDir, implicit_libs_, | |
| LayerType::Implicit); | |
| } | |
| // Scan the implicit layer paths from settings: | |
| if (isDebuggable) { | |
| DiscoverLibrariesInPathList(env_.GetImplicitLayerPaths(), | |
| implicit_libs_, LayerType::Implicit); | |
| } | |
| } | |
| void DiscoveredLayerLibraries::DiscoverExplicitLibraries() { | |
| DiscoverLibrariesInPathList(env_.GetExplicitLayerPaths(), explicit_libs_, | |
| LayerType::Explicit); | |
| } | |
| void DiscoveredLayerLibraries::DiscoverLibrariesInPathList( | |
| const std::string& paths_str, | |
| std::vector<std::shared_ptr<DiscoveredLayerLibrary>>& list, | |
| LayerType type) { | |
| if (paths_str.empty()) { | |
| return; | |
| } | |
| std::vector<std::string> paths = android::base::Split(paths_str, ":"); | |
| for (size_t i = 0; i < paths.size(); ++i) { | |
| const auto& path = paths[i]; | |
| if (path.empty()) | |
| continue; | |
| size_t zip_pos = path.find("!/"); | |
| if (zip_pos == std::string::npos) { | |
| // Regular directory | |
| ALOGD("Scanning directory: %s (classified as %s)", path.c_str(), | |
| type == LayerType::Explicit ? "Explicit" : "Implicit"); | |
| std::vector<std::string> files = env_.ListDirectory(path); | |
| for (const auto& file : files) { | |
| AddLibraryIfValid(path, file, list, type); | |
| } | |
| } else { | |
| // Inside ZIP/APK | |
| std::string zip_path = path.substr(0, zip_pos); | |
| std::string dir_in_zip = path.substr(zip_pos + 2); | |
| ALOGD("Scanning ZIP directory: %s!/%s (classified as %s)", | |
| zip_path.c_str(), dir_in_zip.c_str(), | |
| type == LayerType::Explicit ? "Explicit" : "Implicit"); | |
| std::vector<std::string> files = | |
| env_.ListZipDirectory(zip_path, dir_in_zip); | |
| for (const auto& file : files) { | |
| AddLibraryIfValid(path, file, list, type); | |
| } | |
| } | |
| } | |
| } | |
| void DiscoveredLayerLibraries::DiscoverSystemLibraries() { | |
| // Lambda function that does the discovery for a set of PSL, SSL, or OSL. | |
| // For each set, there can be a maximum of 10 properties, each of which | |
| // specifies a library-layer-order combination. Each library might have | |
| // more than one layer specified. The property specifies the order in which | |
| // these SLs should be opened and chained. In most cases, all PSLs will | |
| // come before SSLs, which will come before OSLs. However, the order allows | |
| // these sets of SLs to be intermingled. | |
| auto parse_set = [&](const std::string& prop_prefix, | |
| const std::string& directory) { | |
| for (int priority = 0; priority <= 9; priority++) { | |
| std::string prop_key = prop_prefix + "." + std::to_string(priority); | |
| std::string prop_val = env_.GetProperty(prop_key); | |
| if (prop_val.empty()) { | |
| continue; | |
| } | |
| std::vector<std::string> parts = | |
| android::base::Split(prop_val, ":"); | |
| if (parts.size() != 3) { | |
| ALOGW( | |
| "Invalid system layer property format for %s: %s (expected " | |
| "library_name:layer_name:order)", | |
| prop_key.c_str(), prop_val.c_str()); | |
| continue; | |
| } | |
| std::string library_name = parts[0]; | |
| std::string layer_name = parts[1]; | |
| char* endptr; | |
| long order_long = std::strtol(parts[2].c_str(), &endptr, 10); | |
| if (*endptr != '\0' || endptr == parts[2].c_str()) { | |
| ALOGW("Invalid order in system layer property %s: %s", | |
| prop_key.c_str(), parts[2].c_str()); | |
| continue; | |
| } | |
| int order = static_cast<int>(order_long); | |
| std::string full_path = directory + "/" + library_name; | |
| if (env_.FileExists(full_path)) { | |
| AddLibraryIfValid(directory, library_name, system_libs_, | |
| LayerType::System, layer_name, order); | |
| } else { | |
| ALOGW( | |
| "System layer library listed in prop %s but not found: %s", | |
| prop_key.c_str(), full_path.c_str()); | |
| } | |
| } | |
| }; | |
| bool isOSDebuggable = env_.IsOSDebuggable(); | |
| // Labmda function to determine whether the official or override SL | |
| // properties should be used for a given set of properties. The override SL | |
| // properties are only available if Android is debuggable (i.e. "userdebug" | |
| // or "eng" build), and can be used for: 1) testing the layer mechanism; 2) | |
| // developing and testing SLs. | |
| auto parse_set_with_override = [&](const std::string& set_name, | |
| const std::string& directory) { | |
| std::string ro_prefix = "ro.vulkan.layers." + set_name; | |
| std::string debug_prefix = "debug.vulkan.layers." + set_name; | |
| bool use_debug = false; | |
| if (isOSDebuggable) { | |
| // Check if any debug property is set for this set | |
| for (int priority = 0; priority <= 9; priority++) { | |
| std::string prop_key = | |
| debug_prefix + "." + std::to_string(priority); | |
| if (!env_.GetProperty(prop_key).empty()) { | |
| use_debug = true; | |
| break; | |
| } | |
| } | |
| } | |
| if (use_debug) { | |
| parse_set(debug_prefix, directory); | |
| } else { | |
| parse_set(ro_prefix, directory); | |
| } | |
| }; | |
| parse_set_with_override("platform", kSystemPlatformLayerLibraryDir); | |
| parse_set_with_override("soc", kSystemSoCLayerLibraryDir); | |
| parse_set_with_override("oem", kSystemOEMLayerLibraryDir); | |
| } | |
| void DiscoveredLayerLibraries::AddLibraryIfValid( | |
| const std::string& directory, | |
| const std::string& filename, | |
| std::vector<std::shared_ptr<DiscoveredLayerLibrary>>& list, | |
| LayerType type, | |
| const std::string& layer_name, | |
| int order) { | |
| if (android::base::StartsWith(filename, "libVkLayer") && | |
| android::base::EndsWith(filename, ".so")) { | |
| std::string full_path = directory + "/" + filename; | |
| if (type == LayerType::System) { | |
| // Determine if we've seen this system library path before (i.e. a | |
| // case where multiple layers are in the same library) | |
| for (const auto& lib : list) { | |
| if (lib->GetPath() == full_path) { | |
| // If we've seen a system-layer library before, add the new | |
| // layer name to the map of layers to use from that library | |
| ALOGD( | |
| "Adding system layer name \"%s\" with order %d to " | |
| "library " | |
| "%s)", | |
| layer_name.c_str(), order, full_path.c_str()); | |
| lib->AddLayer(order, layer_name); | |
| return; // Duplicate path/filename, skip | |
| } | |
| } | |
| } | |
| ALOGD("Discovered layer library: %s (Type: %s)", full_path.c_str(), | |
| type == LayerType::Explicit ? "Explicit" | |
| : type == LayerType::Implicit ? "Implicit" | |
| : "System"); | |
| auto lib = | |
| std::make_shared<DiscoveredLayerLibrary>(full_path, filename, type); | |
| if (type == LayerType::System) { | |
| // Since this is the first time we've seen this system-layer | |
| // library, add the specified layer name to the map of layers to use | |
| // from that library | |
| ALOGD( | |
| "Adding system layer name \"%s\" with order %d to library " | |
| "%s)", | |
| layer_name.c_str(), order, full_path.c_str()); | |
| lib->AddLayer(order, layer_name); | |
| } | |
| list.push_back(lib); | |
| } | |
| } | |
| } // namespace layers | |
| } // namespace vulkan |