oculus1

view libovr/Src/OVR_DeviceHandle.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 source
1 /************************************************************************************
3 Filename : OVR_DeviceHandle.cpp
4 Content : Implementation of device handle class
5 Created : February 5, 2013
6 Authors : Lee Cooper
8 Copyright : Copyright 2013 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 #include "OVR_DeviceHandle.h"
18 #include "OVR_DeviceImpl.h"
20 namespace OVR {
22 //-------------------------------------------------------------------------------------
23 // ***** DeviceHandle
25 DeviceHandle::DeviceHandle(DeviceCreateDesc* impl) : pImpl(impl)
26 {
27 if (pImpl)
28 pImpl->AddRef();
29 }
31 DeviceHandle::DeviceHandle(const DeviceHandle& src) : pImpl(src.pImpl)
32 {
33 if (pImpl)
34 pImpl->AddRef();
35 }
37 DeviceHandle::~DeviceHandle()
38 {
39 if (pImpl)
40 pImpl->Release();
41 }
43 void DeviceHandle::operator = (const DeviceHandle& src)
44 {
45 if (src.pImpl)
46 src.pImpl->AddRef();
47 if (pImpl)
48 pImpl->Release();
49 pImpl = src.pImpl;
50 }
52 DeviceBase* DeviceHandle::GetDevice_AddRef() const
53 {
54 if (pImpl && pImpl->pDevice)
55 {
56 pImpl->pDevice->AddRef();
57 return pImpl->pDevice;
58 }
59 return NULL;
60 }
62 // Returns true, if the handle contains the same device ptr
63 // as specified in the parameter.
64 bool DeviceHandle::IsDevice(DeviceBase* pdev) const
65 {
66 return (pdev && pImpl && pImpl->pDevice) ?
67 pImpl->pDevice == pdev : false;
68 }
70 DeviceType DeviceHandle::GetType() const
71 {
72 return pImpl ? pImpl->Type : Device_None;
73 }
75 bool DeviceHandle::GetDeviceInfo(DeviceInfo* info) const
76 {
77 return pImpl ? pImpl->GetDeviceInfo(info) : false;
78 }
79 bool DeviceHandle::IsAvailable() const
80 {
81 // This isn't "atomically safe", but the function only returns the
82 // recent state that may change.
83 return pImpl ? (pImpl->Enumerated && pImpl->pLock->pManager) : false;
84 }
86 bool DeviceHandle::IsCreated() const
87 {
88 return pImpl ? (pImpl->pDevice != 0) : false;
89 }
91 DeviceBase* DeviceHandle::CreateDevice()
92 {
93 if (!pImpl)
94 return 0;
96 DeviceBase* device = 0;
97 Ptr<DeviceManagerImpl> manager= 0;
99 // Since both manager and device pointers can only be destroyed during a lock,
100 // hold it while checking for availability.
101 // AddRef to manager so that it doesn't get released on us.
102 {
103 Lock::Locker deviceLockScope(pImpl->GetLock());
105 if (pImpl->pDevice)
106 {
107 pImpl->pDevice->AddRef();
108 return pImpl->pDevice;
109 }
110 manager = pImpl->GetManagerImpl();
111 }
113 if (manager)
114 {
115 if (manager->GetThreadId() != OVR::GetCurrentThreadId())
116 {
117 // Queue up a CreateDevice request. This fills in '&device' with AddRefed value,
118 // or keep it at null.
119 manager->GetThreadQueue()->PushCallAndWaitResult(
120 manager.GetPtr(), &DeviceManagerImpl::CreateDevice_MgrThread,
121 &device, pImpl, (DeviceBase*)0);
122 }
123 else
124 device = manager->CreateDevice_MgrThread(pImpl, (DeviceBase*)0);
125 }
126 return device;
127 }
129 void DeviceHandle::Clear()
130 {
131 if (pImpl)
132 {
133 pImpl->Release();
134 pImpl = 0;
135 }
136 }
138 bool DeviceHandle::enumerateNext(const DeviceEnumerationArgs& args)
139 {
140 if (GetType() == Device_None)
141 return false;
143 Ptr<DeviceManagerImpl> managerKeepAlive;
144 Lock::Locker lockScope(pImpl->GetLock());
146 DeviceCreateDesc* next = pImpl;
147 // If manager was destroyed, we get removed from the list.
148 if (!pImpl->pNext)
149 return false;
151 managerKeepAlive = next->GetManagerImpl();
152 OVR_ASSERT(managerKeepAlive);
154 do {
155 next = next->pNext;
157 if (managerKeepAlive->Devices.IsNull(next))
158 {
159 pImpl->Release();
160 pImpl = 0;
161 return false;
162 }
164 } while(!args.MatchRule(next->Type, next->Enumerated));
166 next->AddRef();
167 pImpl->Release();
168 pImpl = next;
170 return true;
171 }
173 } // namespace OVR