oculus1

diff libovr/Src/OVR_LatencyTestImpl.cpp @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/OVR_LatencyTestImpl.cpp	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,774 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +Filename    :   OVR_LatencyTestImpl.cpp
     1.7 +Content     :   Oculus Latency Tester device implementation.
     1.8 +Created     :   March 7, 2013
     1.9 +Authors     :   Lee Cooper
    1.10 +
    1.11 +Copyright   :   Copyright 2013 Oculus VR, Inc. All Rights reserved.
    1.12 +
    1.13 +Use of this software is subject to the terms of the Oculus license
    1.14 +agreement provided at the time of installation or download, or which
    1.15 +otherwise accompanies this software in either electronic or hard copy form.
    1.16 +
    1.17 +*************************************************************************************/
    1.18 +
    1.19 +#include "OVR_LatencyTestImpl.h"
    1.20 +
    1.21 +namespace OVR {
    1.22 +
    1.23 +//-------------------------------------------------------------------------------------
    1.24 +// ***** Oculus Latency Tester specific packet data structures
    1.25 +
    1.26 +enum {    
    1.27 +    LatencyTester_VendorId  = Oculus_VendorId,
    1.28 +    LatencyTester_ProductId = 0x0101,
    1.29 +};
    1.30 +
    1.31 +// Reported data is little-endian now
    1.32 +static UInt16 DecodeUInt16(const UByte* buffer)
    1.33 +{
    1.34 +    return (UInt16(buffer[1]) << 8) | UInt16(buffer[0]);
    1.35 +}
    1.36 +
    1.37 +/* Unreferenced
    1.38 +static SInt16 DecodeSInt16(const UByte* buffer)
    1.39 +{
    1.40 +    return (SInt16(buffer[1]) << 8) | SInt16(buffer[0]);
    1.41 +}*/
    1.42 +
    1.43 +static void UnpackSamples(const UByte* buffer, UByte* r, UByte* g, UByte* b)
    1.44 +{
    1.45 +    *r = buffer[0];
    1.46 +    *g = buffer[1];
    1.47 +    *b = buffer[2];
    1.48 +}
    1.49 +
    1.50 +// Messages we handle.
    1.51 +enum LatencyTestMessageType
    1.52 +{
    1.53 +    LatencyTestMessage_None                 = 0,
    1.54 +    LatencyTestMessage_Samples              = 1,
    1.55 +    LatencyTestMessage_ColorDetected        = 2,
    1.56 +    LatencyTestMessage_TestStarted          = 3,
    1.57 +    LatencyTestMessage_Button               = 4,
    1.58 +    LatencyTestMessage_Unknown              = 0x100,
    1.59 +    LatencyTestMessage_SizeError            = 0x101,
    1.60 +};
    1.61 +
    1.62 +struct LatencyTestSample
    1.63 +{
    1.64 +    UByte Value[3];
    1.65 +};
    1.66 +
    1.67 +struct LatencyTestSamples
    1.68 +{
    1.69 +    UByte	SampleCount;
    1.70 +    UInt16	Timestamp;
    1.71 +
    1.72 +    LatencyTestSample Samples[20];
    1.73 +
    1.74 +    LatencyTestMessageType Decode(const UByte* buffer, int size)
    1.75 +    {
    1.76 +        if (size < 64)
    1.77 +        {
    1.78 +            return LatencyTestMessage_SizeError;
    1.79 +        }
    1.80 +
    1.81 +        SampleCount		= buffer[1];
    1.82 +        Timestamp		= DecodeUInt16(buffer + 2);
    1.83 +        
    1.84 +        for (UByte i = 0; i < SampleCount; i++)
    1.85 +        {
    1.86 +            UnpackSamples(buffer + 4 + (3 * i),  &Samples[i].Value[0], &Samples[i].Value[1], &Samples[i].Value[2]);
    1.87 +        }
    1.88 +
    1.89 +        return LatencyTestMessage_Samples;
    1.90 +    }
    1.91 +};
    1.92 +
    1.93 +struct LatencyTestSamplesMessage
    1.94 +{
    1.95 +    LatencyTestMessageType      Type;
    1.96 +    LatencyTestSamples        Samples;
    1.97 +};
    1.98 +
    1.99 +bool DecodeLatencyTestSamplesMessage(LatencyTestSamplesMessage* message, UByte* buffer, int size)
   1.100 +{
   1.101 +    memset(message, 0, sizeof(LatencyTestSamplesMessage));
   1.102 +
   1.103 +    if (size < 64)
   1.104 +    {
   1.105 +        message->Type = LatencyTestMessage_SizeError;
   1.106 +        return false;
   1.107 +    }
   1.108 +
   1.109 +    switch (buffer[0])
   1.110 +    {
   1.111 +    case LatencyTestMessage_Samples:
   1.112 +        message->Type = message->Samples.Decode(buffer, size);
   1.113 +        break;
   1.114 +
   1.115 +    default:
   1.116 +        message->Type = LatencyTestMessage_Unknown;
   1.117 +        break;
   1.118 +    }
   1.119 +
   1.120 +    return (message->Type < LatencyTestMessage_Unknown) && (message->Type != LatencyTestMessage_None);
   1.121 +}
   1.122 +
   1.123 +struct LatencyTestColorDetected
   1.124 +{
   1.125 +    UInt16	CommandID;
   1.126 +    UInt16	Timestamp;
   1.127 +    UInt16  Elapsed;
   1.128 +    UByte   TriggerValue[3];
   1.129 +    UByte   TargetValue[3];
   1.130 +
   1.131 +    LatencyTestMessageType Decode(const UByte* buffer, int size)
   1.132 +    {
   1.133 +        if (size < 13)
   1.134 +            return LatencyTestMessage_SizeError;
   1.135 +
   1.136 +        CommandID = DecodeUInt16(buffer + 1);
   1.137 +        Timestamp = DecodeUInt16(buffer + 3);
   1.138 +        Elapsed = DecodeUInt16(buffer + 5);
   1.139 +        memcpy(TriggerValue, buffer + 7, 3);
   1.140 +        memcpy(TargetValue, buffer + 10, 3);
   1.141 +
   1.142 +        return LatencyTestMessage_ColorDetected;
   1.143 +    }
   1.144 +};
   1.145 +
   1.146 +struct LatencyTestColorDetectedMessage
   1.147 +{
   1.148 +    LatencyTestMessageType    Type;
   1.149 +    LatencyTestColorDetected  ColorDetected;
   1.150 +};
   1.151 +
   1.152 +bool DecodeLatencyTestColorDetectedMessage(LatencyTestColorDetectedMessage* message, UByte* buffer, int size)
   1.153 +{
   1.154 +    memset(message, 0, sizeof(LatencyTestColorDetectedMessage));
   1.155 +
   1.156 +    if (size < 13)
   1.157 +    {
   1.158 +        message->Type = LatencyTestMessage_SizeError;
   1.159 +        return false;
   1.160 +    }
   1.161 +
   1.162 +    switch (buffer[0])
   1.163 +    {
   1.164 +    case LatencyTestMessage_ColorDetected:
   1.165 +        message->Type = message->ColorDetected.Decode(buffer, size);
   1.166 +        break;
   1.167 +
   1.168 +    default:
   1.169 +        message->Type = LatencyTestMessage_Unknown;
   1.170 +        break;
   1.171 +    }
   1.172 +
   1.173 +    return (message->Type < LatencyTestMessage_Unknown) && (message->Type != LatencyTestMessage_None);
   1.174 +}
   1.175 +
   1.176 +struct LatencyTestStarted
   1.177 +{
   1.178 +    UInt16	CommandID;
   1.179 +    UInt16	Timestamp;
   1.180 +    UByte   TargetValue[3];
   1.181 +
   1.182 +    LatencyTestMessageType Decode(const UByte* buffer, int size)
   1.183 +    {
   1.184 +        if (size < 8)
   1.185 +            return LatencyTestMessage_SizeError;
   1.186 +
   1.187 +        CommandID = DecodeUInt16(buffer + 1);
   1.188 +        Timestamp = DecodeUInt16(buffer + 3);
   1.189 +        memcpy(TargetValue, buffer + 5, 3);
   1.190 +
   1.191 +        return LatencyTestMessage_TestStarted;
   1.192 +    }
   1.193 +};
   1.194 +
   1.195 +struct LatencyTestStartedMessage
   1.196 +{
   1.197 +    LatencyTestMessageType  Type;
   1.198 +    LatencyTestStarted  TestStarted;
   1.199 +};
   1.200 +
   1.201 +bool DecodeLatencyTestStartedMessage(LatencyTestStartedMessage* message, UByte* buffer, int size)
   1.202 +{
   1.203 +    memset(message, 0, sizeof(LatencyTestStartedMessage));
   1.204 +
   1.205 +    if (size < 8)
   1.206 +    {
   1.207 +        message->Type = LatencyTestMessage_SizeError;
   1.208 +        return false;
   1.209 +    }
   1.210 +
   1.211 +    switch (buffer[0])
   1.212 +    {
   1.213 +    case LatencyTestMessage_TestStarted:
   1.214 +        message->Type = message->TestStarted.Decode(buffer, size);
   1.215 +        break;
   1.216 +
   1.217 +    default:
   1.218 +        message->Type = LatencyTestMessage_Unknown;
   1.219 +        break;
   1.220 +    }
   1.221 +
   1.222 +    return (message->Type < LatencyTestMessage_Unknown) && (message->Type != LatencyTestMessage_None);
   1.223 +}
   1.224 +
   1.225 +struct LatencyTestButton
   1.226 +{
   1.227 +    UInt16	CommandID;
   1.228 +    UInt16	Timestamp;
   1.229 +
   1.230 +    LatencyTestMessageType Decode(const UByte* buffer, int size)
   1.231 +    {
   1.232 +        if (size < 5)
   1.233 +            return LatencyTestMessage_SizeError;
   1.234 +
   1.235 +        CommandID = DecodeUInt16(buffer + 1);
   1.236 +        Timestamp = DecodeUInt16(buffer + 3);
   1.237 +
   1.238 +        return LatencyTestMessage_Button;
   1.239 +    }
   1.240 +};
   1.241 +
   1.242 +struct LatencyTestButtonMessage
   1.243 +{
   1.244 +    LatencyTestMessageType    Type;
   1.245 +    LatencyTestButton         Button;
   1.246 +};
   1.247 +
   1.248 +bool DecodeLatencyTestButtonMessage(LatencyTestButtonMessage* message, UByte* buffer, int size)
   1.249 +{
   1.250 +    memset(message, 0, sizeof(LatencyTestButtonMessage));
   1.251 +
   1.252 +    if (size < 5)
   1.253 +    {
   1.254 +        message->Type = LatencyTestMessage_SizeError;
   1.255 +        return false;
   1.256 +    }
   1.257 +
   1.258 +    switch (buffer[0])
   1.259 +    {
   1.260 +    case LatencyTestMessage_Button:
   1.261 +        message->Type = message->Button.Decode(buffer, size);
   1.262 +        break;
   1.263 +
   1.264 +    default:
   1.265 +        message->Type = LatencyTestMessage_Unknown;
   1.266 +        break;
   1.267 +    }
   1.268 +
   1.269 +    return (message->Type < LatencyTestMessage_Unknown) && (message->Type != LatencyTestMessage_None);
   1.270 +}
   1.271 +
   1.272 +struct LatencyTestConfigurationImpl
   1.273 +{
   1.274 +    enum  { PacketSize = 5 };
   1.275 +    UByte   Buffer[PacketSize];
   1.276 +
   1.277 +    OVR::LatencyTestConfiguration  Configuration;
   1.278 +
   1.279 +    LatencyTestConfigurationImpl(const OVR::LatencyTestConfiguration& configuration)
   1.280 +        : Configuration(configuration)
   1.281 +    {
   1.282 +        Pack();
   1.283 +    }
   1.284 +
   1.285 +    void Pack()
   1.286 +    {
   1.287 +        Buffer[0] = 5;
   1.288 +		Buffer[1] = UByte(Configuration.SendSamples);
   1.289 +		Buffer[2] = Configuration.Threshold.R;
   1.290 +        Buffer[3] = Configuration.Threshold.G;
   1.291 +        Buffer[4] = Configuration.Threshold.B;
   1.292 +    }
   1.293 +
   1.294 +    void Unpack()
   1.295 +    {
   1.296 +		Configuration.SendSamples = Buffer[1] != 0 ? true : false;
   1.297 +        Configuration.Threshold.R = Buffer[2];
   1.298 +        Configuration.Threshold.G = Buffer[3];
   1.299 +        Configuration.Threshold.B = Buffer[4];
   1.300 +    }
   1.301 +};
   1.302 +
   1.303 +struct LatencyTestCalibrateImpl
   1.304 +{
   1.305 +    enum  { PacketSize = 4 };
   1.306 +    UByte   Buffer[PacketSize];
   1.307 +
   1.308 +    Color CalibrationColor;
   1.309 +    
   1.310 +    LatencyTestCalibrateImpl(const Color& calibrationColor)
   1.311 +        : CalibrationColor(calibrationColor)
   1.312 +    {
   1.313 +        Pack();
   1.314 +    }
   1.315 +
   1.316 +    void Pack()
   1.317 +    {
   1.318 +        Buffer[0] = 7;
   1.319 +		Buffer[1] = CalibrationColor.R;
   1.320 +		Buffer[2] = CalibrationColor.G;
   1.321 +		Buffer[3] = CalibrationColor.B;
   1.322 +    }
   1.323 +
   1.324 +    void Unpack()
   1.325 +    {
   1.326 +        CalibrationColor.R = Buffer[1];
   1.327 +        CalibrationColor.G = Buffer[2];
   1.328 +        CalibrationColor.B = Buffer[3];
   1.329 +    }
   1.330 +};
   1.331 +
   1.332 +struct LatencyTestStartTestImpl
   1.333 +{
   1.334 +    enum  { PacketSize = 6 };
   1.335 +    UByte   Buffer[PacketSize];
   1.336 +
   1.337 +    Color TargetColor;
   1.338 +
   1.339 +    LatencyTestStartTestImpl(const Color& targetColor)
   1.340 +        : TargetColor(targetColor)
   1.341 +    {
   1.342 +        Pack();
   1.343 +    }
   1.344 +
   1.345 +    void Pack()
   1.346 +    {
   1.347 +        UInt16 commandID = 1;
   1.348 +
   1.349 +        Buffer[0] = 8;
   1.350 +		Buffer[1] = UByte(commandID  & 0xFF);
   1.351 +		Buffer[2] = UByte(commandID >> 8);
   1.352 +		Buffer[3] = TargetColor.R;
   1.353 +		Buffer[4] = TargetColor.G;
   1.354 +		Buffer[5] = TargetColor.B;
   1.355 +    }
   1.356 +
   1.357 +    void Unpack()
   1.358 +    {
   1.359 +//      UInt16 commandID = Buffer[1] | (UInt16(Buffer[2]) << 8);
   1.360 +        TargetColor.R = Buffer[3];
   1.361 +        TargetColor.G = Buffer[4];
   1.362 +        TargetColor.B = Buffer[5];
   1.363 +    }
   1.364 +};
   1.365 +
   1.366 +struct LatencyTestDisplayImpl
   1.367 +{
   1.368 +    enum  { PacketSize = 6 };
   1.369 +    UByte   Buffer[PacketSize];
   1.370 +
   1.371 +    OVR::LatencyTestDisplay  Display;
   1.372 +
   1.373 +    LatencyTestDisplayImpl(const OVR::LatencyTestDisplay& display)
   1.374 +        : Display(display)
   1.375 +    {
   1.376 +        Pack();
   1.377 +    }
   1.378 +
   1.379 +    void Pack()
   1.380 +    {
   1.381 +        Buffer[0] = 9;
   1.382 +        Buffer[1] = Display.Mode;
   1.383 +        Buffer[2] = UByte(Display.Value & 0xFF);
   1.384 +        Buffer[3] = UByte((Display.Value >> 8) & 0xFF);
   1.385 +        Buffer[4] = UByte((Display.Value >> 16) & 0xFF);
   1.386 +        Buffer[5] = UByte((Display.Value >> 24) & 0xFF);
   1.387 +    }
   1.388 +
   1.389 +    void Unpack()
   1.390 +    {
   1.391 +        Display.Mode = Buffer[1];
   1.392 +        Display.Value = UInt32(Buffer[2]) |
   1.393 +            (UInt32(Buffer[3]) << 8) |
   1.394 +            (UInt32(Buffer[4]) << 16) |
   1.395 +            (UInt32(Buffer[5]) << 24);
   1.396 +    }
   1.397 +};
   1.398 +
   1.399 +//-------------------------------------------------------------------------------------
   1.400 +// ***** LatencyTestDeviceFactory
   1.401 +
   1.402 +LatencyTestDeviceFactory LatencyTestDeviceFactory::Instance;
   1.403 +
   1.404 +void LatencyTestDeviceFactory::EnumerateDevices(EnumerateVisitor& visitor)
   1.405 +{
   1.406 +
   1.407 +    class LatencyTestEnumerator : public HIDEnumerateVisitor
   1.408 +    {
   1.409 +        // Assign not supported; suppress MSVC warning.
   1.410 +        void operator = (const LatencyTestEnumerator&) { }
   1.411 +
   1.412 +        DeviceFactory*     pFactory;
   1.413 +        EnumerateVisitor&  ExternalVisitor;   
   1.414 +    public:
   1.415 +        LatencyTestEnumerator(DeviceFactory* factory, EnumerateVisitor& externalVisitor)
   1.416 +            : pFactory(factory), ExternalVisitor(externalVisitor) { }
   1.417 +
   1.418 +        virtual bool MatchVendorProduct(UInt16 vendorId, UInt16 productId)
   1.419 +        {
   1.420 +            return pFactory->MatchVendorProduct(vendorId, productId);
   1.421 +        }
   1.422 +
   1.423 +        virtual void Visit(HIDDevice& device, const HIDDeviceDesc& desc)
   1.424 +        {
   1.425 +            OVR_UNUSED(device);
   1.426 +
   1.427 +            LatencyTestDeviceCreateDesc createDesc(pFactory, desc);
   1.428 +            ExternalVisitor.Visit(createDesc);
   1.429 +        }
   1.430 +    };
   1.431 +
   1.432 +    LatencyTestEnumerator latencyTestEnumerator(this, visitor);
   1.433 +    GetManagerImpl()->GetHIDDeviceManager()->Enumerate(&latencyTestEnumerator);
   1.434 +}
   1.435 +
   1.436 +bool LatencyTestDeviceFactory::MatchVendorProduct(UInt16 vendorId, UInt16 productId) const
   1.437 +{
   1.438 +    return ((vendorId == LatencyTester_VendorId) && (productId == LatencyTester_ProductId));                
   1.439 +}
   1.440 +
   1.441 +bool LatencyTestDeviceFactory::DetectHIDDevice(DeviceManager* pdevMgr, 
   1.442 +                                               const HIDDeviceDesc& desc)
   1.443 +{
   1.444 +    if (MatchVendorProduct(desc.VendorId, desc.ProductId))
   1.445 +    {
   1.446 +        LatencyTestDeviceCreateDesc createDesc(this, desc);
   1.447 +        return pdevMgr->AddDevice_NeedsLock(createDesc).GetPtr() != NULL;
   1.448 +    }
   1.449 +    return false;
   1.450 +}
   1.451 +
   1.452 +//-------------------------------------------------------------------------------------
   1.453 +// ***** LatencyTestDeviceCreateDesc
   1.454 +
   1.455 +DeviceBase* LatencyTestDeviceCreateDesc::NewDeviceInstance()
   1.456 +{
   1.457 +    return new LatencyTestDeviceImpl(this);
   1.458 +}
   1.459 +
   1.460 +bool LatencyTestDeviceCreateDesc::GetDeviceInfo(DeviceInfo* info) const
   1.461 +{
   1.462 +    if ((info->InfoClassType != Device_LatencyTester) &&
   1.463 +        (info->InfoClassType != Device_None))
   1.464 +        return false;
   1.465 +
   1.466 +    OVR_strcpy(info->ProductName,  DeviceInfo::MaxNameLength, HIDDesc.Product.ToCStr());
   1.467 +    OVR_strcpy(info->Manufacturer, DeviceInfo::MaxNameLength, HIDDesc.Manufacturer.ToCStr());
   1.468 +    info->Type    = Device_LatencyTester;
   1.469 +    info->Version = 0;
   1.470 +
   1.471 +    if (info->InfoClassType == Device_LatencyTester)
   1.472 +    {
   1.473 +        SensorInfo* sinfo = (SensorInfo*)info;
   1.474 +        sinfo->VendorId  = HIDDesc.VendorId;
   1.475 +        sinfo->ProductId = HIDDesc.ProductId;
   1.476 +        OVR_strcpy(sinfo->SerialNumber, sizeof(sinfo->SerialNumber),HIDDesc.SerialNumber.ToCStr());
   1.477 +    }
   1.478 +    return true;
   1.479 +}
   1.480 +
   1.481 +//-------------------------------------------------------------------------------------
   1.482 +// ***** LatencyTestDevice
   1.483 +
   1.484 +LatencyTestDeviceImpl::LatencyTestDeviceImpl(LatencyTestDeviceCreateDesc* createDesc)
   1.485 +    : OVR::HIDDeviceImpl<OVR::LatencyTestDevice>(createDesc, 0)
   1.486 +{
   1.487 +}
   1.488 +
   1.489 +LatencyTestDeviceImpl::~LatencyTestDeviceImpl()
   1.490 +{
   1.491 +    // Check that Shutdown() was called.
   1.492 +    OVR_ASSERT(!pCreateDesc->pDevice);    
   1.493 +}
   1.494 +
   1.495 +// Internal creation APIs.
   1.496 +bool LatencyTestDeviceImpl::Initialize(DeviceBase* parent)
   1.497 +{
   1.498 +    if (HIDDeviceImpl<OVR::LatencyTestDevice>::Initialize(parent))
   1.499 +    {
   1.500 +        LogText("OVR::LatencyTestDevice initialized.\n");
   1.501 +        return true;
   1.502 +    }
   1.503 +
   1.504 +    return false;
   1.505 +}
   1.506 +
   1.507 +void LatencyTestDeviceImpl::Shutdown()
   1.508 +{   
   1.509 +    HIDDeviceImpl<OVR::LatencyTestDevice>::Shutdown();
   1.510 +
   1.511 +    LogText("OVR::LatencyTestDevice - Closed '%s'\n", getHIDDesc()->Path.ToCStr());
   1.512 +}
   1.513 +
   1.514 +void LatencyTestDeviceImpl::OnInputReport(UByte* pData, UInt32 length)
   1.515 +{
   1.516 +    
   1.517 +    bool processed = false;
   1.518 +    if (!processed)
   1.519 +    {
   1.520 +        LatencyTestSamplesMessage message; 
   1.521 +        if (DecodeLatencyTestSamplesMessage(&message, pData, length))     
   1.522 +        {
   1.523 +            processed = true;
   1.524 +            onLatencyTestSamplesMessage(&message);
   1.525 +        }
   1.526 +    }
   1.527 +
   1.528 +    if (!processed)
   1.529 +    {
   1.530 +        LatencyTestColorDetectedMessage message; 
   1.531 +        if (DecodeLatencyTestColorDetectedMessage(&message, pData, length))     
   1.532 +        {
   1.533 +            processed = true;
   1.534 +            onLatencyTestColorDetectedMessage(&message);
   1.535 +        }
   1.536 +    }
   1.537 +
   1.538 +    if (!processed)
   1.539 +    {
   1.540 +        LatencyTestStartedMessage message; 
   1.541 +        if (DecodeLatencyTestStartedMessage(&message, pData, length))     
   1.542 +        {
   1.543 +            processed = true;
   1.544 +            onLatencyTestStartedMessage(&message);
   1.545 +        }
   1.546 +    }
   1.547 +
   1.548 +    if (!processed)
   1.549 +    {
   1.550 +        LatencyTestButtonMessage message; 
   1.551 +        if (DecodeLatencyTestButtonMessage(&message, pData, length))     
   1.552 +        {
   1.553 +            processed = true;
   1.554 +            onLatencyTestButtonMessage(&message);
   1.555 +        }
   1.556 +    }
   1.557 +}
   1.558 +
   1.559 +bool LatencyTestDeviceImpl::SetConfiguration(const OVR::LatencyTestConfiguration& configuration, bool waitFlag)
   1.560 +{  
   1.561 +    bool                result = false;
   1.562 +    ThreadCommandQueue* queue = GetManagerImpl()->GetThreadQueue();
   1.563 +
   1.564 +    if (GetManagerImpl()->GetThreadId() != OVR::GetCurrentThreadId())
   1.565 +    {
   1.566 +        if (!waitFlag)
   1.567 +        {
   1.568 +            return queue->PushCall(this, &LatencyTestDeviceImpl::setConfiguration, configuration);
   1.569 +        }
   1.570 +
   1.571 +        if (!queue->PushCallAndWaitResult(  this, 
   1.572 +            &LatencyTestDeviceImpl::setConfiguration,
   1.573 +            &result, 
   1.574 +            configuration))
   1.575 +        {
   1.576 +            return false;
   1.577 +        }
   1.578 +    }
   1.579 +    else
   1.580 +        return setConfiguration(configuration);
   1.581 +
   1.582 +    return result;
   1.583 +}
   1.584 +
   1.585 +bool LatencyTestDeviceImpl::setConfiguration(const OVR::LatencyTestConfiguration& configuration)
   1.586 +{
   1.587 +    LatencyTestConfigurationImpl ltc(configuration);
   1.588 +    return GetInternalDevice()->SetFeatureReport(ltc.Buffer, LatencyTestConfigurationImpl::PacketSize);
   1.589 +}
   1.590 +
   1.591 +bool LatencyTestDeviceImpl::GetConfiguration(OVR::LatencyTestConfiguration* configuration)
   1.592 +{  
   1.593 +    bool result = false;
   1.594 +
   1.595 +	ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
   1.596 +    if (!pQueue->PushCallAndWaitResult(this, &LatencyTestDeviceImpl::getConfiguration, &result, configuration))
   1.597 +        return false;
   1.598 +
   1.599 +    return result;
   1.600 +}
   1.601 +
   1.602 +bool LatencyTestDeviceImpl::getConfiguration(OVR::LatencyTestConfiguration* configuration)
   1.603 +{
   1.604 +    LatencyTestConfigurationImpl ltc(*configuration);
   1.605 +    if (GetInternalDevice()->GetFeatureReport(ltc.Buffer, LatencyTestConfigurationImpl::PacketSize))
   1.606 +    {
   1.607 +        ltc.Unpack();
   1.608 +        *configuration = ltc.Configuration;
   1.609 +        return true;
   1.610 +    }
   1.611 +
   1.612 +    return false;
   1.613 +}
   1.614 +
   1.615 +bool LatencyTestDeviceImpl::SetCalibrate(const Color& calibrationColor, bool waitFlag)
   1.616 +{
   1.617 +    bool                result = false;
   1.618 +    ThreadCommandQueue* queue = GetManagerImpl()->GetThreadQueue();
   1.619 +
   1.620 +    if (!waitFlag)
   1.621 +    {
   1.622 +        return queue->PushCall(this, &LatencyTestDeviceImpl::setCalibrate, calibrationColor);
   1.623 +    }
   1.624 +
   1.625 +    if (!queue->PushCallAndWaitResult(  this, 
   1.626 +                                        &LatencyTestDeviceImpl::setCalibrate,
   1.627 +                                        &result, 
   1.628 +                                        calibrationColor))
   1.629 +    {
   1.630 +        return false;
   1.631 +    }
   1.632 +
   1.633 +    return result;
   1.634 +}
   1.635 +
   1.636 +bool LatencyTestDeviceImpl::setCalibrate(const Color& calibrationColor)
   1.637 +{
   1.638 +    LatencyTestCalibrateImpl ltc(calibrationColor);
   1.639 +    return GetInternalDevice()->SetFeatureReport(ltc.Buffer, LatencyTestCalibrateImpl::PacketSize);
   1.640 +}
   1.641 +
   1.642 +bool LatencyTestDeviceImpl::SetStartTest(const Color& targetColor, bool waitFlag)
   1.643 +{
   1.644 +    bool                result = false;
   1.645 +    ThreadCommandQueue* queue = GetManagerImpl()->GetThreadQueue();
   1.646 +
   1.647 +    if (!waitFlag)
   1.648 +    {
   1.649 +        return queue->PushCall(this, &LatencyTestDeviceImpl::setStartTest, targetColor);
   1.650 +    }
   1.651 +
   1.652 +    if (!queue->PushCallAndWaitResult(  this, 
   1.653 +                                        &LatencyTestDeviceImpl::setStartTest,
   1.654 +                                        &result, 
   1.655 +                                        targetColor))
   1.656 +    {
   1.657 +        return false;
   1.658 +    }
   1.659 +
   1.660 +    return result;
   1.661 +}
   1.662 +
   1.663 +bool LatencyTestDeviceImpl::setStartTest(const Color& targetColor)
   1.664 +{
   1.665 +    LatencyTestStartTestImpl ltst(targetColor);
   1.666 +    return GetInternalDevice()->SetFeatureReport(ltst.Buffer, LatencyTestStartTestImpl::PacketSize);
   1.667 +}
   1.668 +
   1.669 +bool LatencyTestDeviceImpl::SetDisplay(const OVR::LatencyTestDisplay& display, bool waitFlag)
   1.670 +{
   1.671 +    bool                 result = false;
   1.672 +    ThreadCommandQueue * queue = GetManagerImpl()->GetThreadQueue();
   1.673 +
   1.674 +    if (!waitFlag)
   1.675 +    {
   1.676 +        return queue->PushCall(this, &LatencyTestDeviceImpl::setDisplay, display);
   1.677 +    }
   1.678 +
   1.679 +    if (!queue->PushCallAndWaitResult(  this, 
   1.680 +                                        &LatencyTestDeviceImpl::setDisplay,
   1.681 +                                        &result, 
   1.682 +                                        display))
   1.683 +    {
   1.684 +        return false;
   1.685 +    }
   1.686 +
   1.687 +    return result;
   1.688 +}
   1.689 +
   1.690 +bool LatencyTestDeviceImpl::setDisplay(const OVR::LatencyTestDisplay& display)
   1.691 +{
   1.692 +    LatencyTestDisplayImpl ltd(display);
   1.693 +    return GetInternalDevice()->SetFeatureReport(ltd.Buffer, LatencyTestDisplayImpl::PacketSize);
   1.694 +}
   1.695 +
   1.696 +void LatencyTestDeviceImpl::onLatencyTestSamplesMessage(LatencyTestSamplesMessage* message)
   1.697 +{
   1.698 +
   1.699 +    if (message->Type != LatencyTestMessage_Samples)
   1.700 +        return;
   1.701 +
   1.702 +    LatencyTestSamples& s = message->Samples;
   1.703 +
   1.704 +    // Call OnMessage() within a lock to avoid conflicts with handlers.
   1.705 +    Lock::Locker scopeLock(HandlerRef.GetLock());
   1.706 +  
   1.707 +    if (HandlerRef.GetHandler())
   1.708 +    {
   1.709 +        MessageLatencyTestSamples samples(this);
   1.710 +        for (UByte i = 0; i < s.SampleCount; i++)
   1.711 +        {            
   1.712 +            samples.Samples.PushBack(Color(s.Samples[i].Value[0], s.Samples[i].Value[1], s.Samples[i].Value[2]));
   1.713 +        }
   1.714 +
   1.715 +        HandlerRef.GetHandler()->OnMessage(samples);
   1.716 +    }
   1.717 +}
   1.718 +
   1.719 +void LatencyTestDeviceImpl::onLatencyTestColorDetectedMessage(LatencyTestColorDetectedMessage* message)
   1.720 +{
   1.721 +    if (message->Type != LatencyTestMessage_ColorDetected)
   1.722 +        return;
   1.723 +
   1.724 +    LatencyTestColorDetected& s = message->ColorDetected;
   1.725 +
   1.726 +    // Call OnMessage() within a lock to avoid conflicts with handlers.
   1.727 +    Lock::Locker scopeLock(HandlerRef.GetLock());
   1.728 +
   1.729 +    if (HandlerRef.GetHandler())
   1.730 +    {
   1.731 +        MessageLatencyTestColorDetected detected(this);
   1.732 +        detected.Elapsed = s.Elapsed;
   1.733 +        detected.DetectedValue = Color(s.TriggerValue[0], s.TriggerValue[1], s.TriggerValue[2]);
   1.734 +        detected.TargetValue = Color(s.TargetValue[0], s.TargetValue[1], s.TargetValue[2]);
   1.735 +
   1.736 +        HandlerRef.GetHandler()->OnMessage(detected);
   1.737 +    }
   1.738 +}
   1.739 +
   1.740 +void LatencyTestDeviceImpl::onLatencyTestStartedMessage(LatencyTestStartedMessage* message)
   1.741 +{
   1.742 +    if (message->Type != LatencyTestMessage_TestStarted)
   1.743 +        return;
   1.744 +
   1.745 +    LatencyTestStarted& ts = message->TestStarted;
   1.746 +
   1.747 +    // Call OnMessage() within a lock to avoid conflicts with handlers.
   1.748 +    Lock::Locker scopeLock(HandlerRef.GetLock());
   1.749 +
   1.750 +    if (HandlerRef.GetHandler())
   1.751 +    {
   1.752 +        MessageLatencyTestStarted started(this);
   1.753 +        started.TargetValue = Color(ts.TargetValue[0], ts.TargetValue[1], ts.TargetValue[2]);
   1.754 +
   1.755 +        HandlerRef.GetHandler()->OnMessage(started);
   1.756 +    }
   1.757 +}
   1.758 +
   1.759 +void LatencyTestDeviceImpl::onLatencyTestButtonMessage(LatencyTestButtonMessage* message)
   1.760 +{
   1.761 +    if (message->Type != LatencyTestMessage_Button)
   1.762 +        return;
   1.763 +
   1.764 +//  LatencyTestButton& s = message->Button;
   1.765 +
   1.766 +    // Call OnMessage() within a lock to avoid conflicts with handlers.
   1.767 +    Lock::Locker scopeLock(HandlerRef.GetLock());
   1.768 +
   1.769 +    if (HandlerRef.GetHandler())
   1.770 +    {
   1.771 +        MessageLatencyTestButton button(this);
   1.772 +
   1.773 +        HandlerRef.GetHandler()->OnMessage(button);
   1.774 +    }
   1.775 +}
   1.776 +
   1.777 +} // namespace OVR