-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathFpsUtils.kt
99 lines (93 loc) · 4.01 KB
/
FpsUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.omarea.library.shell
import com.omarea.common.shell.KeepShell
import com.omarea.common.shell.KeepShellPublic
import com.omarea.common.shell.RootFile.fileExists
/**
* 帧率检测
*/
class FpsUtils(private val keepShell: KeepShell = KeepShellPublic.secondaryKeepShell) {
private var fpsFilePath: String? = null
private var subStrCommand = "| awk '{print \$2}'"
private var fpsCommand2 = "service call SurfaceFlinger 1013"
private var lastTime = -1L
private var lastFrames = -1
val currentFps: String?
get() {
// 优先使用GPU的内核级帧数数据
if (!fpsFilePath.isNullOrEmpty()) {
return keepShell.doCmdSync("cat $fpsFilePath $subStrCommand")
}
// 如果系统帧率不可用使用GPU的内核级帧数数据
else if (fpsFilePath == null) {
when {
fileExists("/sys/class/drm/sde-crtc-0/measured_fps") -> {
fpsFilePath = "/sys/class/drm/sde-crtc-0/measured_fps"
}
fileExists("/sys/class/graphics/fb0/measured_fps") -> {
fpsFilePath = "/sys/class/graphics/fb0/measured_fps"
subStrCommand = ""
}
else -> {
fpsFilePath = ""
val keepShell = KeepShell()
Thread(Runnable {
try {
keepShell.doCmdSync("find /sys -name measured_fps 2>/dev/null")
.trim { it <= ' ' }.split("\n").filter { it.contains("crtc") }.min()?.run {
fpsFilePath = this
}
if (fpsFilePath == null || fpsFilePath == "") {
keepShell.doCmdSync("find /sys -name fps 2>/dev/null")
.trim { it <= ' ' }.split("\n").filter { it.contains("crtc") }.min()?.run {
fpsFilePath = this
}
}
if (fpsFilePath == null) {
fpsFilePath = ""
}
keepShell.tryExit()
} catch (ex: Exception) {
fpsFilePath = ""
}
}).start()
}
}
}
// 使用系统帧率
else if (fpsCommand2.isNotEmpty()) {
val result = keepShell.doCmdSync(fpsCommand2).trim()
if (result != "error" && !result.contains("Parcel")) {
fpsCommand2 = ""
} else {
try {
val index = result.indexOf("(") + 1
val frames = Integer.parseInt(result.substring(index, index + 8), 16)
val time = System.currentTimeMillis()
var fps = 0F
if (lastTime > 0 && lastFrames > 0) {
fps = (frames - lastFrames) * 1000.0f / (time - lastTime)
}
lastFrames = frames
lastTime = time
return String.format("%.1f", fps)
} catch (ex: Exception) {
if (!(lastTime > 0 && lastFrames > 0)) {
fpsCommand2 = ""
}
}
}
}
return null
}
val fps: Float
get() {
val fpsStr = currentFps
if (fpsStr != null) {
try {
return fpsStr.toFloat()
} catch (ex: java.lang.Exception) {
}
}
return -0f
}
}