Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CAPI_HMDState.h
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : CAPI_HMDState.h
4 Content : State associated with a single HMD
5 Created : January 24, 2014
6 Authors : Michael Antonov
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 #ifndef OVR_CAPI_HMDState_h
28 #define OVR_CAPI_HMDState_h
29 
30 #include "../Kernel/OVR_Math.h"
31 #include "../Kernel/OVR_List.h"
32 #include "../Kernel/OVR_Log.h"
33 #include "../OVR_CAPI.h"
34 #include "../OVR_SensorFusion.h"
35 #include "../Util/Util_LatencyTest.h"
36 #include "../Util/Util_LatencyTest2.h"
37 
38 #include "CAPI_FrameTimeManager.h"
39 #include "CAPI_HMDRenderState.h"
41 
42 // Define OVR_CAPI_VISIONSUPPORT to compile in vision support
43 #ifdef OVR_CAPI_VISIONSUPPORT
44  #define OVR_CAPI_VISION_CODE(c) c
45  #include "../Vision/Vision_PoseTracker.h"
46 #else
47  #define OVR_CAPI_VISION_CODE(c)
48 #endif
49 
50 
51 struct ovrHmdStruct { };
52 
53 namespace OVR { namespace CAPI {
54 
55 using namespace OVR::Util::Render;
56 
57 
58 //-------------------------------------------------------------------------------------
59 // ***** ThreadChecker
60 
61 // This helper class is used to verify that the API is used according to supported
62 // thread safety constraints (is not re-entrant for this and related functions).
64 {
65 public:
66 
67 #ifndef OVR_BUILD_DEBUG
68 
69  // In release build, thread checks are disabled.
71  void Begin(const char* functionName) { OVR_UNUSED1(functionName); }
72  void End() { }
73 
74  // Add thread-re-entrancy check for function scope
75  struct Scope
76  {
77  Scope(ThreadChecker*, const char *) { }
78  ~Scope() { }
79  };
80 
81 
82 #else // OVR_BUILD_DEBUG
83  ThreadChecker() : pFunctionName(0), FirstThread(0)
84  { }
85 
86  void Begin(const char* functionName)
87  {
88  if (!pFunctionName)
89  {
90  pFunctionName = functionName;
91  FirstThread = GetCurrentThreadId();
92  }
93  else
94  {
95  // pFunctionName may be not null here if function is called internally on the same thread.
96  OVR_ASSERT_LOG((FirstThread == GetCurrentThreadId()),
97  ("%s (threadId=%p) called at the same times as %s (threadId=%p)\n",
98  functionName, GetCurrentThreadId(), pFunctionName, FirstThread) );
99  }
100  }
101  void End()
102  {
103  pFunctionName = 0;
104  FirstThread = 0;
105  }
106 
107  // Add thread-re-entrancy check for function scope.
108  struct Scope
109  {
110  Scope(ThreadChecker* threadChecker, const char *functionName) : pChecker(threadChecker)
111  { pChecker->Begin(functionName); }
112  ~Scope()
113  { pChecker->End(); }
114  private:
115  ThreadChecker* pChecker;
116  };
117 
118 private:
119  // If not 0, contains the name of the function that first entered the scope.
120  const char * pFunctionName;
121  ThreadId FirstThread;
122 
123 #endif // OVR_BUILD_DEBUG
124 };
125 
126 
127 //-------------------------------------------------------------------------------------
128 // ***** HMDState
129 
130 // Describes a single HMD.
131 class HMDState : public ListNode<HMDState>,
132  public ovrHmdStruct, public NewOverrideBase
133 {
134 public:
135 
136  HMDState(HMDDevice* device);
137  HMDState(ovrHmdType hmdType);
138  virtual ~HMDState();
139 
140 
141  // *** Sensor Setup
142 
143  bool StartSensor(unsigned supportedCaps, unsigned requiredCaps);
144  void StopSensor();
145  void ResetSensor();
146  ovrSensorState PredictedSensorState(double absTime);
147  bool GetSensorDesc(ovrSensorDesc* descOut);
148 
149  // Changes HMD Caps.
150  // Capability bits that are not directly or logically tied to one system (such as sensor)
151  // are grouped here. ovrHmdCap_VSync, for example, affects rendering and timing.
152  void SetEnabledHmdCaps(unsigned caps);
153 
154 
155  bool ProcessLatencyTest(unsigned char rgbColorOut[3]);
156  void ProcessLatencyTest2(unsigned char rgbColorOut[3], double startTime);
157 
158 
159  // *** Rendering Setup
160 
161  bool ConfigureRendering(ovrEyeRenderDesc eyeRenderDescOut[2],
162  const ovrFovPort eyeFovIn[2],
163  const ovrRenderAPIConfig* apiConfig,
164  unsigned distortionCaps);
165 
166  ovrPosef BeginEyeRender(ovrEyeType eye);
167  void EndEyeRender(ovrEyeType eye, ovrPosef renderPose, ovrTexture* eyeTexture);
168 
169 
170  const char* GetLastError()
171  {
172  const char* p = pLastError;
173  pLastError = 0;
174  return p;
175  }
176 
178  {
179  if (deviceType == Device_Sensor)
180  AddSensorCount++;
181  else if (deviceType == Device_LatencyTester)
182  {
183  AddLatencyTestCount++;
184  AddLatencyTestDisplayCount++;
185  }
186  }
187 
188  bool checkCreateSensor();
189 
190  void applyProfileToSensorFusion();
191 
192  // INlines so that they can be easily compiled out.
193  // Does debug ASSERT checks for functions that require BeginFrame.
194  // Also verifies that we are on the right thread.
195  void checkBeginFrameScope(const char* functionName)
196  {
197  OVR_UNUSED1(functionName); // for Release build.
198  OVR_ASSERT_LOG(BeginFrameCalled == true,
199  ("%s called outside ovrHmd_BeginFrame.", functionName));
200  OVR_ASSERT_LOG(BeginFrameThreadId == OVR::GetCurrentThreadId(),
201  ("%s called on a different thread then ovrHmd_BeginFrame.", functionName));
202  }
203 
204  void checkRenderingConfigured(const char* functionName)
205  {
206  OVR_UNUSED1(functionName); // for Release build.
207  OVR_ASSERT_LOG(RenderingConfigured == true,
208  ("%s called without ovrHmd_ConfigureRendering.", functionName));
209  }
210 
211  void checkBeginFrameTimingScope(const char* functionName)
212  {
213  OVR_UNUSED1(functionName); // for Release build.
214  OVR_ASSERT_LOG(BeginFrameTimingCalled == true,
215  ("%s called outside ovrHmd_BeginFrameTiming.", functionName));
216  }
217 
218 
219  HMDState* getThis() { return this; }
220 
221  void updateLowPersistenceMode(bool lowPersistence) const;
222  void updateLatencyTestForHmd(bool latencyTesting);
223 
224  void updateDK2FeaturesTiedToSensor(bool sensorCreatedJustNow);
225 
226  // Get properties by name.
227  float getFloatValue(const char* propertyName, float defaultVal);
228  bool setFloatValue(const char* propertyName, float value);
229  unsigned getFloatArray(const char* propertyName, float values[], unsigned arraySize);
230  bool setFloatArray(const char* propertyName, float values[], unsigned arraySize);
231  const char* getString(const char* propertyName, const char* defaultVal);
232 public:
233 
234  // Wrapper to support 'const'
236  {
238  {
240  if (hmdType == ovrHmd_DK1)
241  t = HmdType_DK1;
242  else if (hmdType == ovrHmd_CrystalCoveProto)
244  else if (hmdType == ovrHmd_DK2)
245  t = HmdType_DK2;
246  h = CreateDebugHMDInfo(t);
247  }
248  HMDInfoWrapper(HMDDevice* device) { if (device) device->GetDeviceInfo(&h); }
250  };
251 
252  // Note: pHMD can be null if we are representing a virtualized debug HMD.
254 
255  // HMDInfo shouldn't change, as its string pointers are passed out.
258 
259  const char* pLastError;
260 
261  // Caps enabled for the HMD.
262  unsigned EnabledHmdCaps;
263  // These are the flags actually applied to the Sensor device,
264  // used to track whether SetDisplayReport calls are necessary.
266 
267 
268  // *** Sensor
269 
270  // Lock used to support thread-safe lifetime access to sensor.
272 
273  // Atomic integer used as a flag that we should check the sensor device.
275 
276  // All of Sensor variables may be modified/used with DevicesLock, with exception that
277  // the {SensorStarted, SensorCreated} can be read outside the lock to see
278  // if device creation check is necessary.
279  // Whether we called StartSensor() and requested sensor caps.
280  volatile bool SensorStarted;
281  volatile bool SensorCreated;
282  // pSensor may still be null or non-running after start if it wasn't yet available
284  unsigned SensorCaps;
285 
286  // SensorFusion state may be accessible without a lock.
288 
289 
290  // Vision pose tracker is currently new-allocated
292  Vision::PoseTracker* pPoseTracker;
293  )
294 
295  // Latency tester
296  Ptr<LatencyTestDevice> pLatencyTester;
299 
301  unsigned char LatencyTestDrawColor[3];
302 
303  // Using latency tester as debug display
307 
309  unsigned char LatencyTest2DrawColor[3];
310  //bool ReadbackColor;
311 
312  // Rendering part
316 
317  // Last timing value reported by BeginFrame.
319  // Last timing value reported by GetFrameTime. These are separate since the intended
320  // use is from different threads. TBD: Move to FrameTimeManager? Make atomic?
322 
323  // Last cached value returned by ovrHmd_GetString/ovrHmd_GetStringArray.
324  char LastGetStringValue[256];
325 
326 
327  // Debug flag set after ovrHmd_ConfigureRendering succeeds.
329  // Set after BeginFrame succeeds, and its corresponding thread id for debug checks.
332  // Graphics functions are not re-entrant from other threads.
334  //
336 
337  // Flags set when we've called BeginEyeRender on a given eye.
338  bool EyeRenderActive[2];
339 };
340 
341 
342 }} // namespace OVR::CAPI
343 
344 
345 #endif // OVR_CAPI_HMDState_h
346 
347 
virtual bool GetDeviceInfo(DeviceInfo *info) const
HMDState * getThis()
volatile bool SensorStarted
ovrHmdType
Definition: OVR_CAPI.h:113
Ptr< HMDDevice > pHMD
#define OVR_ASSERT_LOG(c, args)
Definition: OVR_Log.h:198
SensorFusion SFusion
ThreadChecker RenderAPIThreadChecker
AtomicInt< int > AddLatencyTestCount
const OVR::HMDInfo & HMDInfo
HMDRenderState RenderState
void checkRenderingConfigured(const char *functionName)
#define OVR_UNUSED1(a1)
ovrEyeType
Definition: OVR_CAPI.h:177
enum _deviceType deviceType
void checkBeginFrameTimingScope(const char *functionName)
ThreadId BeginFrameThreadId
Scope(ThreadChecker *, const char *)
Definition: CAPI_HMDState.h:77
const HMDInfoWrapper HMDInfoW
#define OVR_CAPI_VISION_CODE(c)
Definition: CAPI_HMDState.h:47
double LastGetFrameTimeSeconds
void Begin(const char *functionName)
Definition: CAPI_HMDState.h:71
AtomicInt< int > AddLatencyTestDisplayCount
Ptr< LatencyTestDevice > pLatencyTesterDisplay
HMDInfo CreateDebugHMDInfo(HmdTypeEnum hmdType)
Definition: OVR_Stereo.cpp:588
unsigned HmdCapsAppliedToSensor
Ptr< SensorDevice > pSensor
Ptr< DistortionRenderer > pRenderer
void checkBeginFrameScope(const char *functionName)
Util::LatencyTest LatencyUtil
void * ThreadId
Definition: OVR_Threads.h:180
Util::LatencyTest2 LatencyUtil2
AtomicInt< int > AddSensorCount
FrameTimeManager TimeManager
volatile bool SensorCreated
void NotifyAddDevice(DeviceType deviceType)
ThreadId GetCurrentThreadId()
const char * GetLastError()
const char * pLastError