oculus1

view libovr/Src/OVR_DeviceImpl.h @ 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 source
1 /************************************************************************************
3 Filename : OVR_DeviceImpl.h
4 Content : Partial back-end independent implementation of Device interfaces
5 Created : October 10, 2012
6 Authors : Michael Antonov
8 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
10 Use of this software is subject to the terms of the Oculus license
11 agreement provided at the time of installation or download, or which
12 otherwise accompanies this software in either electronic or hard copy form.
14 *************************************************************************************/
16 #ifndef OVR_DeviceImpl_h
17 #define OVR_DeviceImpl_h
19 #include "OVR_Device.h"
20 #include "Kernel/OVR_Atomic.h"
21 #include "Kernel/OVR_Log.h"
22 #include "Kernel/OVR_System.h"
24 #include "Kernel/OVR_Threads.h"
25 #include "OVR_ThreadCommandQueue.h"
26 #include "OVR_HIDDevice.h"
28 namespace OVR {
30 class DeviceManagerImpl;
31 class DeviceFactory;
33 enum
34 {
35 Oculus_VendorId = 0x2833
36 };
38 //-------------------------------------------------------------------------------------
39 // Globally shared Lock implementation used for MessageHandlers.
41 class SharedLock
42 {
43 public:
44 SharedLock() : UseCount(0) {}
46 Lock* GetLockAddRef();
47 void ReleaseLock(Lock* plock);
49 private:
50 Lock* toLock() { return (Lock*)Buffer; }
52 // UseCount and max alignment.
53 volatile int UseCount;
54 UInt64 Buffer[(sizeof(Lock)+sizeof(UInt64)-1)/sizeof(UInt64)];
55 };
58 // Wrapper for MessageHandler that includes synchronization logic.
59 // References to MessageHandlers are organized in a list to allow for them to
60 // easily removed with MessageHandler::RemoveAllHandlers.
61 class MessageHandlerRef : public ListNode<MessageHandlerRef>
62 {
63 public:
64 MessageHandlerRef(DeviceBase* device);
65 ~MessageHandlerRef();
67 void SetHandler(MessageHandler* hander);
69 // Not-thread-safe version
70 void SetHandler_NTS(MessageHandler* hander);
72 void Call(const Message& msg)
73 {
74 Lock::Locker lockScope(pLock);
75 if (pHandler)
76 pHandler->OnMessage(msg);
77 }
79 Lock* GetLock() const { return pLock; }
81 // GetHandler() is not thread safe if used out of order across threads; nothing can be done
82 // about that.
83 MessageHandler* GetHandler() const { return pHandler; }
84 DeviceBase* GetDevice() const { return pDevice; }
86 private:
87 Lock* pLock; // Cached global handler lock.
88 DeviceBase* pDevice;
89 MessageHandler* pHandler;
90 };
94 //-------------------------------------------------------------------------------------
96 // DeviceManagerLock is a synchronization lock used by DeviceManager for Devices
97 // and is allocated separately for potentially longer lifetime.
98 //
99 // DeviceManagerLock is used for all of the following:
100 // - Adding/removing devices
101 // - Reporting manager lifetime (pManager != 0) for DeviceHandles
102 // - Protecting device creation/shutdown.
104 class DeviceManagerLock : public RefCountBase<DeviceManagerLock>
105 {
106 public:
107 Lock CreateLock;
108 DeviceManagerImpl* pManager;
110 DeviceManagerLock() : pManager(0) { }
111 };
114 // DeviceCreateDesc provides all of the information needed to create any device, a derived
115 // instance of this class is created by DeviceFactory during enumeration.
116 // - DeviceCreateDesc may or may not be a part of DeviceManager::Devices list (check pNext != 0).
117 // - Referenced and kept alive by DeviceHandle.
119 class DeviceCreateDesc : public ListNode<DeviceCreateDesc>, public NewOverrideBase
120 {
121 void operator = (const DeviceCreateDesc&) { } // Assign not supported; suppress MSVC warning.
122 public:
123 DeviceCreateDesc(DeviceFactory* factory, DeviceType type)
124 : pFactory(factory), Type(type), pLock(0), HandleCount(0), pDevice(0), Enumerated(true)
125 {
126 pNext = pPrev = 0;
127 }
129 virtual ~DeviceCreateDesc()
130 {
131 OVR_ASSERT(!pDevice);
132 if (pNext)
133 RemoveNode();
134 }
136 DeviceManagerImpl* GetManagerImpl() const { return pLock->pManager; }
137 Lock* GetLock() const { return &pLock->CreateLock; }
139 // DeviceCreateDesc reference counting is tied to Devices list management,
140 // see comments for HandleCount.
141 void AddRef();
142 void Release();
145 // *** Device creation/matching Interface
148 // Cloning copies us to an allocated object when new device is enumerated.
149 virtual DeviceCreateDesc* Clone() const = 0;
150 // Creates a new device instance without Initializing it; the
151 // later is done my Initialize()/Shutdown() methods of the device itself.
152 virtual DeviceBase* NewDeviceInstance() = 0;
153 // Override to return device-specific info.
154 virtual bool GetDeviceInfo(DeviceInfo* info) const = 0;
157 enum MatchResult
158 {
159 Match_None,
160 Match_Found,
161 Match_Candidate
162 };
164 // Override to return Match_Found if descriptor matches our device.
165 // Match_Candidate can be returned, with pcandicate update, if this may be a match
166 // but more searching is necessary. If this is the case UpdateMatchedCandidate will be called.
167 virtual MatchResult MatchDevice(const DeviceCreateDesc& other,
168 DeviceCreateDesc** pcandidate) const = 0;
170 // Called for matched candidate after all potential matches are iterated.
171 // Used to update HMDevice creation arguments from Sensor.
172 // Optional return param 'newDeviceFlag' will be set to true if the
173 // 'desc' refers to a new device; false, otherwise.
174 // Return 'false' to create new object, 'true' if done with this argument.
175 virtual bool UpdateMatchedCandidate(
176 const DeviceCreateDesc& desc, bool* newDeviceFlag = NULL)
177 { OVR_UNUSED2(desc, newDeviceFlag); return false; }
179 // Matches HID device to the descriptor.
180 virtual bool MatchHIDDevice(const HIDDeviceDesc&) const { return false; }
182 // Matches device by path.
183 virtual bool MatchDevice(const String& /*path*/) { return false; }
184 //protected:
185 DeviceFactory* const pFactory;
186 const DeviceType Type;
188 // List in which this descriptor lives. pList->CreateLock required if added/removed.
189 Ptr<DeviceManagerLock> pLock;
191 // Strong references to us: Incremented by Device, DeviceHandles & Enumerators.
192 // May be 0 if device not created and there are no handles.
193 // Following transitions require pList->CreateLock:
194 // {1 -> 0}: May delete & remove handle if no longer available.
195 // {0 -> 1}: Device creation is only possible if manager is still alive.
196 AtomicInt<UInt32> HandleCount;
197 // If not null, points to our created device instance. Modified during lock only.
198 DeviceBase* pDevice;
199 // True if device is marked as available during enumeration.
200 bool Enumerated;
201 };
205 // Common data present in the implementation of every DeviceBase.
206 // Injected by DeviceImpl.
207 class DeviceCommon
208 {
209 public:
210 AtomicInt<UInt32> RefCount;
211 Ptr<DeviceCreateDesc> pCreateDesc;
212 Ptr<DeviceBase> pParent;
213 MessageHandlerRef HandlerRef;
215 DeviceCommon(DeviceCreateDesc* createDesc, DeviceBase* device, DeviceBase* parent)
216 : RefCount(1), pCreateDesc(createDesc), pParent(parent), HandlerRef(device)
217 {
218 }
220 // Device reference counting delegates to Manager thread to actually kill devices.
221 void DeviceAddRef();
222 void DeviceRelease();
224 Lock* GetLock() const { return pCreateDesc->GetLock(); }
226 virtual bool Initialize(DeviceBase* parent) = 0;
227 virtual void Shutdown() = 0;
228 };
231 //-------------------------------------------------------------------------------------
232 // DeviceImpl address DeviceRecord implementation to a device base class B.
233 // B must be derived form DeviceBase.
235 template<class B>
236 class DeviceImpl : public B, public DeviceCommon
237 {
238 public:
239 DeviceImpl(DeviceCreateDesc* createDesc, DeviceBase* parent)
240 : DeviceCommon(createDesc, getThis(), parent)
241 {
242 }
244 // Convenience method to avoid manager access typecasts.
245 DeviceManagerImpl* GetManagerImpl() const { return pCreateDesc->pLock->pManager; }
247 // Inline to avoid warnings.
248 DeviceImpl* getThis() { return this; }
250 // Common implementation delegate to avoid virtual inheritance and dynamic casts.
251 virtual DeviceCommon* getDeviceCommon() const { return (DeviceCommon*)this; }
253 /*
254 virtual void AddRef() { pCreateDesc->DeviceAddRef(); }
255 virtual void Release() { pCreateDesc->DeviceRelease(); }
256 virtual DeviceBase* GetParent() const { return pParent.GetPtr(); }
257 virtual DeviceManager* GetManager() const { return pCreateDesc->pLock->pManager;}
258 virtual void SetMessageHandler(MessageHandler* handler) { HanderRef.SetHandler(handler); }
259 virtual MessageHandler* GetMessageHandler() const { return HanderRef.GetHandler(); }
260 virtual DeviceType GetType() const { return pCreateDesc->Type; }
261 virtual DeviceType GetType() const { return pCreateDesc->Type; }
262 */
263 };
266 //-------------------------------------------------------------------------------------
267 // ***** DeviceFactory
269 // DeviceFactory is maintained in DeviceManager for each separately-enumerable
270 // device type; factories allow separation of unrelated enumeration code.
272 class DeviceFactory : public ListNode<DeviceFactory>, public NewOverrideBase
273 {
274 public:
276 DeviceFactory() : pManager(0)
277 {
278 pNext = pPrev = 0;
279 }
280 virtual ~DeviceFactory() { }
282 DeviceManagerImpl* GetManagerImpl() { return pManager; }
284 // Notifiers called when we are added to/removed from a device.
285 virtual bool AddedToManager(DeviceManagerImpl* manager)
286 {
287 OVR_ASSERT(pManager == 0);
288 pManager = manager;
289 return true;
290 }
292 virtual void RemovedFromManager()
293 {
294 pManager = 0;
295 }
298 // *** Device Enumeration/Creation Support
300 // Passed to EnumerateDevices to be informed of every device detected.
301 class EnumerateVisitor
302 {
303 public:
304 virtual void Visit(const DeviceCreateDesc& createDesc) = 0;
305 };
307 // Enumerates factory devices by notifying EnumerateVisitor about every
308 // device that is present.
309 virtual void EnumerateDevices(EnumerateVisitor& visitor) = 0;
311 // Matches vendorId/productId pair with the factory; returns 'true'
312 // if the factory can handle the device.
313 virtual bool MatchVendorProduct(UInt16 vendorId, UInt16 productId) const
314 {
315 OVR_UNUSED2(vendorId, productId);
316 return false;
317 }
319 // Detects the HID device and adds the DeviceCreateDesc into Devices list, if
320 // the device belongs to this factory. Returns 'false', if not.
321 virtual bool DetectHIDDevice(DeviceManager* pdevMgr, const HIDDeviceDesc& desc)
322 {
323 OVR_UNUSED2(pdevMgr, desc);
324 return false;
325 }
327 protected:
328 DeviceManagerImpl* pManager;
329 };
332 //-------------------------------------------------------------------------------------
333 // ***** DeviceManagerImpl
335 // DeviceManagerImpl is a partial default DeviceManager implementation that
336 // maintains a list of devices and supports their enumeration.
338 class DeviceManagerImpl : public DeviceImpl<OVR::DeviceManager>, public ThreadCommandQueue
339 {
340 public:
341 DeviceManagerImpl();
342 ~DeviceManagerImpl();
344 // Constructor helper function to create Descriptor and manager lock during initialization.
345 static DeviceCreateDesc* CreateManagerDesc();
347 // DeviceManagerImpl provides partial implementation of Initialize/Shutdown that must
348 // be called by the platform-specific derived class.
349 virtual bool Initialize(DeviceBase* parent);
350 virtual void Shutdown();
353 // Every DeviceManager has an associated profile manager, which is used to store
354 // user settings that may affect device behavior.
355 virtual ProfileManager* GetProfileManager() const { return pProfileManager.GetPtr(); }
357 // Override to return ThreadCommandQueue implementation used to post commands
358 // to the background device manager thread (that must be created by Initialize).
359 virtual ThreadCommandQueue* GetThreadQueue() = 0;
361 // Returns the thread id of the DeviceManager.
362 virtual ThreadId GetThreadId() const = 0;
364 virtual DeviceEnumerator<> EnumerateDevicesEx(const DeviceEnumerationArgs& args);
367 //
368 void AddFactory(DeviceFactory* factory)
369 {
370 // This lock is only needed if we call AddFactory after manager thread creation.
371 Lock::Locker scopeLock(GetLock());
372 Factories.PushBack(factory);
373 factory->AddedToManager(this);
374 }
376 void CallOnDeviceAdded(DeviceCreateDesc* desc)
377 {
378 HandlerRef.Call(MessageDeviceStatus(Message_DeviceAdded, this, DeviceHandle(desc)));
379 }
380 void CallOnDeviceRemoved(DeviceCreateDesc* desc)
381 {
382 HandlerRef.Call(MessageDeviceStatus(Message_DeviceRemoved, this, DeviceHandle(desc)));
383 }
385 // Helper to access Common data for a device.
386 static DeviceCommon* GetDeviceCommon(DeviceBase* device)
387 {
388 return device->getDeviceCommon();
389 }
392 // Background-thread callbacks for DeviceCreation/Release. These
393 DeviceBase* CreateDevice_MgrThread(DeviceCreateDesc* createDesc, DeviceBase* parent = 0);
394 Void ReleaseDevice_MgrThread(DeviceBase* device);
397 // Calls EnumerateDevices() on all factories
398 virtual Void EnumerateAllFactoryDevices();
399 // Enumerates devices for a particular factory.
400 virtual Void EnumerateFactoryDevices(DeviceFactory* factory);
402 virtual HIDDeviceManager* GetHIDDeviceManager() const
403 {
404 return HidDeviceManager;
405 }
407 // Adds device (DeviceCreateDesc*) into Devices. Returns NULL,
408 // if unsuccessful or device is already in the list.
409 virtual Ptr<DeviceCreateDesc> AddDevice_NeedsLock(const DeviceCreateDesc& createDesc);
411 // Finds a device descriptor by path and optional type.
412 Ptr<DeviceCreateDesc> FindDevice(const String& path, DeviceType = Device_None);
414 // Finds HID device by HIDDeviceDesc.
415 Ptr<DeviceCreateDesc> FindHIDDevice(const HIDDeviceDesc&);
416 void DetectHIDDevice(const HIDDeviceDesc&);
418 // Manager Lock-protected list of devices.
419 List<DeviceCreateDesc> Devices;
421 // Factories used to detect and manage devices.
422 List<DeviceFactory> Factories;
424 protected:
425 Ptr<HIDDeviceManager> HidDeviceManager;
426 Ptr<ProfileManager> pProfileManager;
427 };
430 } // namespace OVR
432 #endif