| /* | |
| * 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 "IdentifiedLayers.h" | |
| #include <android-base/strings.h> | |
| #include <log/log.h> | |
| #include <algorithm> | |
| #include <map> | |
| #include <unordered_set> | |
| #include "DiscoveredLayerLibraries.h" | |
| #include "LayerEnvironment.h" | |
| namespace vulkan { | |
| namespace layers { | |
| namespace { | |
| template <typename F> | |
| void ParseDebugVulkanLayerPriorityProperties(const LayerEnvironment& env, | |
| F add_fn) { | |
| static const char prefix[] = "debug.vulkan.layer."; | |
| static constexpr int kFixedRangeBeginInclusive = 0; | |
| static constexpr int kFixedRangeEndInclusive = 9; | |
| int priority = kFixedRangeBeginInclusive; | |
| while (true) { | |
| const std::string prop_key = | |
| std::string(prefix) + std::to_string(priority); | |
| const std::string prop_val = env.GetProperty(prop_key); | |
| if (!prop_val.empty()) { | |
| add_fn(prop_val); | |
| } else if (priority >= kFixedRangeEndInclusive) { | |
| break; | |
| } | |
| priority++; | |
| } | |
| } | |
| } // namespace | |
| IdentifiedLayers& IdentifiedLayers::GetInstance() { | |
| static IdentifiedLayers instance; | |
| return instance; | |
| } | |
| void IdentifiedLayers::DoOneTimeSetup() { | |
| // Get the environment and call the "testable" version of DoOneTimeSetup(). | |
| vulkan::layers::LayerEnvironment env; | |
| DoOneTimeSetup(env); | |
| } | |
| void IdentifiedLayers::DoOneTimeSetup(const LayerEnvironment& env) { | |
| std::lock_guard<std::mutex> lock(mutex_); | |
| if (initialized_) { | |
| return; | |
| } | |
| // Get the DiscoveredLayerLibraries class, and discover layers: | |
| libraries_ = std::make_unique<DiscoveredLayerLibraries>(env); | |
| libraries_->DiscoverLibraries(); | |
| // Identify the layers in those libraries: | |
| IdentifyLayers(env); | |
| // Do any necessary duplicate filtering of the identified layers, and | |
| // partition them into those to implicitly enable vs those to give to | |
| // vkEnumearateInstanceLayerProperties vs system layers: | |
| FilterLayers(env); | |
| if (env.IsZygoteProcess()) { | |
| // TODO: In Phase 2, we want to note that the SLs have been identified | |
| ALOGD("IdentifiedLayers::DoOneTimeSetup(): setup done for Zygote"); | |
| } else { | |
| // TODO: In Phase 2, we don't need this code path, as this function will | |
| // only be called once per process. Therefore, "initialized_" should | |
| // become "zygote_initialized_", and only be set to "true" in the "then" | |
| // branch of this if-then-else. | |
| initialized_ = true; | |
| } | |
| } | |
| std::vector<const IdentifiedLayer*> IdentifiedLayers::GetLayersForEnumeration() | |
| const { | |
| std::lock_guard<std::mutex> lock(mutex_); | |
| std::vector<const IdentifiedLayer*> result; | |
| for (const auto& layer : implicit_layers_) { | |
| if (layer->IsVisibleToApp()) | |
| result.push_back(layer.get()); | |
| } | |
| for (const auto& layer : explicit_layers_) { | |
| if (layer->IsVisibleToApp()) | |
| result.push_back(layer.get()); | |
| } | |
| return result; | |
| } | |
| // TODO: When fully implementing the Android 18 specification, the list of | |
| // implicitly-enabled layers should be built during FilterLayers(), so that this | |
| // method only needs to return that list. | |
| std::vector<const IdentifiedLayer*> | |
| IdentifiedLayers::GetImplicitlyEnabledLayers() const { | |
| std::lock_guard<std::mutex> lock(mutex_); | |
| std::vector<const IdentifiedLayer*> result; | |
| for (const auto& name : ordered_implicit_names_) { | |
| const auto* layer = FindLayerLocked(name); | |
| if (layer && layer->IsImplicitlyEnabled()) { | |
| result.push_back(layer); | |
| } | |
| } | |
| return result; | |
| } | |
| std::vector<const IdentifiedLayer*> IdentifiedLayers::GetSystemLayers() const { | |
| std::lock_guard<std::mutex> lock(mutex_); | |
| std::vector<const IdentifiedLayer*> result; | |
| for (const auto& layer : system_layers_) { | |
| result.push_back(layer.get()); | |
| } | |
| return result; | |
| } | |
| const IdentifiedLayer* IdentifiedLayers::FindLayerLocked( | |
| const std::string& name) const { | |
| for (const auto& layer : implicit_layers_) { | |
| if (layer->GetName() == name) | |
| return layer.get(); | |
| } | |
| for (const auto& layer : explicit_layers_) { | |
| if (layer->GetName() == name) | |
| return layer.get(); | |
| } | |
| // TODO: When fully implementing the Android 18 specification, ensure that | |
| // SLs are not accidentally leaked to applications. I don't like that this | |
| // method will look at system_layers_, in part because FindLayer() calls | |
| // this and doesn't do any filtering of its output like | |
| // GetImplicitlyEnabledLayers() does. There aren't many calls to FindLayer() | |
| // yet, but I don't want SLs to accidentally be leaked to applications. | |
| for (const auto& layer : system_layers_) { | |
| if (layer->GetName() == name) | |
| return layer.get(); | |
| } | |
| return nullptr; | |
| } | |
| // TODO: When fully implementing the Android 18 specification, ensure that | |
| // SLs are not accidentally leaked to applications. | |
| // | |
| // Note: This method is used by both tests (that need to see SLs) and methods | |
| // that implement the layer mechanism specification. | |
| const IdentifiedLayer* IdentifiedLayers::FindLayer( | |
| const std::string& name) const { | |
| std::lock_guard<std::mutex> lock(mutex_); | |
| return FindLayerLocked(name); | |
| } | |
| void IdentifiedLayers::IdentifyLayers(const LayerEnvironment& env) { | |
| // Identify the layers inside of the explicit-layer libraries | |
| for (const auto& lib : libraries_->GetExplicitLibraries()) { | |
| std::shared_ptr<DynamicLibrary> dynamic_lib = lib->Open(env); | |
| if (dynamic_lib) { | |
| IdentifyLayersInLibrary(lib, dynamic_lib, explicit_layers_); | |
| } else { | |
| ALOGW( | |
| "IdentifiedLayers::IdentifyLayers(): Failed to open " | |
| "library=%s", | |
| lib->GetFilename().c_str()); | |
| } | |
| } | |
| // Identify the layers inside of the implicit-layer libraries | |
| for (const auto& lib : libraries_->GetImplicitLibraries()) { | |
| std::shared_ptr<DynamicLibrary> dynamic_lib = lib->Open(env); | |
| if (dynamic_lib) { | |
| IdentifyLayersInLibrary(lib, dynamic_lib, implicit_layers_); | |
| } else { | |
| ALOGW( | |
| "IdentifiedLayers::IdentifyLayers(): Failed to open " | |
| "library=%s", | |
| lib->GetFilename().c_str()); | |
| } | |
| } | |
| // Identify the layers inside of the system-layer libraries, using the | |
| // GetLayerNames() from each library for the layer names that can be used | |
| // from that library. | |
| for (const auto& lib : libraries_->GetSystemLibraries()) { | |
| std::shared_ptr<DynamicLibrary> dynamic_lib = lib->Open(env); | |
| if (!dynamic_lib) { | |
| ALOGW( | |
| "IdentifiedLayers::IdentifyLayers(): Failed to open " | |
| "library=%s", | |
| lib->GetFilename().c_str()); | |
| continue; | |
| } | |
| IdentifyLayersInLibrary(lib, dynamic_lib, system_layers_, | |
| lib->GetLayerNames()); | |
| } | |
| std::stable_sort(system_layers_.begin(), system_layers_.end(), | |
| [](const std::unique_ptr<IdentifiedLayer>& a, | |
| const std::unique_ptr<IdentifiedLayer>& b) { | |
| return a->GetOrder() < b->GetOrder(); | |
| }); | |
| } | |
| // TODO: Fully implement the Android 18 layer mechanism specification. This | |
| // involves removing support for device layers. When doing so, also change the | |
| // corresponding "api.cpp" code. | |
| void IdentifiedLayers::IdentifyLayersInLibrary( | |
| std::shared_ptr<const DiscoveredLayerLibrary> library, | |
| std::shared_ptr<DynamicLibrary> lib, | |
| std::vector<std::unique_ptr<IdentifiedLayer>>& target_list, | |
| const std::multimap<int, std::string>& layer_names) { | |
| if (!lib) { | |
| return; | |
| } | |
| auto enumerate_instance_layers = | |
| reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>( | |
| lib->GetSymbol("vkEnumerateInstanceLayerProperties")); | |
| auto enumerate_instance_extensions = | |
| reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>( | |
| lib->GetSymbol("vkEnumerateInstanceExtensionProperties")); | |
| if (!enumerate_instance_layers || !enumerate_instance_extensions) { | |
| ALOGE("Layer library '%s' missing some instance enumeration functions", | |
| library->GetPath().c_str()); | |
| return; | |
| } | |
| // device functions are optional | |
| auto enumerate_device_extensions = | |
| reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>( | |
| lib->GetSymbol("vkEnumerateDeviceExtensionProperties")); | |
| auto enumerate_device_layers = | |
| reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>( | |
| lib->GetSymbol("vkEnumerateDeviceLayerProperties")); | |
| uint32_t num_layers = 0; | |
| VkResult result = enumerate_instance_layers(&num_layers, nullptr); | |
| if (result != VK_SUCCESS || num_layers == 0) { | |
| return; | |
| } | |
| std::vector<VkLayerProperties> properties(num_layers); | |
| result = enumerate_instance_layers(&num_layers, properties.data()); | |
| if (result != VK_SUCCESS) { | |
| ALOGE("vkEnumerateInstanceLayerProperties failed for %s: %d", | |
| library->GetPath().c_str(), result); | |
| return; | |
| } | |
| ALOGD("Layer library \"%s\": has %d layers", library->GetFilename().c_str(), | |
| num_layers); | |
| uint32_t num_device_layers = 0; | |
| std::vector<VkLayerProperties> device_properties; | |
| if (enumerate_device_layers) { | |
| result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers, | |
| nullptr); | |
| if (result == VK_SUCCESS && num_device_layers > 0) { | |
| device_properties.resize(num_device_layers); | |
| result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers, | |
| device_properties.data()); | |
| if (result != VK_SUCCESS) { | |
| num_device_layers = 0; | |
| device_properties.clear(); | |
| } | |
| } | |
| } | |
| for (const auto& props : properties) { | |
| int order = 0; | |
| if (!layer_names.empty()) { | |
| bool found = false; | |
| for (const auto& [ord, name] : layer_names) { | |
| if (name == props.layerName) { | |
| found = true; | |
| order = ord; | |
| break; | |
| } | |
| } | |
| if (!found) { | |
| continue; | |
| } | |
| } | |
| ALOGD("\t Considering layer named \"%s\"", props.layerName); | |
| auto identified_layer = | |
| std::make_unique<IdentifiedLayer>(props, library, order); | |
| bool is_global = false; | |
| if (enumerate_device_layers) { | |
| for (const auto& dev_props : device_properties) { | |
| if (strcmp(props.layerName, dev_props.layerName) == 0) { | |
| is_global = true; | |
| break; | |
| } | |
| } | |
| } else { | |
| is_global = true; | |
| } | |
| identified_layer->SetGlobal(is_global); | |
| // Instance extensions | |
| if (enumerate_instance_extensions) { | |
| uint32_t count = 0; | |
| result = | |
| enumerate_instance_extensions(props.layerName, &count, nullptr); | |
| if (result == VK_SUCCESS && count > 0) { | |
| std::vector<VkExtensionProperties> ext_props(count); | |
| result = enumerate_instance_extensions(props.layerName, &count, | |
| ext_props.data()); | |
| if (result == VK_SUCCESS) { | |
| for (const auto& ext : ext_props) { | |
| identified_layer->AddInstanceExtension(ext); | |
| } | |
| } | |
| } | |
| } | |
| // Device extensions (treat all as global) | |
| if (enumerate_device_extensions) { | |
| uint32_t count = 0; | |
| result = enumerate_device_extensions( | |
| VK_NULL_HANDLE, props.layerName, &count, nullptr); | |
| if (result == VK_SUCCESS && count > 0) { | |
| std::vector<VkExtensionProperties> ext_props(count); | |
| result = enumerate_device_extensions( | |
| VK_NULL_HANDLE, props.layerName, &count, ext_props.data()); | |
| if (result == VK_SUCCESS) { | |
| for (const auto& ext : ext_props) { | |
| identified_layer->AddDeviceExtension(ext); | |
| } | |
| } | |
| } | |
| } | |
| ALOGD("Identified layer %s in library %s", props.layerName, | |
| library->GetPath().c_str()); | |
| target_list.push_back(std::move(identified_layer)); | |
| } | |
| } | |
| // TODO: Fully implement the Android 18 layer mechanism specification | |
| void IdentifiedLayers::FilterLayers(const LayerEnvironment& env) { | |
| // 1. Resolve duplicates across all lists, prioritizing Implicit > Explicit. | |
| // The above statement is true to the Android 17 behavior, but for | |
| // Android 18, we want System layers to be in their own "namespace" so to | |
| // speak. | |
| std::unordered_set<std::string> seen_names; | |
| auto filter_list = | |
| [&](std::vector<std::unique_ptr<IdentifiedLayer>>& list) { | |
| auto it = std::remove_if( | |
| list.begin(), list.end(), | |
| [&](const std::unique_ptr<IdentifiedLayer>& layer) { | |
| if (seen_names.count(layer->GetName()) > 0) { | |
| ALOGD("Removing duplicate layer %s from %s", | |
| layer->GetName().c_str(), | |
| layer->GetLibrary().GetPath().c_str()); | |
| return true; // Duplicate, remove | |
| } | |
| seen_names.insert(layer->GetName()); | |
| return false; // Keep | |
| }); | |
| list.erase(it, list.end()); | |
| }; | |
| filter_list(implicit_layers_); | |
| filter_list(explicit_layers_); | |
| // 2. Resolve visibility and implicit enablement | |
| ResolveVisibilityAndImplicitEnablement(env); | |
| } | |
| void IdentifiedLayers::ResolveVisibilityAndImplicitEnablement( | |
| const LayerEnvironment& env) { | |
| ordered_implicit_names_.clear(); | |
| std::unordered_set<std::string> implicit_enabled_names; | |
| auto add_implicit_name = [&](const std::string& name) { | |
| if (name.empty()) | |
| return; | |
| if (implicit_enabled_names.insert(name).second) { | |
| ordered_implicit_names_.push_back(name); | |
| } | |
| }; | |
| bool isDebuggable = env.IsDebuggable(); | |
| bool isOSDebuggable = env.IsOSDebuggable(); | |
| std::string prognameProp = env.GetProperty("debug.vulkan.progname"); | |
| std::string processName = env.GetProcessName(); | |
| bool processSpecificPropertiesApply = | |
| isOSDebuggable && !prognameProp.empty() && prognameProp == processName; | |
| bool use_properties = false; | |
| bool use_settings = false; | |
| std::string debug_layers; | |
| if (processSpecificPropertiesApply) { | |
| use_properties = true; | |
| } else if (isDebuggable) { | |
| debug_layers = env.GetDebugLayers(); | |
| if (!debug_layers.empty()) { | |
| use_settings = true; | |
| } else { | |
| use_properties = true; | |
| } | |
| } | |
| if (use_properties) { | |
| std::string prop_layers = env.GetProperty("debug.vulkan.layers"); | |
| if (!prop_layers.empty()) { | |
| std::vector<std::string> names = | |
| android::base::Split(prop_layers, ":"); | |
| for (const auto& name : names) { | |
| add_implicit_name(name); | |
| } | |
| } else { | |
| ParseDebugVulkanLayerPriorityProperties(env, add_implicit_name); | |
| } | |
| } else if (use_settings) { | |
| std::vector<std::string> names = | |
| android::base::Split(debug_layers, ":"); | |
| for (const auto& name : names) { | |
| add_implicit_name(name); | |
| } | |
| } | |
| auto update_state = [&](std::vector<std::unique_ptr<IdentifiedLayer>>& list, | |
| bool is_system) { | |
| for (auto& layer : list) { | |
| if (is_system) { | |
| // System layers are always implicitly enabled (in the sense | |
| // they are always in the chain) but we don't mark them as | |
| // settings-enabled to keep them separate for ordering. | |
| layer->SetImplicitlyEnabled(false); | |
| layer->SetVisibleToApp(false); | |
| } else { | |
| bool is_enabled = | |
| (implicit_enabled_names.count(layer->GetName()) > 0); | |
| layer->SetImplicitlyEnabled(is_enabled); | |
| layer->SetVisibleToApp(true); | |
| } | |
| } | |
| }; | |
| update_state(implicit_layers_, false); | |
| update_state(explicit_layers_, false); | |
| update_state(system_layers_, true); | |
| } | |
| } // namespace layers | |
| } // namespace vulkan |