FpsInfo.java 1.6 KB
Newer Older
A
andrewleo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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;
A
andrewleo 已提交
15
	private static boolean ok = true;
A
andrewleo 已提交
16 17 18

	/**
	 * get frame per second
A
andrewleo 已提交
19
	 * 
A
andrewleo 已提交
20 21 22
	 * @return frame per second
	 */
	public static float fps() {
A
andrewleo 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
		if (ok) {
			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;
		} else {
			return -1;
		}

A
andrewleo 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	}

	/**
	 * 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();
A
andrewleo 已提交
54 55 56 57 58 59 60
			if (str1 != null) {
				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);
				}
A
andrewleo 已提交
61 62 63 64
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
A
andrewleo 已提交
65 66
		ok = false;
		return -1;
A
andrewleo 已提交
67 68
	}
}