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
|
/************************************************************************************
PublicHeader: OVR.h
Filename : Util_LatencyTest2.h
Content : Wraps the lower level LatencyTester interface for DK2 and adds functionality.
Created : March 10, 2014
Authors : Volga Aksoy
Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
Licensed under the Oculus VR Rift SDK License Version 3.1 (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.1
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.
*************************************************************************************/
#ifndef OVR_Util_LatencyTest2_h
#define OVR_Util_LatencyTest2_h
#include "../OVR_Device.h"
#include "../Kernel/OVR_String.h"
#include "../Kernel/OVR_List.h"
#include "../Kernel/OVR_Lockless.h"
namespace OVR { namespace Util {
enum {
LT2_ColorIncrement = 32,
LT2_PixelTestThreshold = LT2_ColorIncrement / 3,
LT2_IncrementCount = 256 / LT2_ColorIncrement,
LT2_TimeoutWaitingForColorDetected = 1000 // 1 second
};
//-------------------------------------------------------------------------------------
// Describes frame scanout time used for latency testing.
struct FrameTimeRecord
{
int ReadbackIndex;
double TimeSeconds;
// Utility functions to convert color to readBack indices and back.
// The purpose of ReadbackIndex is to allow direct comparison by value.
static bool ColorToReadbackIndex(int *readbackIndex, unsigned char color)
{
int compareColor = color - LT2_ColorIncrement/2;
int index = color / LT2_ColorIncrement; // Use color without subtraction due to rounding.
int delta = compareColor - index * LT2_ColorIncrement;
if ((delta < LT2_PixelTestThreshold) && (delta > -LT2_PixelTestThreshold))
{
*readbackIndex = index;
return true;
}
return false;
}
static unsigned char ReadbackIndexToColor(int readbackIndex)
{
OVR_ASSERT(readbackIndex < LT2_IncrementCount);
return (unsigned char)(readbackIndex * LT2_ColorIncrement + LT2_ColorIncrement/2);
}
};
// FrameTimeRecordSet is a container holding multiple consecutive frame timing records
// returned from the lock-less state. Used by FrameTimeManager.
struct FrameTimeRecordSet
{
enum {
RecordCount = 4,
RecordMask = RecordCount - 1
};
FrameTimeRecord Records[RecordCount];
int NextWriteIndex;
FrameTimeRecordSet()
{
NextWriteIndex = 0;
memset(this, 0, sizeof(FrameTimeRecordSet));
}
void AddValue(int readValue, double timeSeconds)
{
Records[NextWriteIndex].ReadbackIndex = readValue;
Records[NextWriteIndex].TimeSeconds = timeSeconds;
NextWriteIndex ++;
if (NextWriteIndex == RecordCount)
NextWriteIndex = 0;
}
// Matching should be done starting from NextWrite index
// until wrap-around
const FrameTimeRecord& operator [] (int i) const
{
return Records[(NextWriteIndex + i) & RecordMask];
}
const FrameTimeRecord& GetMostRecentFrame()
{
return Records[(NextWriteIndex - 1) & RecordMask];
}
// Advances I to absolute color index
bool FindReadbackIndex(int* i, int readbackIndex) const
{
for (; *i < RecordCount; (*i)++)
{
if ((*this)[*i].ReadbackIndex == readbackIndex)
return true;
}
return false;
}
bool IsAllZeroes() const
{
for (int i = 0; i < RecordCount; i++)
if (Records[i].ReadbackIndex != 0)
return false;
return true;
}
};
//-------------------------------------------------------------------------------------
// ***** LatencyTest2
//
// LatencyTest2 utility class wraps the low level SensorDevice and manages the scheduling
// of a latency test. A single test is composed of a series of individual latency measurements
// which are used to derive min, max, and an average latency value.
//
// Developers are required to call the following methods:
// SetDevice - Sets the SensorDevice to be used for the tests.
// ProcessInputs - This should be called at the same place in the code where the game engine
// reads the headset orientation from LibOVR (typically done by calling
// 'GetOrientation' on the SensorFusion object). Calling this at the right time
// enables us to measure the same latency that occurs for headset orientation
// changes.
// DisplayScreenColor - The latency tester works by sensing the color of the pixels directly
// beneath it. The color of these pixels can be set by drawing a small
// quad at the end of the rendering stage. The quad should be small
// such that it doesn't significantly impact the rendering of the scene,
// but large enough to be 'seen' by the sensor. See the SDK
// documentation for more information.
// GetResultsString - Call this to get a string containing the most recent results.
// If the string has already been gotten then NULL will be returned.
// The string pointer will remain valid until the next time this
// method is called.
//
class LatencyTest2 : public NewOverrideBase
{
public:
LatencyTest2(SensorDevice* device = NULL);
~LatencyTest2();
// Set the Latency Tester device that we'll use to send commands to and receive
// notification messages from.
bool SetSensorDevice(SensorDevice* device);
bool SetDisplayDevice(LatencyTestDevice* device);
// Returns true if this LatencyTestUtil has a Latency Tester device.
bool HasDisplayDevice() const { return LatencyTesterDev.GetPtr() != NULL; }
bool HasDevice() const { return Handler.IsHandlerInstalled(); }
bool DisplayScreenColor(Color& colorToDisplay);
//const char* GetResultsString();
// Begin test. Equivalent to pressing the button on the latency tester.
void BeginTest(double startTime = -1.0f);
bool IsMeasuringNow() const { return TestActive; }
double GetMeasuredLatency() const { return LatencyMeasuredInSeconds; }
//
FrameTimeRecordSet GetLocklessState() { return LockessRecords.GetState(); }
private:
LatencyTest2* getThis() { return this; }
enum LatencyTestMessageType
{
LatencyTest_None,
LatencyTest_Timer,
LatencyTest_ProcessInputs,
};
void handleMessage(const MessagePixelRead& msg);
class PixelReadHandler : public MessageHandler
{
LatencyTest2* pLatencyTestUtil;
public:
PixelReadHandler(LatencyTest2* latencyTester) : pLatencyTestUtil(latencyTester) { }
~PixelReadHandler();
virtual void OnMessage(const Message& msg);
};
PixelReadHandler Handler;
Ptr<SensorDevice> HmdDevice;
Ptr<LatencyTestDevice> LatencyTesterDev;
Lock TesterLock;
bool TestActive;
unsigned char RenderColorValue;
MessagePixelRead LastPixelReadMsg;
double StartTiming;
unsigned int RawStartTiming;
UInt32 RawLatencyMeasured;
double LatencyMeasuredInSeconds;
int NumMsgsBeforeSettle;
unsigned int NumTestsSuccessful;
// MA:
// Frames are added here, then copied into lockess state
FrameTimeRecordSet RecentFrameSet;
LocklessUpdater<FrameTimeRecordSet> LockessRecords;
};
}} // namespace OVR::Util
#endif // OVR_Util_LatencyTest2_h
|