aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/Util/Util_LatencyTest2.cpp
blob: 6fc8b1fb254d4ae336268735627e57d4c5a290dc (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/************************************************************************************

Filename    :   Util_LatencyTest2.cpp
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.

*************************************************************************************/

#include "Util_LatencyTest2.h"

#include "../OVR_CAPI.h"
#include "../Kernel/OVR_Log.h"
#include "../Kernel/OVR_Timer.h"


namespace OVR { namespace Util {

//-------------------------------------------------------------------------------------
// ***** LatencyTest2

LatencyTest2::LatencyTest2(SensorDevice* device)
 :  Handler(getThis())
 ,  TestActive(false)
 ,  StartTiming(-1)
 ,  LatencyMeasuredInSeconds(-1)
 ,  LastPixelReadMsg(NULL)
 ,  RenderColorValue(0)
 ,  NumMsgsBeforeSettle(0)
 ,  NumTestsSuccessful(0)
{
    if (device != NULL)
    {
        SetSensorDevice(device);
    }
}

LatencyTest2::~LatencyTest2()
{
    HmdDevice = NULL;
    LatencyTesterDev = NULL;
    
    Handler.RemoveHandlerFromDevices();
}

bool LatencyTest2::SetSensorDevice(SensorDevice* device)
{
    Lock::Locker devLocker(&TesterLock);

    // Enable/Disable pixel read from HMD
    if (device != HmdDevice)
    {
        Handler.RemoveHandlerFromDevices();

        HmdDevice = device;

        if (HmdDevice != NULL)
        {
            HmdDevice->AddMessageHandler(&Handler);
        }
    }

    return true;
}

bool LatencyTest2::SetDisplayDevice(LatencyTestDevice* device)
{
    Lock::Locker devLocker(&TesterLock);

    if (device != LatencyTesterDev)
    {
        LatencyTesterDev = device;
        if (LatencyTesterDev != NULL)
        {
            // Set display to initial (3 dashes).
            LatencyTestDisplay ltd(2, 0x40400040);
            LatencyTesterDev->SetDisplay(ltd);
        }
    }

    return true;
}

void LatencyTest2::BeginTest(double startTime)
{
    Lock::Locker devLocker(&TesterLock);

    if (!TestActive)
    {
        TestActive = true;
        NumMsgsBeforeSettle = 0;

        // Go to next pixel value
        //RenderColorValue = (RenderColorValue == 0) ? 255 : 0;
        RenderColorValue = (RenderColorValue + LT2_ColorIncrement) % 256;        
        RawStartTiming   = LastPixelReadMsg.RawSensorTime;
        
        if (startTime > 0.0)
            StartTiming = startTime;
        else
            StartTiming = ovr_GetTimeInSeconds();
          
    }
}

void LatencyTest2::handleMessage(const MessagePixelRead& msg)
{
    Lock::Locker devLocker(&TesterLock);

    // Hold onto the last message as we will use this when we start a new test
    LastPixelReadMsg = msg;

    // If color readback index is valid, store it in the lock-less queue.
    int readbackIndex = 0;
    if (FrameTimeRecord::ColorToReadbackIndex(&readbackIndex, msg.PixelReadValue))
    {
        RecentFrameSet.AddValue(readbackIndex, msg.FrameTimeSeconds);
        LockessRecords.SetState(RecentFrameSet);
    }

    NumMsgsBeforeSettle++;

    if (TestActive)
    {
        int pixelValueDiff = RenderColorValue - LastPixelReadMsg.PixelReadValue;
        int rawTimeDiff    = LastPixelReadMsg.RawFrameTime - RawStartTiming;

        if (pixelValueDiff < LT2_PixelTestThreshold && pixelValueDiff > -LT2_PixelTestThreshold)
        {
            TestActive = false;

            LatencyMeasuredInSeconds = LastPixelReadMsg.FrameTimeSeconds - StartTiming;
            RawLatencyMeasured       = rawTimeDiff;
            //LatencyMeasuredInSeconds = RawLatencyMeasured / 1000000.0;

            if(LatencyTesterDev && (NumTestsSuccessful % 5) == 0)
            {
                int displayNum = (int)(RawLatencyMeasured / 100.0);
                //int displayNum = NumMsgsBeforeSettle;
                //int displayNum = (int)(LatencyMeasuredInSeconds * 1000.0);
                LatencyTestDisplay ltd(1, displayNum);
                LatencyTesterDev->SetDisplay(ltd);
            }

            NumTestsSuccessful++;
        }
        else if (TestActive && (rawTimeDiff / 1000) > LT2_TimeoutWaitingForColorDetected)
        {
            TestActive = false;
            LatencyMeasuredInSeconds = -1;
        }
    }
}

LatencyTest2::PixelReadHandler::~PixelReadHandler()
{
    RemoveHandlerFromDevices();
}

void LatencyTest2::PixelReadHandler::OnMessage(const Message& msg)
{
    if(msg.Type == Message_PixelRead)
        pLatencyTestUtil->handleMessage(static_cast<const MessagePixelRead&>(msg));
}

bool LatencyTest2::DisplayScreenColor(Color& colorToDisplay)
{
    Lock::Locker devLocker(&TesterLock);
    colorToDisplay = Color(RenderColorValue, RenderColorValue, RenderColorValue, 255);

    return TestActive;
}

}} // namespace OVR::Util