FpsInfo.java 1.5 KB
Newer Older
A
andrewleo 已提交
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
package com.netease.qa.emmagee.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FpsInfo {

	private static Process process;
	private static BufferedReader ir;
	private static DataOutputStream os = null;
	private static long startTime = 0L;
	private static int lastFrameNum = 0;

	/**
	 * get frame per second
	 * @return frame per second
	 */
	public static float fps() {
		long nowTime = System.nanoTime();
		float f = (float) (nowTime - startTime) / 1000000.0F;
		startTime = nowTime;
		int nowFrameNum = getFrameNum();
		final float fps = Math.round((nowFrameNum - lastFrameNum) * 1000 / f);
		lastFrameNum = nowFrameNum;
		return fps;
	}

	/**
	 * get frame value
	 * 
	 * @return frame value
	 */
	public static final int getFrameNum() {
		try {
			if (process == null) {
				process = Runtime.getRuntime().exec("su");
				os = new DataOutputStream(process.getOutputStream());
				ir = new BufferedReader(new InputStreamReader(
						process.getInputStream()));
			}
			os.writeBytes("service call SurfaceFlinger 1013" + "\n");
			os.flush();
			String str1 = ir.readLine();
			if (str1 == null) {
				return -1;
			}
			int start = str1.indexOf("(");
			int end = str1.indexOf("  ");
			if ((start != -1) & (end > start)) {
				String str2 = str1.substring(start + 1, end);
				return Integer.parseInt((String) str2, 16);
			}
			return -1;
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		}
	}
}