Skip to content

Commit

Permalink
feat: 添加帧率监控功能
Browse files Browse the repository at this point in the history
  • Loading branch information
刘雷雷 committed Sep 9, 2022
0 parents commit e52522c
Show file tree
Hide file tree
Showing 54 changed files with 1,813 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions FrameRateMonitor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
30 changes: 30 additions & 0 deletions FrameRateMonitor/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apply plugin: 'com.android.library'

android {

compileSdkVersion 28

defaultConfig {
minSdkVersion 16
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 28
versionCode 1
versionName "1.0"
consumerProguardFiles 'plugin-proguard-rules.pro'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
21 changes: 21 additions & 0 deletions FrameRateMonitor/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
5 changes: 5 additions & 0 deletions FrameRateMonitor/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.erlei.tools.fpsmonitor">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.erlei.tools.fpsmonitor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

class Calculation {

public static List<Integer> getDroppedSet(FPSConfig fpsConfig, List<Long> dataSet) {
List<Integer> droppedSet = new ArrayList<>();
long start = -1;
for (Long value : dataSet) {
if (start == -1) {
start = value;
continue;
}
int droppedCount = droppedCount(start, value, fpsConfig.refreshRateInMs);
if (droppedCount > 0) {
droppedSet.add(droppedCount);
}
start = value;
}
return droppedSet;
}

public static int droppedCount(long start, long end, float devRefreshRate) {
int count = 0;
long diffNs = end - start;

long diffMs = TimeUnit.MILLISECONDS.convert(diffNs, TimeUnit.NANOSECONDS);
long dev = Math.round(devRefreshRate);
if (diffMs > dev) {
long droppedCount = (diffMs / dev);
count = (int) droppedCount;
}

return count;
}

public static FPSView.FrameInfo calculateMetric(FPSConfig fpsConfig,
List<Long> dataSet,
List<Integer> droppedSet) {
long timeInNS = dataSet.get(dataSet.size() - 1) - dataSet.get(0);
long size = getNumberOfFramesInSet(timeInNS, fpsConfig);

//metric
int runningOver = 0;
// total dropped
int dropped = 0;

for (Integer k : droppedSet) {
dropped += k;
if (k >= 2) {
runningOver += k;
}
}

float multiplier = fpsConfig.refreshRate / size;
float answer = multiplier * (size - dropped);
long realAnswer = Math.round(answer);

// calculate metric
float percentOver = (float) runningOver / (float) size;
int color = fpsConfig.colorEvaluator.evaluate(1 - percentOver);
return new FPSView.FrameInfo(color, realAnswer, (int) size, dropped, percentOver);
}

protected static long getNumberOfFramesInSet(long realSampleLengthNs, FPSConfig fpsConfig) {
float realSampleLengthMs = TimeUnit.MILLISECONDS.convert(realSampleLengthNs, TimeUnit.NANOSECONDS);
float size = realSampleLengthMs / fpsConfig.refreshRateInMs;
return Math.round(size);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.erlei.tools.fpsmonitor;

interface ColorEvaluator {
int evaluate(float fraction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.erlei.tools.fpsmonitor;

import android.graphics.Color;
import android.graphics.Rect;

import java.util.concurrent.TimeUnit;

class FPSConfig {
public static ColorEvaluator sColorEvaluator = fraction -> {
int color = Color.TRANSPARENT;
if (fraction >= 0.85) {
color = Color.GREEN;
} else if (fraction > 0.6 && fraction <= 0.85) {
color = Color.YELLOW;
} else if (fraction <= 0.6) {
color = Color.RED;
} else if (fraction == 0) {
color = Color.BLACK;
}
return color;
};
public float refreshRateInMs;
public float refreshRate;
public float textSize = 20;
public long showAnimateDuration = 200;
public long hideAnimateDuration = 200;
public long sampleTimeInNs = TimeUnit.NANOSECONDS.convert(1000, TimeUnit.MILLISECONDS);
public ColorEvaluator colorEvaluator = sColorEvaluator;
public float lineWidth = 2;
public float lineHeight = 0;
public float lineSpacing = 2;
public Rect rect;
public Float[] levelLines = new Float[]{1F, 0.85F, 0.7F, 0.4F, 0.2F};
/**
* 当连续N个fps等于设备最大fps时,跳过绘制
*/
public int skipSameFpsCount = 2;
}
Loading

0 comments on commit e52522c

Please sign in to comment.