|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: MIT |
| 3 | + * ---------------------------------------------------------------------------- |
| 4 | + * Copyright (c) 2025 Arm Limited |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to |
| 8 | + * deal in the Software without restriction, including without limitation the |
| 9 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS 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. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | + * IN THE SOFTWARE. |
| 23 | + * ---------------------------------------------------------------------------- |
| 24 | + */ |
| 25 | +#include <vulkan/utility/vk_safe_struct.hpp> |
| 26 | +#include <vulkan/utility/vk_struct_helper.hpp> |
| 27 | + |
| 28 | +#include "device.hpp" |
| 29 | +#include "framework/device_dispatch_table.hpp" |
| 30 | + |
| 31 | +#include <bit> |
| 32 | +#include <mutex> |
| 33 | + |
| 34 | +extern std::mutex g_vulkanLock; |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief Intercept vkAllocateMemory and hard-fail on external memory imports |
| 38 | + * that could mandate external compression; otherwise pass through to the driver. |
| 39 | + * |
| 40 | + * @details Scans @p pAllocateInfo->pNext for import structs. If it finds |
| 41 | + * VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID or |
| 42 | + * VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR with |
| 43 | + * VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, it returns |
| 44 | + * VK_ERROR_FEATURE_NOT_PRESENT to preserve the guarantee that |
| 45 | + * external compression is not used. When no such imports are present, the |
| 46 | + * call is forwarded unchanged. |
| 47 | + * |
| 48 | + * @param device Logical device allocating the memory. |
| 49 | + * @param pAllocateInfo Allocation parameters; its pNext chain is inspected |
| 50 | + * for import info. |
| 51 | + * @param pAllocator Optional allocation callbacks. |
| 52 | + * @param pMemory Receives the allocated device memory on success. |
| 53 | + * |
| 54 | + * @return VK_SUCCESS on success, or a VkResult propagated from the driver. |
| 55 | + * Returns VK_ERROR_FEATURE_NOT_PRESENT when disallowed external |
| 56 | + * import types are detected in the pNext chain. |
| 57 | + * |
| 58 | + * @note This is a strict policy check; additional import structures may be |
| 59 | + * rejected in the future to maintain the compression-off guarantee. |
| 60 | + */ |
| 61 | + |
| 62 | +/* See Vulkan API for documentation. */ |
| 63 | +template <> |
| 64 | +VKAPI_ATTR VkResult VKAPI_CALL layer_vkAllocateMemory<user_tag>( |
| 65 | + VkDevice device, |
| 66 | + const VkMemoryAllocateInfo* pAllocateInfo, |
| 67 | + const VkAllocationCallbacks* pAllocator, |
| 68 | + VkDeviceMemory* pMemory |
| 69 | +) { |
| 70 | + |
| 71 | + LAYER_TRACE(__func__); |
| 72 | + |
| 73 | + //fprintf(stderr, "[libGPULayers] HIT %s\n", __func__); fflush(stderr); |
| 74 | + |
| 75 | + std::unique_lock<std::mutex> lock { g_vulkanLock }; |
| 76 | + auto* layer = Device::retrieve(device); |
| 77 | + const auto& config = layer->instance->config; |
| 78 | + |
| 79 | + int disable_external_compression = config.disable_external_compression(); |
| 80 | + |
| 81 | + //absolute passthrough if feature is off |
| 82 | + if (disable_external_compression == 0) { |
| 83 | + lock.unlock(); |
| 84 | + return layer->driver.vkAllocateMemory(device, pAllocateInfo, pAllocator, pMemory); |
| 85 | + } |
| 86 | + |
| 87 | + // Scan pNext for imports that we cannot sanitize -> hard-fail to keep the 100% guarantee that we can disable external compression |
| 88 | + for (const VkBaseInStructure* n = (const VkBaseInStructure*)pAllocateInfo->pNext; n; n = n->pNext) { |
| 89 | + |
| 90 | + if (n->sType == VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID) { |
| 91 | + printf("[libGPULayers] vkAllocateMemory: AHardwareBuffer IMPORT detected. " |
| 92 | + "Cannot guarantee external compression is disabled (buffer created outside Vulkan). Failing by policy.\n"); |
| 93 | + return VK_ERROR_FEATURE_NOT_PRESENT; |
| 94 | + } |
| 95 | + |
| 96 | + if (n->sType == VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR) { |
| 97 | + const VkImportMemoryFdInfoKHR* fdInfo = (const VkImportMemoryFdInfoKHR*)n; |
| 98 | + if (fdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT) { |
| 99 | + printf("[libGPULayers] vkAllocateMemory: DMA-BUF memory IMPORT detected (fd=%d). " |
| 100 | + "Cannot guarantee external compression (DRM modifier may imply compression). Failing by policy.\n", |
| 101 | + fdInfo->fd); |
| 102 | + return VK_ERROR_FEATURE_NOT_PRESENT; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + // Release the lock to call into the driver |
| 109 | + lock.unlock(); |
| 110 | + return layer->driver.vkAllocateMemory(device, pAllocateInfo, pAllocator, pMemory); |
| 111 | + |
| 112 | +} |
0 commit comments