blob: 4da42359d98a0ac5096037fa9aeafa9f31c3d011 [file]
/*
* 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 "EnabledLayers.h"
#include <log/log.h>
#include "DiscoveredLayerLibrary.h"
#include "IdentifiedLayers.h"
#include "LayerEnvironment.h"
namespace vulkan {
namespace layers {
VkResult EnabledLayers::Build(const IdentifiedLayers& identified,
const char* const* requested_layers,
uint32_t requested_count,
const LayerEnvironment& env) {
active_layers_.clear();
// TODO / FIXME: Eliminate this Android 17-type behavior once
// IdentifiedLayers::FilterLayers() fully implements "proper layer
// filtering, as written in the layer mechanism specification.
auto is_already_active = [&](const std::string& name) {
for (const auto& active : active_layers_) {
if (active->GetLayer().GetName() == name) {
return true;
}
}
return false;
};
// 1. Add implicitly enabled layers (from settings/properties)
for (const auto* layer : identified.GetImplicitlyEnabledLayers()) {
if (!is_already_active(layer->GetName())) {
if (!AddActiveLayer(*layer, env)) {
return VK_ERROR_LAYER_NOT_PRESENT;
}
}
}
// 2. Add explicitly requested layers
for (uint32_t i = 0; i < requested_count; ++i) {
std::string requested_name(requested_layers[i]);
if (is_already_active(requested_name)) {
continue; // Skip, already enabled as implicit
}
// Paranoia checks: Vulkan applications request layer names to be
// explicitly enabled via vkCreateInstance(). We carefully vet the list
// given by vkCreateInstance(), so that only identified ILs and ELs can
// be explicitly enabled (no SLs). While most apps will behave properly
// by calling vkEnumerateInstanceLayerProperties() and returning a
// proper subset of those layer names with vkCreateInstance(), it is
// possible that apps won't call vkEnumerateInstanceLayerProperties(),
// or even if they do, they may provide layer names that would not have
// been returned by vkEnumerateInstanceLayerProperties().
// If the requested name is not found, treat it as an error
const auto* layer = identified.FindLayer(requested_name);
if (!layer) {
ALOGE("Requested layer %s not found", requested_name.c_str());
return VK_ERROR_LAYER_NOT_PRESENT;
}
// Since FindLayer() returns SLs, treat SLs as an error, and only
// proceed if the requested layer is an IL or EL that wasn't implicitly
// enabled
if (!layer->IsVisibleToApp() && !layer->IsImplicitlyEnabled()) {
ALOGE("Requested layer %s not found", requested_name.c_str());
return VK_ERROR_LAYER_NOT_PRESENT;
}
if (!AddActiveLayer(*layer, env)) {
return VK_ERROR_LAYER_NOT_PRESENT;
}
}
// 3. Add system layers
for (const auto* layer : identified.GetSystemLayers()) {
// SLs are in their own namespace, and any duplicate checks will be done
// by IdentifiedLayers::FilterLayers()
if (!AddActiveLayer(*layer, env)) {
return VK_ERROR_LAYER_NOT_PRESENT;
}
}
return VK_SUCCESS;
}
bool EnabledLayers::AddActiveLayer(const IdentifiedLayer& layer,
const LayerEnvironment& env) {
auto lib = layer.GetLibrary().Open(env);
if (!lib) {
ALOGE("Failed to open library for layer %s", layer.GetName().c_str());
return false;
}
active_layers_.push_back(
std::make_unique<ActiveLayer>(layer, std::move(lib)));
return true;
}
} // namespace layers
} // namespace vulkan