Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_Common_HMDDevice.cpp
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : OVR_Common_HMDDevice.cpp
4 Content :
5 Created :
6 Authors :
7 
8 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
9 
10 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
14 
15 You may obtain a copy of the License at
16 
17 http://www.oculusvr.com/licenses/LICENSE-3.1
18 
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24 
25 *************************************************************************************/
26 
27 // Should be #included from the relevant OVR_YourPlatformHere_HMDDevice.cpp
28 
29 #include "Kernel/OVR_Alg.h"
30 
31 //-------------------------------------------------------------------------------------
32 // ***** HMDDeviceCreateDesc
33 
34 DeviceBase* HMDDeviceCreateDesc::NewDeviceInstance()
35 {
36  return new HMDDevice(this);
37 }
38 
39 void HMDDeviceCreateDesc::SetScreenParameters(int x, int y,
40  int hres, int vres,
41  float hsize, float vsize,
42  float vCenterFromTopInMeters, float lensSeparationInMeters)
43 {
44  Desktop.X = x;
45  Desktop.Y = y;
46  ResolutionInPixels = Sizei(hres, vres);
47  ScreenSizeInMeters = Sizef(hsize, vsize);
48  VCenterFromTopInMeters = vCenterFromTopInMeters;
49  LensSeparationInMeters = lensSeparationInMeters;
50 
51  Contents |= Contents_Screen;
52 }
53 
54 
55 void HMDDeviceCreateDesc::SetDistortion(const float* dks)
56 {
57  for (int i = 0; i < 4; i++)
58  DistortionK[i] = dks[i];
59  // TODO: add DistortionEqn
60  Contents |= Contents_Distortion;
61 }
62 
63 HmdTypeEnum HMDDeviceCreateDesc::GetHmdType() const
64 {
65  // Determine the HMD model
66  // The closest thing we have to a dependable model indicator are the
67  // the screen characteristics. Additionally we can check the sensor
68  // (on attached devices) to further refine our guess
69  HmdTypeEnum hmdType = HmdType_Unknown;
70 
71  if ( ResolutionInPixels.w == 1280 )
72  {
73  if ( ScreenSizeInMeters.w > 0.1497f && ScreenSizeInMeters.w < 0.1498f )
74  hmdType = HmdType_DK1;
75  else
76  hmdType = HmdType_DKProto;
77  }
78  else if ( ResolutionInPixels.w == 1920 )
79  {
80  // DKHD protoypes, all 1920x1080
81  if ( ScreenSizeInMeters.w > 0.1209f && ScreenSizeInMeters.w < 0.1210f )
82  {
83  // Screen size 0.12096 x 0.06804
84  hmdType = HmdType_DKHDProto;
85  }
86  else if ( ScreenSizeInMeters.w > 0.1257f && ScreenSizeInMeters.w < 0.1258f )
87  {
88  // Screen size 0.125 x 0.071
89  // Could be a HmdType_DKHDProto566Mi, HmdType_CrystalCoveProto, or DK2
90  // - most likely the latter.
91  hmdType = HmdType_DK2;
92 
93  // If available, check the sensor to determine exactly which variant this is
94  if (pDevice)
95  {
96  Ptr<SensorDevice> sensor = *((HMDDevice*)pDevice)->GetSensor();
97 
98  SensorInfo sinfo;
99  if (sensor && sensor->GetDeviceInfo(&sinfo))
100  {
101  if (sinfo.ProductId == 1)
102  {
103  hmdType = HmdType_DKHDProto566Mi;
104  }
105  else
106  { // Crystal Cove uses 0.# firmware, DK2 uses 1.#
107  int firm_major = Alg::DecodeBCD((sinfo.Version >> 8) & 0x00ff);
108  int firm_minor = Alg::DecodeBCD(sinfo.Version & 0xff);
109  OVR_UNUSED(firm_minor);
110  if (firm_major == 0)
111  hmdType = HmdType_CrystalCoveProto;
112  else
113  hmdType = HmdType_DK2;
114  }
115  }
116  }
117  }
118  else if (ScreenSizeInMeters.w > 0.1295f && ScreenSizeInMeters.w < 0.1297f)
119  {
120  // Screen size 0.1296 x 0.0729
121  hmdType = HmdType_DKHD2Proto;
122  }
123  }
124 
125  OVR_ASSERT( hmdType != HmdType_Unknown );
126  return hmdType;
127 }
128 
129 bool HMDDeviceCreateDesc::GetDeviceInfo(DeviceInfo* info) const
130 {
131  if ((info->InfoClassType != Device_HMD) &&
132  (info->InfoClassType != Device_None))
133  return false;
134 
135  HmdTypeEnum hmdType = GetHmdType();
136  char const* deviceName = "Oculus HMD";
137  switch (hmdType)
138  {
139  case HmdType_DKProto: deviceName = "Oculus Rift Prototype"; break;
140  case HmdType_DK1: deviceName = "Oculus Rift DK1"; break;
141  case HmdType_DKHDProto: deviceName = "Oculus Rift DKHD"; break;
142  case HmdType_DKHD2Proto: deviceName = "Oculus Rift DKHD2"; break;
143  case HmdType_DKHDProto566Mi: deviceName = "Oculus Rift DKHD 566 Mi"; break;
144  case HmdType_CrystalCoveProto: deviceName = "Oculus Rift Crystal Cove"; break;
145  case HmdType_DK2: deviceName = "Oculus Rift DK2"; break;
146  default: deviceName = "Oculus HMD"; break;
147  }
148 
149  info->ProductName = deviceName;
150  info->Manufacturer = "Oculus VR";
151  info->Type = Device_HMD;
152  info->Version = 0;
153 
154  // Display detection.
155  if (info->InfoClassType == Device_HMD)
156  {
157  HMDInfo* hmdInfo = static_cast<HMDInfo*>(info);
158 
159  hmdInfo->HmdType = hmdType;
160  hmdInfo->DesktopX = Desktop.X;
161  hmdInfo->DesktopY = Desktop.Y;
162  hmdInfo->ResolutionInPixels = ResolutionInPixels;
163  hmdInfo->ScreenSizeInMeters = ScreenSizeInMeters; // Includes ScreenGapSizeInMeters
164  hmdInfo->ScreenGapSizeInMeters = 0.0f;
165  hmdInfo->CenterFromTopInMeters = VCenterFromTopInMeters;
166  hmdInfo->LensSeparationInMeters = LensSeparationInMeters;
167  // TODO: any other information we get from the hardware itself should be added to this list
168 
169  switch ( hmdInfo->HmdType )
170  {
171  case HmdType_DKProto:
172  // WARNING - estimated.
174  hmdInfo->Shutter.VsyncToNextVsync = ( 1.0f / 60.0f );
175  hmdInfo->Shutter.VsyncToFirstScanline = 0.000052f;
176  hmdInfo->Shutter.FirstScanlineToLastScanline = 0.016580f;
177  hmdInfo->Shutter.PixelSettleTime = 0.015f; // estimated.
178  hmdInfo->Shutter.PixelPersistence = hmdInfo->Shutter.VsyncToNextVsync; // Full persistence
179  break;
180  case HmdType_DK1:
181  // Data from specs.
183  hmdInfo->Shutter.VsyncToNextVsync = ( 1.0f / 60.0f );
184  hmdInfo->Shutter.VsyncToFirstScanline = 0.00018226f;
185  hmdInfo->Shutter.FirstScanlineToLastScanline = 0.01620089f;
186  hmdInfo->Shutter.PixelSettleTime = 0.017f; // estimated.
187  hmdInfo->Shutter.PixelPersistence = hmdInfo->Shutter.VsyncToNextVsync; // Full persistence
188  break;
189  case HmdType_DKHDProto:
190  // Data from specs.
192  hmdInfo->Shutter.VsyncToNextVsync = ( 1.0f / 60.0f );
193  hmdInfo->Shutter.VsyncToFirstScanline = 0.0000859f;
194  hmdInfo->Shutter.FirstScanlineToLastScanline = 0.0164948f;
195  hmdInfo->Shutter.PixelSettleTime = 0.012f; // estimated.
196  hmdInfo->Shutter.PixelPersistence = hmdInfo->Shutter.VsyncToNextVsync; // Full persistence
197  break;
198  case HmdType_DKHD2Proto:
199  // Data from specs.
201  hmdInfo->Shutter.VsyncToNextVsync = ( 1.0f / 60.0f );
202  hmdInfo->Shutter.VsyncToFirstScanline = 0.000052f;
203  hmdInfo->Shutter.FirstScanlineToLastScanline = 0.016580f;
204  hmdInfo->Shutter.PixelSettleTime = 0.015f; // estimated.
205  hmdInfo->Shutter.PixelPersistence = hmdInfo->Shutter.VsyncToNextVsync; // Full persistence
206  break;
208 #if 0
209  // Low-persistence global shutter
210  hmdInfo->Shutter.Type = HmdShutter_Global;
211  hmdInfo->Shutter.VsyncToNextVsync = ( 1.0f / 76.0f );
212  hmdInfo->Shutter.VsyncToFirstScanline = 0.0000273f + 0.0131033f; // Global shutter - first visible scan line is actually the last!
213  hmdInfo->Shutter.FirstScanlineToLastScanline = 0.000f; // Global shutter - all visible at once.
214  hmdInfo->Shutter.PixelSettleTime = 0.0f; // <100us
215  hmdInfo->Shutter.PixelPersistence = 0.18f * hmdInfo->Shutter.VsyncToNextVsync; // Confgurable - currently set to 18% of total frame.
216 #else
217  // Low-persistence rolling shutter
219  hmdInfo->Shutter.VsyncToNextVsync = ( 1.0f / 76.0f );
220  hmdInfo->Shutter.VsyncToFirstScanline = 0.0000273f;
221  hmdInfo->Shutter.FirstScanlineToLastScanline = 0.0131033f;
222  hmdInfo->Shutter.PixelSettleTime = 0.0f; // <100us
223  hmdInfo->Shutter.PixelPersistence = 0.18f * hmdInfo->Shutter.VsyncToNextVsync; // Confgurable - currently set to 18% of total frame.
224 #endif
225  break;
227  // Low-persistence rolling shutter
229  hmdInfo->Shutter.VsyncToNextVsync = ( 1.0f / 76.0f );
230  hmdInfo->Shutter.VsyncToFirstScanline = 0.0000273f;
231  hmdInfo->Shutter.FirstScanlineToLastScanline = 0.0131033f;
232  hmdInfo->Shutter.PixelSettleTime = 0.0f; // <100us
233  hmdInfo->Shutter.PixelPersistence = 0.18f * hmdInfo->Shutter.VsyncToNextVsync; // Confgurable - currently set to 18% of total frame.
234  break;
235  case HmdType_DK2:
236  // Low-persistence rolling shutter
238  hmdInfo->Shutter.VsyncToNextVsync = ( 1.0f / 76.0f );
239  hmdInfo->Shutter.VsyncToFirstScanline = 0.0000273f;
240  hmdInfo->Shutter.FirstScanlineToLastScanline = 0.0131033f;
241  hmdInfo->Shutter.PixelSettleTime = 0.0f; // <100us
242  hmdInfo->Shutter.PixelPersistence = 0.18f * hmdInfo->Shutter.VsyncToNextVsync; // Confgurable - currently set to 18% of total frame.
243  break;
244  default: OVR_ASSERT ( false ); break;
245  }
246 
247 
248  OVR_strcpy(hmdInfo->DisplayDeviceName, sizeof(hmdInfo->DisplayDeviceName),
249  DisplayDeviceName.ToCStr());
250 #if defined(OVR_OS_WIN32)
251  // Nothing special for Win32.
252 #elif defined(OVR_OS_MAC)
253  hmdInfo->DisplayId = DisplayId;
254 #elif defined(OVR_OS_LINUX)
255  hmdInfo->DisplayId = DisplayId;
256 #elif defined(OVR_OS_ANDROID)
257  hmdInfo->DisplayId = DisplayId;
258 #else
259 #error Unknown platform
260 #endif
261 
262  }
263 
264  return true;
265 }
266 
267 
268 
269 
270 
271 //-------------------------------------------------------------------------------------
272 // ***** HMDDevice
273 
274 HMDDevice::HMDDevice(HMDDeviceCreateDesc* createDesc)
275  : OVR::DeviceImpl<OVR::HMDDevice>(createDesc, 0)
276 {
277 }
279 {
280 }
281 
283 {
284  pParent = parent;
285  return true;
286 }
288 {
289  ProfileName.Clear();
290  pCachedProfile.Clear();
291  pParent.Clear();
292 }
293 
295 {
296  // Loads and returns a cached profile based on this device and current user
297  if (pCachedProfile == NULL)
298  {
300  const char* profile_name = GetProfileName();
301  if (profile_name && profile_name[0])
302  pCachedProfile = *mgr->GetProfile(this, profile_name);
303 
304  if (pCachedProfile == NULL)
305  pCachedProfile = *mgr->GetDefaultProfile(this);
306 
307  }
308  return pCachedProfile.GetPtr();
309 }
310 
312 {
313  if (ProfileName.IsEmpty())
314  { // If the profile name has not been initialized then
315  // retrieve the stored default user for this specific device
317  const char* name = mgr->GetDefaultUser(this);
318  ProfileName = name;
319  }
320 
321  return ProfileName.ToCStr();
322 }
323 
324 bool HMDDevice::SetProfileName(const char* name)
325 {
326  if (ProfileName == name)
327  return true; // already set
328 
329  // Flush the old profile
330  pCachedProfile.Clear();
331  if (!name)
332  {
333  ProfileName.Clear();
334  return false;
335  }
336 
337  // Set the name and attempt to cache the profile
338  ProfileName = name;
339  if (GetProfile())
340  {
341  return true;
342  }
343  else
344  {
345  ProfileName.Clear();
346  return false;
347  }
348 }
349 
351 {
352  // Just return first sensor found since we have no way to match it yet.
353 
354  // Create DK2 sensor if it exists otherwise create first DK1 sensor.
355  SensorDevice* sensor = NULL;
356 
358 
359  while(enumerator.GetType() != Device_None)
360  {
361  SensorInfo info;
362  enumerator.GetDeviceInfo(&info);
363 
365  {
366  sensor = enumerator.CreateDevice();
367  break;
368  }
369 
370  enumerator.Next();
371  }
372 
373  if (sensor == NULL)
374  {
375  sensor = GetManager()->EnumerateDevices<SensorDevice>().CreateDevice();
376  }
377 
378  if (sensor)
379  {
381  }
382 
383  return sensor;
384 }
DeviceType Type
Definition: OVR_Device.h:152
float CenterFromTopInMeters
Definition: OVR_Device.h:327
const DeviceType InfoClassType
Definition: OVR_Device.h:149
Size< float > ScreenSizeInMeters
Definition: OVR_Device.h:325
virtual DeviceManager * GetManager() const
#define NULL
bool GetDeviceInfo(DeviceInfo *info) const
unsigned Version
Definition: OVR_Device.h:156
DeviceType GetType() const
Ptr< DeviceBase > pParent
virtual bool SetProfileName(const char *name)
HmdShutterTypeEnum Type
Definition: OVR_Device.h:333
#define OVR_UNUSED(a)
void Clear()
Definition: OVR_String.cpp:386
char *OVR_CDECL OVR_strcpy(char *dest, UPInt destsize, const char *src)
Definition: OVR_Std.h:148
virtual ProfileManager * GetProfileManager() const =0
const char * GetDefaultUser(const DeviceBase *device)
float ScreenGapSizeInMeters
Definition: OVR_Device.h:326
Profile * GetProfile(const DeviceBase *device, const char *user)
char DisplayDeviceName[32]
Definition: OVR_Device.h:347
const char * ToCStr() const
Definition: OVR_String.h:186
float LensSeparationInMeters
Definition: OVR_Device.h:328
String ProductName
Definition: OVR_Device.h:154
DeviceEnumerator< D > EnumerateDevices(bool availableOnly=true)
Definition: OVR_Device.h:265
#define OVR_ASSERT(p)
virtual const char * GetProfileName()
Profile * GetDefaultProfile(const DeviceBase *device)
bool IsEmpty() const
Definition: OVR_String.h:191
Size< float > Sizef
Definition: OVR_Math.h:638
HmdTypeEnum HmdType
Definition: OVR_Device.h:323
SByte DecodeBCD(UByte byte)
Definition: OVR_Alg.h:1049
virtual OVR::SensorDevice * GetSensor()
virtual bool Initialize(DeviceBase *parent)
struct OVR::HMDInfo::ShutterInfo Shutter
String Manufacturer
Definition: OVR_Device.h:155
Size< int > ResolutionInPixels
Definition: OVR_Device.h:324
virtual void SetCoordinateFrame(CoordinateFrame coordframe)=0
virtual Profile * GetProfile()
Size< int > Sizei
Definition: OVR_Math.h:636
UInt16 ProductId
Definition: OVR_Device.h:485