blob: f42e9f78e8773d31e14644818706ed6129d3ad56 [file]
/*
* Copyright 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 <cstring>
#include <mutex>
#include <unordered_map>
#include <android/log.h>
#include <vulkan/vk_layer.h>
#include <vulkan/vulkan.h>
#ifndef LAYER_NAME
#define LAYER_NAME "VK_LAYER_ANDROID_sample"
#endif
#ifndef LAYER_DESCRIPTION
#define LAYER_DESCRIPTION "Android Vulkan Sample Layer"
#endif
#ifndef LOG_TAG
#define LOG_TAG "VkSampleLayer_APK"
#endif
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define VK_LAYER_EXPORT __attribute__((visibility("default"))) extern "C"
#define GETPROCADDR(func) \
if (!std::strcmp(pName, "vk" #func)) \
return (PFN_vkVoidFunction) & vk##func;
#define GETLAYERPROCADDR(func) \
if (!std::strcmp(pName, "vk" #func)) \
return (PFN_vkVoidFunction) & VkSample_##func;
struct InstanceData {
PFN_vkGetInstanceProcAddr getInstanceProcAddr;
PFN_vkDestroyInstance destroyInstance;
PFN_vkEnumerateDeviceExtensionProperties enumerateDeviceExtensionProperties;
};
struct DeviceData {
PFN_vkGetDeviceProcAddr getDeviceProcAddr;
PFN_vkDestroyDevice destroyDevice;
};
static std::mutex g_lock;
static std::unordered_map<void*, InstanceData> g_instance_data;
static std::unordered_map<void*, DeviceData> g_device_data;
// Helper to get the dispatch key from a Vulkan object
template <typename T>
void* GetKey(T object) {
return *reinterpret_cast<void**>(object);
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
vkCreateDevice(VkPhysicalDevice physicalDevice,
const VkDeviceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkDevice* pDevice) {
PFN_vkGetInstanceProcAddr gipa = nullptr;
PFN_vkGetDeviceProcAddr gdpa = nullptr;
auto* layerCreateInfo = reinterpret_cast<VkLayerDeviceCreateInfo*>(
const_cast<void*>(pCreateInfo->pNext));
while (layerCreateInfo &&
(layerCreateInfo->sType !=
VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO ||
layerCreateInfo->function != VK_LAYER_LINK_INFO)) {
layerCreateInfo = reinterpret_cast<VkLayerDeviceCreateInfo*>(
const_cast<void*>(layerCreateInfo->pNext));
}
// Vulkan device layers were deprecated "Early in Vulkan’s life". However,
// Android does not populate VkLayerDeviceCreateInfo unless the layer has
// advertised itself as both a device and instance layer. Alternatively, use
// this workaround to get the
// PFN_vkGetInstanceProcAddr/PFN_vkGetInstanceProcAddr for the VkInstance
// associated with this VkPhysicalDevice.
if (layerCreateInfo == nullptr) {
LOGE("VkLayerDeviceCreateInfo not found in pNext.");
return VK_ERROR_INITIALIZATION_FAILED;
}
gipa = layerCreateInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
gdpa = layerCreateInfo->u.pLayerInfo->pfnNextGetDeviceProcAddr;
layerCreateInfo->u.pLayerInfo = layerCreateInfo->u.pLayerInfo->pNext;
if (gipa == nullptr || gdpa == nullptr) {
LOGE("Failed to find valid procurement functions for next layer");
return VK_ERROR_INITIALIZATION_FAILED;
}
auto createDevice = reinterpret_cast<PFN_vkCreateDevice>(
gipa(VK_NULL_HANDLE, "vkCreateDevice"));
VkResult result =
createDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
if (result != VK_SUCCESS) {
LOGE("createDevice() error: %d", result);
return result;
}
DeviceData data{};
data.getDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
gdpa(*pDevice, "vkGetDeviceProcAddr"));
data.destroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
gdpa(*pDevice, "vkDestroyDevice"));
{
std::scoped_lock lock(g_lock);
g_device_data[GetKey(*pDevice)] = data;
}
LOGI("Device initialized successfully. Layer initialization complete.");
return result;
}
VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
VkSample_DestroyDevice(VkDevice device,
const VkAllocationCallbacks* pAllocator) {
PFN_vkDestroyDevice destroyDevice = nullptr;
{
std::scoped_lock lock(g_lock);
auto it = g_device_data.find(GetKey(device));
if (it != g_device_data.end()) {
destroyDevice = it->second.destroyDevice;
g_device_data.erase(it);
}
}
if (destroyDevice) {
destroyDevice(device, pAllocator);
}
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance) {
auto* layerCreateInfo = reinterpret_cast<VkLayerInstanceCreateInfo*>(
const_cast<void*>(pCreateInfo->pNext));
while (layerCreateInfo &&
(layerCreateInfo->sType !=
VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
layerCreateInfo->function != VK_LAYER_LINK_INFO)) {
layerCreateInfo = reinterpret_cast<VkLayerInstanceCreateInfo*>(
const_cast<void*>(layerCreateInfo->pNext));
}
if (layerCreateInfo == nullptr) {
LOGE("VkLayerInstanceCreateInfo not found in pNext chain.");
return VK_ERROR_INITIALIZATION_FAILED;
}
PFN_vkGetInstanceProcAddr gipa =
layerCreateInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
layerCreateInfo->u.pLayerInfo = layerCreateInfo->u.pLayerInfo->pNext;
auto createInstance = reinterpret_cast<PFN_vkCreateInstance>(
gipa(VK_NULL_HANDLE, "vkCreateInstance"));
VkResult result = createInstance(pCreateInfo, pAllocator, pInstance);
if (result == VK_SUCCESS) {
InstanceData data{};
data.getInstanceProcAddr = gipa;
data.destroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
gipa(*pInstance, "vkDestroyInstance"));
data.enumerateDeviceExtensionProperties =
reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
gipa(*pInstance, "vkEnumerateDeviceExtensionProperties"));
{
std::scoped_lock lock(g_lock);
g_instance_data[GetKey(*pInstance)] = data;
}
LOGI("Instance initialized successfully.");
} else {
LOGE("createInstance() error: %d", result);
}
return result;
}
VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
VkSample_DestroyInstance(VkInstance instance,
const VkAllocationCallbacks* pAllocator) {
PFN_vkDestroyInstance destroyInstance = nullptr;
{
std::scoped_lock lock(g_lock);
auto it = g_instance_data.find(GetKey(instance));
if (it != g_instance_data.end()) {
destroyInstance = it->second.destroyInstance;
g_instance_data.erase(it);
}
}
if (destroyInstance) {
destroyInstance(instance, pAllocator);
}
}
extern "C" {
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
vkEnumerateDeviceLayerProperties(VkPhysicalDevice /*physicalDevice*/,
uint32_t* pPropertyCount,
VkLayerProperties* pProperties) {
// Vulkan device layers were deprecated "Early in Vulkan’s life". However,
// Android does not set up the layer correctly and populate
// VkLayerDeviceCreateInfo unless the layer has advertised itself as both a
// device and instance layer.
if (pPropertyCount != nullptr) {
*pPropertyCount = 1;
}
if (pProperties != nullptr) {
std::strcpy(pProperties->layerName, LAYER_NAME);
pProperties->specVersion = VK_API_VERSION_1_1;
pProperties->implementationVersion = 1;
std::strcpy(pProperties->description, LAYER_DESCRIPTION);
}
return VK_SUCCESS;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
const char* pLayerName,
uint32_t* pPropertyCount,
VkExtensionProperties* pProperties) {
// Android is super weird about layers exposing extensions. pLayerName ==
// null and we're expected to filter or splice the extension list from the
// next layer (or the driver if we're the last layer)
if (pLayerName == nullptr || strcmp(pLayerName, LAYER_NAME)) {
if (physicalDevice == nullptr) {
LOGI("No physical device: Android quirk");
return VK_SUCCESS;
}
// otherwise call down.
PFN_vkEnumerateDeviceExtensionProperties pfn;
{
std::scoped_lock lock(g_lock);
auto it = g_instance_data.find(GetKey(physicalDevice));
if (it == g_instance_data.end()) {
LOGI("Not a real instance!? No idea what to call");
return VK_SUCCESS;
}
pfn = it->second.enumerateDeviceExtensionProperties;
}
VkResult res =
pfn(physicalDevice, pLayerName, pPropertyCount, pProperties);
return res;
}
*pPropertyCount = 0u;
return VK_SUCCESS;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
vkEnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
VkLayerProperties* pProperties) {
if (pPropertyCount != nullptr) {
*pPropertyCount = 1;
}
if (pProperties != nullptr) {
std::strcpy(pProperties->layerName, LAYER_NAME);
pProperties->specVersion = VK_API_VERSION_1_1;
pProperties->implementationVersion = 1;
std::strcpy(pProperties->description, LAYER_DESCRIPTION);
}
return VK_SUCCESS;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
vkEnumerateInstanceExtensionProperties(
const char* pLayerName,
uint32_t* pPropertyCount,
VkExtensionProperties* /* pProperties */) {
if (pLayerName && std::strcmp(pLayerName, LAYER_NAME) == 0) {
*pPropertyCount = 0;
return VK_SUCCESS;
}
return VK_ERROR_LAYER_NOT_PRESENT;
}
VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vkGetInstanceProcAddr(VkInstance instance, const char* pName) {
GETPROCADDR(CreateDevice);
GETPROCADDR(GetInstanceProcAddr);
GETPROCADDR(GetDeviceProcAddr);
GETPROCADDR(EnumerateDeviceLayerProperties);
GETPROCADDR(EnumerateDeviceExtensionProperties);
GETPROCADDR(EnumerateInstanceLayerProperties);
GETPROCADDR(EnumerateInstanceExtensionProperties);
GETPROCADDR(CreateInstance);
GETLAYERPROCADDR(DestroyInstance);
GETLAYERPROCADDR(DestroyDevice);
if (instance == VK_NULL_HANDLE)
return nullptr;
PFN_vkGetInstanceProcAddr pfn = nullptr;
{
std::scoped_lock lock(g_lock);
if (auto it = g_instance_data.find(GetKey(instance));
it != g_instance_data.end()) {
pfn = it->second.getInstanceProcAddr;
}
}
if (pfn)
return pfn(instance, pName);
return nullptr;
}
VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vkGetDeviceProcAddr(VkDevice device, const char* pName) {
GETPROCADDR(GetDeviceProcAddr);
GETLAYERPROCADDR(DestroyDevice);
PFN_vkGetDeviceProcAddr pfn = nullptr;
{
std::scoped_lock lock(g_lock);
if (auto it = g_device_data.find(GetKey(device));
it != g_device_data.end()) {
pfn = it->second.getDeviceProcAddr;
}
}
if (pfn)
return pfn(device, pName);
return nullptr;
}
} // extern "C"