|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.gfx; |
|
7 |
|
8 import org.mozilla.gecko.mozglue.RobocopTarget; |
|
9 |
|
10 import android.os.SystemClock; |
|
11 import android.util.Log; |
|
12 |
|
13 import java.util.ArrayList; |
|
14 import java.util.List; |
|
15 |
|
16 public class PanningPerfAPI { |
|
17 private static final String LOGTAG = "GeckoPanningPerfAPI"; |
|
18 |
|
19 // make this large enough to avoid having to resize the frame time |
|
20 // list, as that may be expensive and impact the thing we're trying |
|
21 // to measure. |
|
22 private static final int EXPECTED_FRAME_COUNT = 2048; |
|
23 |
|
24 private static boolean mRecordingFrames = false; |
|
25 private static List<Long> mFrameTimes; |
|
26 private static long mFrameStartTime; |
|
27 |
|
28 private static boolean mRecordingCheckerboard = false; |
|
29 private static List<Float> mCheckerboardAmounts; |
|
30 private static long mCheckerboardStartTime; |
|
31 |
|
32 private static void initialiseRecordingArrays() { |
|
33 if (mFrameTimes == null) { |
|
34 mFrameTimes = new ArrayList<Long>(EXPECTED_FRAME_COUNT); |
|
35 } else { |
|
36 mFrameTimes.clear(); |
|
37 } |
|
38 if (mCheckerboardAmounts == null) { |
|
39 mCheckerboardAmounts = new ArrayList<Float>(EXPECTED_FRAME_COUNT); |
|
40 } else { |
|
41 mCheckerboardAmounts.clear(); |
|
42 } |
|
43 } |
|
44 |
|
45 @RobocopTarget |
|
46 public static void startFrameTimeRecording() { |
|
47 if (mRecordingFrames || mRecordingCheckerboard) { |
|
48 Log.e(LOGTAG, "Error: startFrameTimeRecording() called while already recording!"); |
|
49 return; |
|
50 } |
|
51 mRecordingFrames = true; |
|
52 initialiseRecordingArrays(); |
|
53 mFrameStartTime = SystemClock.uptimeMillis(); |
|
54 } |
|
55 |
|
56 @RobocopTarget |
|
57 public static List<Long> stopFrameTimeRecording() { |
|
58 if (!mRecordingFrames) { |
|
59 Log.e(LOGTAG, "Error: stopFrameTimeRecording() called when not recording!"); |
|
60 return null; |
|
61 } |
|
62 mRecordingFrames = false; |
|
63 return mFrameTimes; |
|
64 } |
|
65 |
|
66 public static void recordFrameTime() { |
|
67 // this will be called often, so try to make it as quick as possible |
|
68 if (mRecordingFrames) { |
|
69 mFrameTimes.add(SystemClock.uptimeMillis() - mFrameStartTime); |
|
70 } |
|
71 } |
|
72 |
|
73 public static boolean isRecordingCheckerboard() { |
|
74 return mRecordingCheckerboard; |
|
75 } |
|
76 |
|
77 @RobocopTarget |
|
78 public static void startCheckerboardRecording() { |
|
79 if (mRecordingCheckerboard || mRecordingFrames) { |
|
80 Log.e(LOGTAG, "Error: startCheckerboardRecording() called while already recording!"); |
|
81 return; |
|
82 } |
|
83 mRecordingCheckerboard = true; |
|
84 initialiseRecordingArrays(); |
|
85 mCheckerboardStartTime = SystemClock.uptimeMillis(); |
|
86 } |
|
87 |
|
88 @RobocopTarget |
|
89 public static List<Float> stopCheckerboardRecording() { |
|
90 if (!mRecordingCheckerboard) { |
|
91 Log.e(LOGTAG, "Error: stopCheckerboardRecording() called when not recording!"); |
|
92 return null; |
|
93 } |
|
94 mRecordingCheckerboard = false; |
|
95 |
|
96 // We take the number of values in mCheckerboardAmounts here, as there's |
|
97 // the possibility that this function is called while recordCheckerboard |
|
98 // is still executing. As values are added to this list last, we use |
|
99 // this number as the canonical number of recordings. |
|
100 int values = mCheckerboardAmounts.size(); |
|
101 |
|
102 // The score will be the sum of all the values in mCheckerboardAmounts, |
|
103 // so weight the checkerboard values by time so that frame-rate and |
|
104 // run-length don't affect score. |
|
105 long lastTime = 0; |
|
106 float totalTime = mFrameTimes.get(values - 1); |
|
107 for (int i = 0; i < values; i++) { |
|
108 long elapsedTime = mFrameTimes.get(i) - lastTime; |
|
109 mCheckerboardAmounts.set(i, mCheckerboardAmounts.get(i) * elapsedTime / totalTime); |
|
110 lastTime += elapsedTime; |
|
111 } |
|
112 |
|
113 return mCheckerboardAmounts; |
|
114 } |
|
115 |
|
116 public static void recordCheckerboard(float amount) { |
|
117 // this will be called often, so try to make it as quick as possible |
|
118 if (mRecordingCheckerboard) { |
|
119 mFrameTimes.add(SystemClock.uptimeMillis() - mCheckerboardStartTime); |
|
120 mCheckerboardAmounts.add(amount); |
|
121 } |
|
122 } |
|
123 } |