aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/OVR_Common_HMDDevice.cpp
blob: 6bedcbef84a76ffd9402d98d603a3759812fc0a9 (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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/************************************************************************************

Filename    :   OVR_Common_HMDDevice.cpp
Content     :   
Created     :
Authors     :

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.

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

// Should be #included from the relevant OVR_YourPlatformHere_HMDDevice.cpp

#include "Kernel/OVR_Alg.h"

//-------------------------------------------------------------------------------------
// ***** HMDDeviceCreateDesc

DeviceBase* HMDDeviceCreateDesc::NewDeviceInstance()
{
    return new HMDDevice(this);
}

void  HMDDeviceCreateDesc::SetScreenParameters(int x, int y,
                              int hres, int vres,
                              float hsize, float vsize,
                              float vCenterFromTopInMeters, float lensSeparationInMeters)
{
    Desktop.X = x;
    Desktop.Y = y;
    ResolutionInPixels = Sizei(hres, vres);
    ScreenSizeInMeters = Sizef(hsize, vsize);
    VCenterFromTopInMeters = vCenterFromTopInMeters;
    LensSeparationInMeters = lensSeparationInMeters;

    Contents |= Contents_Screen;
}


void HMDDeviceCreateDesc::SetDistortion(const float* dks)
{
    for (int i = 0; i < 4; i++)
        DistortionK[i] = dks[i];
    // TODO: add DistortionEqn
    Contents |= Contents_Distortion;
}

HmdTypeEnum HMDDeviceCreateDesc::GetHmdType() const
{
    // Determine the HMD model
    // The closest thing we have to a dependable model indicator are the 
    // the screen characteristics.  Additionally we can check the sensor
    // (on attached devices) to further refine our guess
    HmdTypeEnum hmdType = HmdType_Unknown;

    if ( ResolutionInPixels.w == 1280 )
    {
        if ( ScreenSizeInMeters.w > 0.1497f && ScreenSizeInMeters.w < 0.1498f )
            hmdType = HmdType_DK1;
        else
            hmdType = HmdType_DKProto;
    }
    else if ( ResolutionInPixels.w == 1920 )
    {
        // DKHD protoypes, all 1920x1080
        if ( ScreenSizeInMeters.w > 0.1209f && ScreenSizeInMeters.w < 0.1210f )
        {
            // Screen size 0.12096 x 0.06804
            hmdType = HmdType_DKHDProto;
        }
        else if ( ScreenSizeInMeters.w > 0.1257f && ScreenSizeInMeters.w < 0.1258f )
        {
            // Screen size 0.125 x 0.071
            // Could be a HmdType_DKHDProto566Mi, HmdType_CrystalCoveProto, or DK2
            // - most likely the latter.
            hmdType = HmdType_DK2;

            // If available, check the sensor to determine exactly which variant this is
            if (pDevice)
            {
                Ptr<SensorDevice> sensor = *((HMDDevice*)pDevice)->GetSensor();
                
                SensorInfo sinfo;
                if (sensor && sensor->GetDeviceInfo(&sinfo))
                {
                    if (sinfo.ProductId == 1)
                    {
                        hmdType = HmdType_DKHDProto566Mi;
                    }
                    else
                    {   // Crystal Cove uses 0.# firmware, DK2 uses 1.#
                        int firm_major = Alg::DecodeBCD((sinfo.Version >> 8) & 0x00ff);
                        int firm_minor = Alg::DecodeBCD(sinfo.Version & 0xff);
                        OVR_UNUSED(firm_minor);
                        if (firm_major == 0)
                            hmdType = HmdType_CrystalCoveProto;
                        else
                            hmdType = HmdType_DK2;
                    }
                }
            }
        }
        else if (ScreenSizeInMeters.w > 0.1295f && ScreenSizeInMeters.w < 0.1297f)
        {
            // Screen size 0.1296 x 0.0729
            hmdType = HmdType_DKHD2Proto;
        }
    }
    
    OVR_ASSERT( hmdType != HmdType_Unknown );
    return hmdType;
}

bool HMDDeviceCreateDesc::GetDeviceInfo(DeviceInfo* info) const
{
    if ((info->InfoClassType != Device_HMD) &&
        (info->InfoClassType != Device_None))
        return false;

    HmdTypeEnum hmdType = GetHmdType();
    char const* deviceName = "Oculus HMD";
    switch (hmdType)
    {
        case HmdType_DKProto:          deviceName = "Oculus Rift Prototype";    break;
        case HmdType_DK1:              deviceName = "Oculus Rift DK1";          break;
        case HmdType_DKHDProto:        deviceName = "Oculus Rift DKHD";         break;
        case HmdType_DKHD2Proto:       deviceName = "Oculus Rift DKHD2";        break;
        case HmdType_DKHDProto566Mi:   deviceName = "Oculus Rift DKHD 566 Mi";  break;
        case HmdType_CrystalCoveProto: deviceName = "Oculus Rift Crystal Cove"; break;
        case HmdType_DK2:              deviceName = "Oculus Rift DK2";          break;
        default:                       deviceName = "Oculus HMD";               break;
    }
   
    info->ProductName = deviceName;
    info->Manufacturer = "Oculus VR";
    info->Type    = Device_HMD;
    info->Version = 0;

    // Display detection.
    if (info->InfoClassType == Device_HMD)
    {
        HMDInfo* hmdInfo = static_cast<HMDInfo*>(info);

        hmdInfo->HmdType                = hmdType;
        hmdInfo->DesktopX               = Desktop.X;
        hmdInfo->DesktopY               = Desktop.Y;
        hmdInfo->ResolutionInPixels     = ResolutionInPixels;                
        hmdInfo->ScreenSizeInMeters     = ScreenSizeInMeters;        // Includes ScreenGapSizeInMeters
        hmdInfo->ScreenGapSizeInMeters  = 0.0f;
        hmdInfo->CenterFromTopInMeters  = VCenterFromTopInMeters;
        hmdInfo->LensSeparationInMeters = LensSeparationInMeters;
        // TODO: any other information we get from the hardware itself should be added to this list

        switch ( hmdInfo->HmdType )
        {
        case HmdType_DKProto:
            // WARNING - estimated.
            hmdInfo->Shutter.Type                             = HmdShutter_RollingTopToBottom;
            hmdInfo->Shutter.VsyncToNextVsync                 = ( 1.0f / 60.0f );
            hmdInfo->Shutter.VsyncToFirstScanline             = 0.000052f;
            hmdInfo->Shutter.FirstScanlineToLastScanline      = 0.016580f;
            hmdInfo->Shutter.PixelSettleTime                  = 0.015f; // estimated.
            hmdInfo->Shutter.PixelPersistence                 = hmdInfo->Shutter.VsyncToNextVsync; // Full persistence
            break;
        case HmdType_DK1:
            // Data from specs.
            hmdInfo->Shutter.Type                             = HmdShutter_RollingTopToBottom;
            hmdInfo->Shutter.VsyncToNextVsync                 = ( 1.0f / 60.0f );
            hmdInfo->Shutter.VsyncToFirstScanline             = 0.00018226f;
            hmdInfo->Shutter.FirstScanlineToLastScanline      = 0.01620089f;
            hmdInfo->Shutter.PixelSettleTime                  = 0.017f; // estimated.
            hmdInfo->Shutter.PixelPersistence                 = hmdInfo->Shutter.VsyncToNextVsync; // Full persistence
            break;
        case HmdType_DKHDProto:
            // Data from specs.
            hmdInfo->Shutter.Type                             = HmdShutter_RollingRightToLeft;
            hmdInfo->Shutter.VsyncToNextVsync                 = ( 1.0f / 60.0f );
            hmdInfo->Shutter.VsyncToFirstScanline             = 0.0000859f;
            hmdInfo->Shutter.FirstScanlineToLastScanline      = 0.0164948f;
            hmdInfo->Shutter.PixelSettleTime                  = 0.012f; // estimated.
            hmdInfo->Shutter.PixelPersistence                 = hmdInfo->Shutter.VsyncToNextVsync; // Full persistence
            break;
        case HmdType_DKHD2Proto:
            // Data from specs.
            hmdInfo->Shutter.Type                             = HmdShutter_RollingRightToLeft;
            hmdInfo->Shutter.VsyncToNextVsync                 = ( 1.0f / 60.0f );
            hmdInfo->Shutter.VsyncToFirstScanline             = 0.000052f;
            hmdInfo->Shutter.FirstScanlineToLastScanline      = 0.016580f;
            hmdInfo->Shutter.PixelSettleTime                  = 0.015f; // estimated.
            hmdInfo->Shutter.PixelPersistence                 = hmdInfo->Shutter.VsyncToNextVsync; // Full persistence
            break;
        case HmdType_DKHDProto566Mi:
#if 0
            // Low-persistence global shutter
            hmdInfo->Shutter.Type                             = HmdShutter_Global;
            hmdInfo->Shutter.VsyncToNextVsync                 = ( 1.0f / 76.0f );
            hmdInfo->Shutter.VsyncToFirstScanline             = 0.0000273f + 0.0131033f;    // Global shutter - first visible scan line is actually the last!
            hmdInfo->Shutter.FirstScanlineToLastScanline      = 0.000f;                     // Global shutter - all visible at once.
            hmdInfo->Shutter.PixelSettleTime                  = 0.0f;                       // <100us
            hmdInfo->Shutter.PixelPersistence                 = 0.18f * hmdInfo->Shutter.VsyncToNextVsync;     // Confgurable - currently set to 18% of total frame.
#else
            // Low-persistence rolling shutter
            hmdInfo->Shutter.Type                             = HmdShutter_RollingRightToLeft;
            hmdInfo->Shutter.VsyncToNextVsync                 = ( 1.0f / 76.0f );
            hmdInfo->Shutter.VsyncToFirstScanline             = 0.0000273f;
            hmdInfo->Shutter.FirstScanlineToLastScanline      = 0.0131033f;
            hmdInfo->Shutter.PixelSettleTime                  = 0.0f;                       // <100us
            hmdInfo->Shutter.PixelPersistence                 = 0.18f * hmdInfo->Shutter.VsyncToNextVsync;     // Confgurable - currently set to 18% of total frame.
#endif
            break;
        case HmdType_CrystalCoveProto:
            // Low-persistence rolling shutter
            hmdInfo->Shutter.Type                             = HmdShutter_RollingRightToLeft;
            hmdInfo->Shutter.VsyncToNextVsync                 = ( 1.0f / 76.0f );
            hmdInfo->Shutter.VsyncToFirstScanline             = 0.0000273f;
            hmdInfo->Shutter.FirstScanlineToLastScanline      = 0.0131033f;
            hmdInfo->Shutter.PixelSettleTime                  = 0.0f;                       // <100us
            hmdInfo->Shutter.PixelPersistence                 = 0.18f * hmdInfo->Shutter.VsyncToNextVsync;     // Confgurable - currently set to 18% of total frame.
            break;
        case HmdType_DK2:
            // Low-persistence rolling shutter
            hmdInfo->Shutter.Type                             = HmdShutter_RollingRightToLeft;
            hmdInfo->Shutter.VsyncToNextVsync                 = ( 1.0f / 76.0f );
            hmdInfo->Shutter.VsyncToFirstScanline             = 0.0000273f;
            hmdInfo->Shutter.FirstScanlineToLastScanline      = 0.0131033f;
            hmdInfo->Shutter.PixelSettleTime                  = 0.0f;                       // <100us
            hmdInfo->Shutter.PixelPersistence                 = 0.18f * hmdInfo->Shutter.VsyncToNextVsync;     // Confgurable - currently set to 18% of total frame.
            break;
        default: OVR_ASSERT ( false ); break;
        }


        OVR_strcpy(hmdInfo->DisplayDeviceName, sizeof(hmdInfo->DisplayDeviceName),
                   DisplayDeviceName.ToCStr());
#if   defined(OVR_OS_WIN32)
        // Nothing special for Win32.
#elif defined(OVR_OS_MAC)
        hmdInfo->DisplayId = DisplayId;
#elif defined(OVR_OS_LINUX)
        hmdInfo->DisplayId = DisplayId;
#elif defined(OVR_OS_ANDROID)
        hmdInfo->DisplayId = DisplayId;
#else
#error Unknown platform
#endif

    }

    return true;
}





//-------------------------------------------------------------------------------------
// ***** HMDDevice

HMDDevice::HMDDevice(HMDDeviceCreateDesc* createDesc)
    : OVR::DeviceImpl<OVR::HMDDevice>(createDesc, 0)
{
}
HMDDevice::~HMDDevice()
{
}

bool HMDDevice::Initialize(DeviceBase* parent)
{
    pParent = parent;
    return true;
}
void HMDDevice::Shutdown()
{
    ProfileName.Clear();
    pCachedProfile.Clear();
    pParent.Clear();
}

Profile* HMDDevice::GetProfile()
{    
    // Loads and returns a cached profile based on this device and current user
    if (pCachedProfile == NULL)
    {
        ProfileManager* mgr = GetManager()->GetProfileManager();
        const char* profile_name = GetProfileName();
        if (profile_name && profile_name[0])
            pCachedProfile = *mgr->GetProfile(this, profile_name);

        if (pCachedProfile == NULL)
            pCachedProfile = *mgr->GetDefaultProfile(this);
        
    }
    return pCachedProfile.GetPtr();
}

const char* HMDDevice::GetProfileName()
{
    if (ProfileName.IsEmpty())
    {   // If the profile name has not been initialized then
        // retrieve the stored default user for this specific device
        ProfileManager* mgr = GetManager()->GetProfileManager();
        const char* name = mgr->GetDefaultUser(this);
        ProfileName = name;
    }
    
    return ProfileName.ToCStr();
}

bool HMDDevice::SetProfileName(const char* name)
{
    if (ProfileName == name)
        return true;   // already set
    
    // Flush the old profile
    pCachedProfile.Clear();
    if (!name)
    {
        ProfileName.Clear();
        return false;
    }

    // Set the name and attempt to cache the profile
    ProfileName = name;
    if (GetProfile())
    {
        return true;
    }
    else
    {
        ProfileName.Clear();
        return false;
    }
}

OVR::SensorDevice* HMDDevice::GetSensor()
{
    // Just return first sensor found since we have no way to match it yet.    

	// Create DK2 sensor if it exists otherwise create first DK1 sensor.
    SensorDevice* sensor = NULL;

    DeviceEnumerator<SensorDevice> enumerator = GetManager()->EnumerateDevices<SensorDevice>();

    while(enumerator.GetType() != Device_None)
    {
        SensorInfo info;
        enumerator.GetDeviceInfo(&info);
      
        if (info.ProductId == Device_Tracker2_ProductId)
        {
            sensor = enumerator.CreateDevice();
            break;
        }

        enumerator.Next();
    }

    if (sensor == NULL)
    {
        sensor = GetManager()->EnumerateDevices<SensorDevice>().CreateDevice();
    }    

    if (sensor)
	{
        sensor->SetCoordinateFrame(SensorDevice::Coord_HMD);
	}

    return sensor;
}