summaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/Util/Util_LatencyTest.h
blob: 08446033ed65b5aec89df02bbf378dfaa10d3724 (plain)
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
/************************************************************************************

PublicHeader:   OVR.h
Filename    :   Util_LatencyTest.h
Content     :   Wraps the lower level LatencyTesterDevice and adds functionality.
Created     :   February 14, 2013
Authors     :   Lee Cooper

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_LatencyTest_h
#define OVR_Util_LatencyTest_h

#include "../OVR_Device.h"

#include "../Kernel/OVR_String.h"
#include "../Kernel/OVR_List.h"

namespace OVR { namespace Util {


//-------------------------------------------------------------------------------------
// ***** LatencyTest
//
// LatencyTest utility class wraps the low level LatencyTestDevice 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 LatencyTestDevice 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 LatencyTest : public NewOverrideBase
{
public:
    LatencyTest(LatencyTestDevice* device = NULL);
    ~LatencyTest();
    
    // Set the Latency Tester device that we'll use to send commands to and receive
    // notification messages from.
    bool        SetDevice(LatencyTestDevice* device);

    // Returns true if this LatencyTestUtil has a Latency Tester device.
    bool        HasDevice() const
    { return Handler.IsHandlerInstalled(); }

    void        ProcessInputs();
    bool        DisplayScreenColor(Color& colorToDisplay);
	const char*	GetResultsString();

    bool		IsMeasuringNow() const { return (State != State_WaitingForButton); }

    // Begin test. Equivalent to pressing the button on the latency tester.
    void BeginTest();

private:
    LatencyTest* getThis()  { return this; }

    enum LatencyTestMessageType
    {
        LatencyTest_None,
        LatencyTest_Timer,
        LatencyTest_ProcessInputs,
    };
    
    UInt32 getRandomComponent(UInt32 range);
    void handleMessage(const Message& msg, LatencyTestMessageType latencyTestMessage = LatencyTest_None);
    void reset();
    void setTimer(UInt32 timeMilliS);
    void clearTimer();

    class LatencyTestHandler : public MessageHandler
    {
        LatencyTest*    pLatencyTestUtil;
    public:
        LatencyTestHandler(LatencyTest* latencyTester) : pLatencyTestUtil(latencyTester) { }
        ~LatencyTestHandler();

        virtual void OnMessage(const Message& msg);
    };

    bool areResultsComplete();
    void processResults();
    void updateForTimeouts();

    Ptr<LatencyTestDevice>      Device;
    LatencyTestHandler          Handler;

    enum TesterState
    {
        State_WaitingForButton,
        State_WaitingForSettlePreCalibrationColorBlack,
        State_WaitingForSettlePostCalibrationColorBlack,
        State_WaitingForSettlePreCalibrationColorWhite,
        State_WaitingForSettlePostCalibrationColorWhite,
        State_WaitingToTakeMeasurement,
        State_WaitingForTestStarted,
        State_WaitingForColorDetected,
        State_WaitingForSettlePostMeasurement
    };
    TesterState                 State;

    bool                        HaveOldTime;
    UInt32                      OldTime;
    UInt32                      ActiveTimerMilliS;

    Color                       RenderColor;

    struct MeasurementResult : public ListNode<MeasurementResult>, public NewOverrideBase
    {
        MeasurementResult()
         :  DeviceMeasuredElapsedMilliS(0),
            TimedOutWaitingForTestStarted(false),
            TimedOutWaitingForColorDetected(false),
            StartTestSeconds(0.0),
            TestStartedSeconds(0.0)
        {}

        Color                   TargetColor;

        UInt32                  DeviceMeasuredElapsedMilliS;

        bool                    TimedOutWaitingForTestStarted;
        bool                    TimedOutWaitingForColorDetected;

        double                  StartTestSeconds;
        double                  TestStartedSeconds;
    };

    List<MeasurementResult>     Results;
    void clearMeasurementResults();

    MeasurementResult*          getActiveResult();

    StringBuffer			    ResultsString;
	String					    ReturnedResultString;
};

}} // namespace OVR::Util

#endif // OVR_Util_LatencyTest_h