|
| 1 | +/* |
| 2 | + * Zalith Launcher 2 |
| 3 | + * Copyright (C) 2025 MovTery <movtery228@qq.com> and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | + * See the GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.movtery.zalithlauncher.utils.device |
| 20 | + |
| 21 | +import android.util.Log |
| 22 | +import androidx.annotation.Keep |
| 23 | + |
| 24 | +@Keep |
| 25 | +data class VulkanCapabilities( |
| 26 | + val apiVersionMajor: Int, |
| 27 | + val apiVersionMinor: Int, |
| 28 | + val apiVersionPatch: Int, |
| 29 | + val extensions: List<String>, |
| 30 | + val features: Map<String, Boolean> |
| 31 | +) { |
| 32 | + /** 检查 Vulkan 版本是否至少为 1.2 */ |
| 33 | + val isVersionSupported: Boolean |
| 34 | + get() = apiVersionMajor > 1 || (apiVersionMajor == 1 && apiVersionMinor >= 2) |
| 35 | + |
| 36 | + /** 返回设备缺失的必要扩展 */ |
| 37 | + val missingExtensions: List<String> |
| 38 | + get() = REQUIRED_EXTENSIONS.filter { it !in extensions } |
| 39 | + |
| 40 | + /** 返回设备不支持的必要功能 */ |
| 41 | + val missingFeatures: List<String> |
| 42 | + get() = REQUIRED_FEATURES.filter { features[it] != true } |
| 43 | + |
| 44 | + /** 设备是否满足所有需求 */ |
| 45 | + val isAllSupported: Boolean |
| 46 | + get() = isVersionSupported && missingExtensions.isEmpty() && missingFeatures.isEmpty() |
| 47 | + |
| 48 | + companion object { |
| 49 | + val REQUIRED_EXTENSIONS = listOf( |
| 50 | + "VK_KHR_dynamic_rendering", |
| 51 | + "VK_KHR_push_descriptor", |
| 52 | + "VK_KHR_synchronization2", |
| 53 | + "VK_EXT_vertex_attribute_divisor", |
| 54 | + "VK_KHR_swapchain" |
| 55 | + ) |
| 56 | + |
| 57 | + val REQUIRED_FEATURES = listOf( |
| 58 | + "multiDrawIndirect", |
| 59 | + "fillModeNonSolid", |
| 60 | + "samplerAnisotropy", |
| 61 | + "shaderDrawParameters", |
| 62 | + "timelineSemaphore", |
| 63 | + "hostQueryReset", |
| 64 | + "synchronization2", |
| 65 | + "dynamicRendering", |
| 66 | + "vertexAttributeInstanceRateDivisor" |
| 67 | + ) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +@Keep |
| 72 | +fun interface VulkanLogCallback { |
| 73 | + fun log(level: String, message: String) |
| 74 | +} |
| 75 | + |
| 76 | +object VulkanChecker { |
| 77 | + private const val TAG = "VulkanChecker" |
| 78 | + |
| 79 | + init { |
| 80 | + try { |
| 81 | + System.loadLibrary("vulkan_check") |
| 82 | + nativeSetLogCallback { level, msg -> |
| 83 | + when (level) { |
| 84 | + "INFO" -> Log.i(TAG, msg) |
| 85 | + "WARN" -> Log.w(TAG, msg) |
| 86 | + "ERROR" -> Log.e(TAG, msg) |
| 87 | + else -> Log.d(TAG, msg) |
| 88 | + } |
| 89 | + } |
| 90 | + } catch (e: UnsatisfiedLinkError) { |
| 91 | + Log.e(TAG, "Failed to load vulkan_check library", e) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * 查询系统 Vulkan 支持情况 |
| 97 | + * @return 如果不支持 Vulkan 或初始化失败,返回 null |
| 98 | + */ |
| 99 | + fun checkCapabilities(): VulkanCapabilities? { |
| 100 | + return try { |
| 101 | + nativeCheckVulkan()?.also { caps -> |
| 102 | + Log.i(TAG, "Vulkan version: ${caps.apiVersionMajor}.${caps.apiVersionMinor}.${caps.apiVersionPatch}") |
| 103 | + Log.i(TAG, "Version >= 1.2: ${caps.isVersionSupported}") |
| 104 | + if (caps.missingExtensions.isNotEmpty()) { |
| 105 | + Log.w(TAG, "Missing required extensions: ${caps.missingExtensions}") |
| 106 | + } |
| 107 | + if (caps.missingFeatures.isNotEmpty()) { |
| 108 | + Log.w(TAG, "Missing required features: ${caps.missingFeatures}") |
| 109 | + } |
| 110 | + Log.i(TAG, "All requirements satisfied: ${caps.isAllSupported}") |
| 111 | + } |
| 112 | + } catch (e: UnsatisfiedLinkError) { |
| 113 | + Log.e(TAG, "Native library or method not found", e) |
| 114 | + null |
| 115 | + } catch (e: Exception) { |
| 116 | + Log.e(TAG, "Native check failed", e) |
| 117 | + null |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @JvmStatic |
| 122 | + private external fun nativeSetLogCallback(callback: VulkanLogCallback) |
| 123 | + |
| 124 | + @JvmStatic |
| 125 | + private external fun nativeCheckVulkan(): VulkanCapabilities? |
| 126 | +} |
0 commit comments