-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimingGraph.prejava
226 lines (207 loc) · 7.89 KB
/
TimingGraph.prejava
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "macros.h"
class TimingGraph
{
public DoubleRingBuffer startTimes = new DoubleRingBuffer();
public DoubleRingBuffer endTimes = new DoubleRingBuffer();
public void recordPaintStartTime(int maxRingBufferSize)
{
double startTime = timeSeconds();
while (startTimes.size() >= maxRingBufferSize) startTimes.deleteFirst();
startTimes.append(startTime);
}
public void recordPaintEndTime(int maxRingBufferSize)
{
while (endTimes.size() >= maxRingBufferSize) endTimes.deleteFirst();
double endTime = timeSeconds();
endTimes.append(endTime);
}
public void preDraw(java.awt.Graphics g,
java.awt.Dimension size,
double pixelsPerMilli)
{
// background horizontal lines for timing
g.setColor(java.awt.Color.DARK_GRAY);
for (int i = 0; ; i++)
{
int y = size.height-(int)Math.round(i*pixelsPerMilli*1000./60.);
if (y < 0)
break;
g.fillRect(0,y-1,
size.width,2);
}
}
public void draw(java.awt.Graphics g,
java.awt.Dimension size,
double pixelsPerMilli)
{
{
double now = timeSeconds();
double startTime = startTimes.get(startTimes.size()-1);
for (int iCol = 0; iCol < size.width; ++iCol)
{
int x = size.width-1-iCol;
int y = size.height;
if (iCol==0)
{
double paintTimeSoFar = now-startTime;
CHECK_GE(paintTimeSoFar, 0.);
//PRINT(paintTimeSoFar);
g.setColor(java.awt.Color.YELLOW);
int barHeight = (int)(.5+paintTimeSoFar*(1000.*pixelsPerMilli));
g.fillRect(x,y-barHeight,
1,barHeight);
y -= barHeight;
}
else if (iCol <= endTimes.size() && iCol < startTimes.size())
{
double paintTime = endTimes.get(endTimes.size()-iCol) - startTimes.get(startTimes.size()-1-iCol);
CHECK_GE(paintTime, 0.);
//PRINT(paintTime);
g.setColor(java.awt.Color.GREEN);
int barHeight = (int)(.5+paintTime*(1000.*pixelsPerMilli));
g.fillRect(x,y-barHeight,
1,barHeight);
y -= barHeight;
}
if (iCol>=1 && (iCol-1) < startTimes.size() && (iCol-1) < endTimes.size())
{
double nonPaintTime = (startTimes.get(startTimes.size()-1-(iCol-1)) - endTimes.get(endTimes.size()-1-(iCol-1)));
//PRINT(nonPaintTime);
CHECK_GE(nonPaintTime, 0.);
g.setColor(java.awt.Color.RED);
int barHeight = (int)(.5+nonPaintTime*(1000.*pixelsPerMilli));
g.fillRect(x,y-barHeight,
1,barHeight);
y -= barHeight;
}
}
}
{
int frames = startTimes.size()-1;
double seconds = (double)(startTimes.get(startTimes.size()-1)-startTimes.get(0));
if (seconds > 0.) // implies frames must be > 0 too
{
g.setColor(java.awt.Color.WHITE);
g.drawString("fps during "+frames+" frames: "+formatFixedPoint(frames/seconds,3),
size.width-300,
40);
g.drawString("spf during "+frames+" frames: "+formatFixedPoint(seconds/frames, 5),
size.width-300,
60);
g.setColor(java.awt.Color.RED);
g.drawString("red is time outside paintComponent",
size.width-300,
80);
g.setColor(java.awt.Color.GREEN);
g.drawString("green is time in paintComponent",
size.width-300,
100);
}
}
} // drawTimingGraph
// holy mother of god
private static String formatFixedPoint(double d, int prec)
{
boolean wasNegative = false;
if (d < 0.)
{
wasNegative = true;
d = -d;
}
FORI (i, prec)
d *= 10;
long l = (long)Math.round(d);
String answer = "";
FORI (i, prec)
{
answer = l%10 + answer;
l /= 10;
}
return (wasNegative ? "-" : "") + l + "." + answer;
}
// ring buffer of doubles.
// O(1) operations:
// size()
// get()
// deleteFirst()
// append() (amortized)
public static class DoubleRingBuffer
{
private int i0 = 0;
private int size = 0;
private double[] array = new double[1];
public int size()
{
return size;
}
public double get(int index)
{
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("DoubleRingBuffer.get: "+index+" out of bounds 0.."+size+"-1");
return array[(i0+index)%array.length];
}
public void set(int index, double item)
{
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("DoubleRingBuffer.set: "+index+" out of bounds 0.."+size+"-1");
array[(i0+index)%array.length] = item;
}
public void append(double item)
{
if (size == array.length)
{
// grow
double newArray[] = new double[array.length*2];
System.arraycopy(array, i0, newArray, 0, size-i0);
System.arraycopy(array, 0, newArray, size-i0, i0);
array = newArray;
i0 = 0;
// and size stays the same... now half of array.length
}
array[(i0+size++)%array.length] = item;
}
public void deleteFirst()
{
if (size == 0)
{
throw new IndexOutOfBoundsException("DoubleRingBuffer.deleteFirst: buffer is empty");
}
i0 = (i0+1)%array.length;
size--;
}
} // private class DoubleRingBuffer
private static java.lang.reflect.Method nanoTimeMethod = null;
{
try {
nanoTimeMethod = System.class.getMethod("nanoTime", new Class[]{});
}
catch (NoSuchMethodException e) {System.out.println("no such method exception!");}
}
// System.nanoTime().
// don't call this unless you know nanoTimeMethod != null.
private static long System_nanoTime()
{
try {
// object allocation here is a bummer.
// maybe better to compile using javac >= 1.4
// but with flag saying create class files for earlier jvm?
Object answer = nanoTimeMethod.invoke(null, new Object[]{});
return ((Long)answer).longValue();
}
catch (IllegalAccessException e) {CHECK(false); return 0L;}
catch (java.lang.reflect.InvocationTargetException e) {CHECK(false); return 0L;}
}
// Time in seconds from when we initialize the times,
// using System.nanoTime() if it exists (jvm >= 1.4),
// otherwise System.currentTimeMillis().
public static double timeSeconds()
{
boolean useNanoTime = (nanoTimeMethod != null);
if (useNanoTime)
return (double)(System_nanoTime() - nanos0) / 1e9;
else
return (double)(System.currentTimeMillis() - millis0) / 1e3;
}
private static long millis0 = System.currentTimeMillis();
private static long nanos0 = (nanoTimeMethod!=null ? System_nanoTime() : 0L);
} // class TimingGraph