DevCheck-lib/GpuInfo使用示例.md
2025-12-24 11:11:29 +08:00

525 lines
14 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# GpuInfo 使用示例
## 概述
GpuInfo类提供了获取设备GPU图形处理器详细信息的功能包括Vulkan和OpenGL ES的硬件信息如供应商、型号、显存、API版本、扩展等。
## 主要功能
1. 获取GPU供应商AMD、ARM、Qualcomm、Mali等
2. 获取GPU型号Adreno 650、Mali-G78等
3. Vulkan API支持检测和版本信息
4. OpenGL ES版本和扩展信息
5. EGL版本和扩展信息
6. GPU设备类型集成显卡、独立显卡等
7. 驱动版本信息
## 使用方法
### 1. 初始化AndInfo
```kotlin
import com.xyzshell.andinfo.AndInfo
// 在Application或Activity中初始化
AndInfo.init(applicationContext)
```
### 2. 获取GpuInfo实例
```kotlin
val gpuInfo = AndInfo.instance.gpu
```
### 3. 获取GPU基本信息
#### 供应商信息
```kotlin
// 获取GPU供应商名称
val vendorName = gpuInfo.getVendorName()
println("GPU供应商: $vendorName")
// 输出示例: "GPU供应商: Qualcomm" 或 "GPU供应商: ARM"
```
#### GPU型号
```kotlin
// 获取GPU型号/渲染器名称
val rendererName = gpuInfo.getRendererName()
println("GPU型号: $rendererName")
// 输出示例: "GPU型号: Adreno (TM) 650" 或 "GPU型号: Mali-G78"
```
#### 设备类型
```kotlin
// 获取GPU设备类型
val deviceType = gpuInfo.getDeviceType()
val deviceTypeDesc = gpuInfo.getDeviceTypeDescription()
println("设备类型: $deviceTypeDesc")
// 输出示例: "设备类型: 集成显卡"
```
### 4. Vulkan 信息
#### Vulkan支持检测
```kotlin
// 检查是否支持Vulkan API
val isVulkanSupported = gpuInfo.isVulkanSupported()
println("Vulkan支持: ${if (isVulkanSupported) "是" else "否"}")
```
#### Vulkan API版本
```kotlin
// 获取Vulkan API版本
val vulkanApiVersion = gpuInfo.getVulkanApiVersion()
vulkanApiVersion?.let { version ->
println("Vulkan API版本: $version")
// 输出示例: "Vulkan API版本: 1.3.0"
}
```
#### Vulkan驱动版本
```kotlin
// 获取Vulkan驱动版本
val driverVersion = gpuInfo.getVulkanDriverVersion()
driverVersion?.let { version ->
println("Vulkan驱动版本: $version")
// 输出示例: "Vulkan驱动版本: 512459776"
}
```
### 5. OpenGL ES 信息
#### OpenGL ES版本
```kotlin
// 获取OpenGL ES版本
val openglVersion = gpuInfo.getOpenGLVersion()
openglVersion?.let { version ->
println("OpenGL ES版本: $version")
// 输出示例: "OpenGL ES版本: OpenGL ES 3.2 V@0490.0"
}
```
#### OpenGL ES扩展
```kotlin
// 获取OpenGL ES扩展列表
val openglExtensions = gpuInfo.getOpenGLExtensions()
openglExtensions?.let { extensions ->
println("OpenGL ES扩展:")
// 扩展字符串通常很长,以空格分隔
val extList = extensions.split(" ").filter { it.isNotEmpty() }
extList.take(10).forEach { ext ->
println(" - $ext")
}
println(" ...")
}
// 获取扩展数量
val extCount = gpuInfo.getOpenGLExtensionCount()
println("OpenGL ES扩展数量: $extCount")
// 输出示例: "OpenGL ES扩展数量: 156"
```
### 6. EGL 信息
#### EGL版本
```kotlin
// 获取EGL版本
val eglVersion = gpuInfo.getEglVersion()
eglVersion?.let { version ->
println("EGL版本: $version")
// 输出示例: "EGL版本: 1.5"
}
```
#### EGL扩展
```kotlin
// 获取EGL扩展列表
val eglExtensions = gpuInfo.getEglExtensions()
eglExtensions?.let { extensions ->
println("EGL扩展:")
extensions.take(10).forEach { ext ->
println(" - $ext")
}
println(" ...")
}
// 获取EGL扩展数量
val eglExtCount = gpuInfo.getEglExtensionCount()
println("EGL扩展数量: $eglExtCount")
// 输出示例: "EGL扩展数量: 45"
```
#### EGL客户端API
```kotlin
// 获取EGL支持的客户端API
val clientApis = gpuInfo.getEglClientApis()
clientApis?.let { apis ->
println("EGL客户端API: ${apis.joinToString(", ")}")
// 输出示例: "EGL客户端API: OpenGL_ES"
}
```
### 7. 获取完整GPU信息
#### 获取原始数据
```kotlin
// 获取完整的GPU信息数据
val gpuData = gpuInfo.getGpuInformation()
// Vulkan物理设备列表
gpuData.vkPhysicalDevices?.forEach { device ->
println("=== Vulkan设备 ===")
println("设备名称: ${device.deviceName}")
println("设备ID: ${device.deviceId}")
println("供应商ID: 0x${device.vendorId.toString(16)}")
println("设备类型: ${device.deviceType}")
val apiVer = device.apiVersion
println("API版本: ${apiVer.major}.${apiVer.minor}.${apiVer.patch}")
if (apiVer.variant > 0) {
println("API变体: ${apiVer.variant}")
}
println("驱动版本: ${device.driverVersion}")
device.registeredVendorId?.let { vendorId ->
println("注册供应商: $vendorId")
}
}
// EGL信息
gpuData.eglInformation?.let { egl ->
println("\n=== EGL信息 ===")
egl.eglVendor?.let { println("供应商: $it") }
egl.eglVersion?.let { println("版本: $it") }
egl.eglClientApi?.let { println("客户端API: ${it.joinToString(", ")}") }
egl.eglExtensions?.let { println("扩展数量: ${it.size}") }
// OpenGL ES信息
egl.glInformation?.let { gl ->
println("\n=== OpenGL ES信息 ===")
gl.glVendor?.let { println("供应商: $it") }
gl.glRenderer?.let { println("渲染器: $it") }
gl.glVersion?.let { println("版本: $it") }
gl.glExtensions?.let {
val count = it.split(" ").filter { ext -> ext.isNotEmpty() }.size
println("扩展数量: $count")
}
}
}
```
#### 获取格式化摘要
```kotlin
// 获取格式化的GPU信息摘要
val gpuSummary = gpuInfo.getGpuSummary()
println(gpuSummary)
```
### 8. 在Android UI中显示GPU信息
```kotlin
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.xyzshell.andinfo.AndInfo
class GpuInfoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 获取GpuInfo实例
val gpuInfo = AndInfo.instance.gpu
// 显示GPU信息摘要
val textView = findViewById<TextView>(R.id.gpuInfoText)
textView.text = buildGpuInfoText(gpuInfo)
}
private fun buildGpuInfoText(gpuInfo: GpuInfo): String {
return buildString {
appendLine("=== GPU 基本信息 ===")
gpuInfo.getVendorName()?.let { appendLine("供应商: $it") }
gpuInfo.getRendererName()?.let { appendLine("型号: $it") }
gpuInfo.getDeviceTypeDescription()?.let { appendLine("类型: $it") }
appendLine("\n=== Vulkan ===")
if (gpuInfo.isVulkanSupported()) {
appendLine("支持: 是")
gpuInfo.getVulkanApiVersion()?.let { appendLine("API版本: $it") }
gpuInfo.getVulkanDriverVersion()?.let { appendLine("驱动版本: $it") }
} else {
appendLine("支持: 否")
}
appendLine("\n=== OpenGL ES ===")
gpuInfo.getOpenGLVersion()?.let { appendLine("版本: $it") }
appendLine("扩展数量: ${gpuInfo.getOpenGLExtensionCount()}")
appendLine("\n=== EGL ===")
gpuInfo.getEglVersion()?.let { appendLine("版本: $it") }
appendLine("扩展数量: ${gpuInfo.getEglExtensionCount()}")
gpuInfo.getEglClientApis()?.let {
appendLine("客户端API: ${it.joinToString(", ")}")
}
}
}
}
```
## 数据结构说明
### GpuInformationData
```kotlin
data class GpuInformationData(
val vkPhysicalDevices: List<VkPhysicalDevice>?, // Vulkan物理设备列表
val eglInformation: EglInformation? // EGL信息
)
```
### VkPhysicalDevice (Vulkan物理设备)
```kotlin
data class VkPhysicalDevice(
val apiVersion: VkVersion, // API版本
val driverVersion: Long, // 驱动版本
val vendorId: Long, // 供应商IDPCI标准
val registeredVendorId: VkVendorId?, // 注册的供应商ID
val deviceId: Long, // 设备ID
val deviceType: VkPhysicalDeviceType, // 设备类型
val deviceName: String, // 设备名称
)
```
### EglInformation (EGL信息)
```kotlin
data class EglInformation(
val eglVendor: String?, // EGL供应商
val eglVersion: String?, // EGL版本
val eglExtensions: List<String>?, // EGL扩展列表
val eglClientApi: List<String>?, // 客户端API列表
val glInformation: GlInformation?, // OpenGL ES信息
)
```
### GlInformation (OpenGL ES信息)
```kotlin
data class GlInformation(
val glVendor: String?, // OpenGL供应商
val glRenderer: String?, // 渲染器名称
val glVersion: String?, // OpenGL版本
val glExtensions: String?, // 扩展列表(空格分隔)
)
```
## 常见GPU供应商和型号
### Qualcomm Adreno
- **供应商**: Qualcomm
- **常见型号**: Adreno 640, Adreno 650, Adreno 660, Adreno 730
- **PCI ID**: 0x5143
### ARM Mali
- **供应商**: ARM
- **常见型号**: Mali-G76, Mali-G77, Mali-G78, Mali-G710
- **PCI ID**: 0x13B5
### Imagination PowerVR
- **供应商**: ImgTec
- **常见型号**: PowerVR GE8320, PowerVR GM9446
- **PCI ID**: 0x1010
### NVIDIA
- **供应商**: NVIDIA
- **常见型号**: Tegra系列
- **PCI ID**: 0x10DE
### AMD
- **供应商**: AMD
- **常见型号**: Radeon系列
- **PCI ID**: 0x1002
### Intel
- **供应商**: Intel
- **常见型号**: HD Graphics, Iris
- **PCI ID**: 0x8086
## GPU设备类型
| 类型 | 中文名称 | 说明 |
|------|----------|------|
| INTEGRATED_GPU | 集成显卡 | 集成在CPU芯片中的GPU |
| DISCRETE_GPU | 独立显卡 | 独立的GPU芯片 |
| VIRTUAL_GPU | 虚拟显卡 | 虚拟化环境中的GPU |
| CPU | CPU | 使用CPU进行图形渲染 |
| OTHER | 其他 | 其他类型 |
## Vulkan供应商ID
| 供应商 | ID | 说明 |
|--------|-----|------|
| Khronos | 0x10001 | Khronos科纳斯组织 |
| Vivante | 0x10002 | Vivante维万特 |
| VeriSilicon | 0x10003 | VeriSilicon芯原 |
| Kazan | 0x10004 | Kazan |
| Codeplay | 0x10005 | Codeplay |
| Mesa | 0x10006 | Mesa开源驱动 |
| POCL | 0x10007 | POCL |
| Mobileye | 0x10008 | Mobileye移动眼 |
## 示例输出
### 完整GPU信息示例
```
=== GPU 基本信息 ===
供应商: Qualcomm
型号: Adreno (TM) 650
类型: 集成显卡
=== Vulkan 信息 ===
支持: 是
API 版本: 1.3.0
驱动版本: 512459776
--- Vulkan 设备 ---
设备名称: Adreno (TM) 650
设备 ID: 16860431361
供应商 ID: 0x5143
设备类型: 集成显卡
API 版本: 1.3.0
=== OpenGL ES 信息 ===
供应商: Qualcomm
渲染器: Adreno (TM) 650
版本: OpenGL ES 3.2 V@0490.0 (GIT@dd96402, Ie36a36c6b7, 1620286309) (Date:05/06/21)
扩展数量: 156
=== EGL 信息 ===
供应商: Android
版本: 1.5
客户端 API: OpenGL_ES
扩展数量: 45
```
### Mali GPU示例
```
=== GPU 基本信息 ===
供应商: ARM
型号: Mali-G78
类型: 集成显卡
=== Vulkan 信息 ===
支持: 是
API 版本: 1.1.0
驱动版本: 28114944
=== OpenGL ES 信息 ===
供应商: ARM
渲染器: Mali-G78
版本: OpenGL ES 3.2
扩展数量: 143
```
## 注意事项
1. **本地库依赖**: GpuInfo需要加载本地库"andinfo"确保NDK部分正确编译
2. **Vulkan支持**: 不是所有设备都支持Vulkan API需要先检查`isVulkanSupported()`
3. **设备差异**: 不同设备返回的GPU信息格式可能略有不同
4. **权限**: 获取GPU信息通常不需要特殊权限
5. **性能**: GPU信息获取涉及native调用建议在后台线程执行
6. **扩展列表**: OpenGL扩展字符串可能非常长数千字符显示时需要适当处理
## 典型应用场景
### 1. 游戏兼容性检查
```kotlin
fun checkGameCompatibility(): Boolean {
val gpuInfo = AndInfo.instance.gpu
// 检查Vulkan支持
if (!gpuInfo.isVulkanSupported()) {
return false
}
// 检查Vulkan API版本
val apiVersion = gpuInfo.getVulkanApiVersion()
if (apiVersion == null || apiVersion < "1.1.0") {
return false
}
// 检查是否为集成显卡或独立显卡
val deviceType = gpuInfo.getDeviceType()
return deviceType == VkPhysicalDeviceType.INTEGRATED_GPU ||
deviceType == VkPhysicalDeviceType.DISCRETE_GPU
}
```
### 2. 图形API选择
```kotlin
fun selectGraphicsApi(): GraphicsApi {
val gpuInfo = AndInfo.instance.gpu
return when {
// 优先使用Vulkan
gpuInfo.isVulkanSupported() -> {
val version = gpuInfo.getVulkanApiVersion()
if (version != null && version >= "1.3.0") {
GraphicsApi.VULKAN_1_3
} else {
GraphicsApi.VULKAN_1_1
}
}
// 回退到OpenGL ES
else -> {
val glVersion = gpuInfo.getOpenGLVersion()
when {
glVersion?.contains("3.2") == true -> GraphicsApi.OPENGL_ES_3_2
glVersion?.contains("3.1") == true -> GraphicsApi.OPENGL_ES_3_1
else -> GraphicsApi.OPENGL_ES_3_0
}
}
}
}
```
### 3. 性能分析和优化建议
```kotlin
fun analyzeGpuPerformance(): PerformanceProfile {
val gpuInfo = AndInfo.instance.gpu
val vendor = gpuInfo.getVendorName()
val renderer = gpuInfo.getRendererName()
return when {
// 高端Adreno
renderer?.contains("Adreno") == true &&
(renderer.contains("6") || renderer.contains("7")) -> {
PerformanceProfile.HIGH_END
}
// 高端Mali
renderer?.contains("Mali-G7") == true ||
renderer?.contains("Mali-G9") == true -> {
PerformanceProfile.HIGH_END
}
// 中端GPU
renderer?.contains("Adreno 5") == true ||
renderer?.contains("Mali-G5") == true -> {
PerformanceProfile.MID_RANGE
}
// 低端GPU
else -> PerformanceProfile.LOW_END
}
}
```
## 完成度
**100%** - 所有功能都已实现并添加详细注释
- GPU供应商信息 ✅
- GPU型号信息 ✅
- Vulkan支持检测 ✅
- Vulkan API版本 ✅
- Vulkan驱动版本 ✅
- OpenGL ES版本 ✅
- OpenGL ES扩展 ✅
- EGL版本 ✅
- EGL扩展 ✅
- EGL客户端API ✅
- GPU设备类型 ✅
- 完整的中文注释 ✅
- 格式化信息输出 ✅