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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/************************************************************************************
Filename : CAPI_FrameLatencyTracker.cpp
Content : DK2 Latency Tester implementation
Created : Dec 12, 2013
Authors : Volga Aksoy, Michael Antonov
Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-3.2
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#include "CAPI_FrameLatencyTracker.h"
#include "Kernel/OVR_Log.h"
namespace OVR { namespace CAPI {
// Number of frame delta samples to include in the median calculation.
static const int kFrameDeltaSamples = 12;
//-------------------------------------------------------------------------------------
// ***** FrameLatencyTracker
FrameLatencyTracker::FrameLatencyTracker() :
FrameDeltas(kFrameDeltaSamples)
{
Reset();
}
void FrameLatencyTracker::Reset()
{
TrackerEnabled = true;
WaitMode = SampleWait_Zeroes;
MatchCount = 0;
memset(History, 0, sizeof(History));
FrameIndex = 0;
LatencyRecordTime = 0.0;
OutputTimings.Clear();
FrameDeltas.Clear();
}
unsigned char FrameLatencyTracker::GetNextDrawColor()
{
if (!TrackerEnabled || (WaitMode == SampleWait_Zeroes) ||
(FrameIndex >= FramesTracked))
{
return (unsigned char)Util::FrameTimeRecord::ReadbackIndexToColor(0);
}
OVR_ASSERT(FrameIndex < FramesTracked);
return (unsigned char)Util::FrameTimeRecord::ReadbackIndexToColor(FrameIndex+1);
}
void FrameLatencyTracker::SaveDrawColor(FrameLatencyData const & data)
{
if (!TrackerEnabled || (WaitMode == SampleWait_Zeroes))
return;
if (FrameIndex < FramesTracked)
{
OVR_ASSERT(Util::FrameTimeRecord::ReadbackIndexToColor(FrameIndex + 1) == data.DrawColor);
// FrameTimeRecord data
History[FrameIndex].ReadbackIndex = FrameIndex + 1;
History[FrameIndex].TimeSeconds = data.PresentTime;
// FrameTimeRecordEx data
History[FrameIndex].MatchedRecord = false;
History[FrameIndex].FrameData = data;
FrameIndex++;
}
else
{
// If the request was outstanding for too long, switch to zero mode to restart.
if (data.PresentTime > (History[FrameIndex-1].TimeSeconds + 0.15))
{
if (MatchCount == 0)
{
OutputTimings.Clear();
}
WaitMode = SampleWait_Zeroes;
MatchCount = 0;
FrameIndex = 0;
}
}
}
void FrameLatencyTracker::onRecordMatch(FrameTimeRecordEx& renderFrame,
Util::FrameTimeRecord const& scanoutFrame)
{
MatchCount++;
double deltaSeconds = scanoutFrame.TimeSeconds - renderFrame.TimeSeconds;
// Reject latencies longer than 100 ms
// This can happen in transient situations like dragging the render window around,
// and since some critical systems depend on this latency data to provide steady-state
// statistics for prediction purposes these outliers should not dirty the data.
if (deltaSeconds < 0.1)
{
if (deltaSeconds < 0.)
{
deltaSeconds = 0.;
}
FrameDeltas.Add(deltaSeconds);
}
LatencyRecordTime = scanoutFrame.TimeSeconds;
OutputTimings.LatencyRender = scanoutFrame.TimeSeconds - renderFrame.FrameData.RenderIMUTime;
OutputTimings.LatencyTimewarp = (renderFrame.FrameData.TimewarpIMUTime == 0.0) ? 0.0 :
(scanoutFrame.TimeSeconds - renderFrame.FrameData.TimewarpIMUTime);
OutputTimings.ErrorRender = scanoutFrame.TimeSeconds - renderFrame.FrameData.RenderPredictedScanoutTime;
OutputTimings.ErrorTimewarp = scanoutFrame.TimeSeconds - renderFrame.FrameData.TimewarpPredictedScanoutTime;
}
void FrameLatencyTracker::MatchRecord(Util::FrameTimeRecordSet const & r)
{
if (!TrackerEnabled)
return;
if (WaitMode == SampleWait_Zeroes)
{
// Do we have all zeros?
if (r.IsAllZeroes())
{
OVR_ASSERT(FrameIndex == 0);
WaitMode = SampleWait_Match;
MatchCount = 0;
}
return;
}
// We are in Match Mode. Wait until all colors are matched or timeout,
// at which point we go back to zeros.
for (int i = 0; i < FrameIndex; i++)
{
int recordIndex = 0;
int consecutiveMatch = 0;
OVR_ASSERT(History[i].ReadbackIndex != 0);
if (r.FindReadbackIndex(&recordIndex, History[i].ReadbackIndex))
{
// Advance forward to see that we have several more matches.
int ri = recordIndex + 1;
int j = i + 1;
consecutiveMatch++;
for (; (j < FrameIndex) && (ri < Util::FrameTimeRecordSet::RecordCount); j++, ri++)
{
if (r[ri].ReadbackIndex != History[j].ReadbackIndex)
break;
consecutiveMatch++;
}
// Match at least 2 items in the row, to avoid accidentally matching color.
if (consecutiveMatch > 1)
{
// Record latency values for all but last samples. Keep last 2 samples
// for the future to simplify matching.
for (int q = 0; q < consecutiveMatch; q++)
{
const Util::FrameTimeRecord &scanoutFrame = r[recordIndex+q];
FrameTimeRecordEx &renderFrame = History[i+q];
if (!renderFrame.MatchedRecord)
{
renderFrame.MatchedRecord = true;
onRecordMatch(renderFrame, scanoutFrame);
}
}
// Exit for.
break;
}
}
} // for ( i => FrameIndex )
// If we matched all frames, start over.
if (MatchCount == FramesTracked)
{
WaitMode = SampleWait_Zeroes;
MatchCount = 0;
FrameIndex = 0;
}
}
bool FrameLatencyTracker::IsLatencyTimingAvailable()
{
return ovr_GetTimeInSeconds() < (LatencyRecordTime + 2.0);
}
void FrameLatencyTracker::GetLatencyTimings(OutputLatencyTimings& timings)
{
if (!IsLatencyTimingAvailable())
{
timings.Clear();
return;
}
timings = OutputTimings;
timings.LatencyPostPresent = FrameDeltas.GetMedian();
}
bool FrameLatencyTracker::GetVsyncToScanout(double& vsyncToScanoutTime) const
{
if (FrameDeltas.GetCount() <= 3)
{
return false;
}
double medianDelta = FrameDeltas.GetMedian();
// Sanity check the result
static const double SmallestAcceptedDelta = -0.0020; // -20 ms
static const double LargestAcceptedDelta = 0.060; // 60 ms
if ((medianDelta < SmallestAcceptedDelta) ||
(medianDelta > LargestAcceptedDelta))
{
return false;
}
vsyncToScanoutTime = medianDelta;
return true;
}
}} // namespace OVR::CAPI
|