oculus1

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/OVR_DeviceHandle.cpp	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,174 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +Filename    :   OVR_DeviceHandle.cpp
     1.7 +Content     :   Implementation of device handle class
     1.8 +Created     :   February 5, 2013
     1.9 +Authors     :   Lee Cooper
    1.10 +
    1.11 +Copyright   :   Copyright 2013 Oculus VR, Inc. All Rights reserved.
    1.12 +
    1.13 +Use of this software is subject to the terms of the Oculus license
    1.14 +agreement provided at the time of installation or download, or which
    1.15 +otherwise accompanies this software in either electronic or hard copy form.
    1.16 +
    1.17 +*************************************************************************************/
    1.18 +
    1.19 +#include "OVR_DeviceHandle.h"
    1.20 +
    1.21 +#include "OVR_DeviceImpl.h"
    1.22 +
    1.23 +namespace OVR {
    1.24 +
    1.25 +//-------------------------------------------------------------------------------------
    1.26 +// ***** DeviceHandle
    1.27 +
    1.28 +DeviceHandle::DeviceHandle(DeviceCreateDesc* impl) : pImpl(impl)
    1.29 +{
    1.30 +    if (pImpl)
    1.31 +        pImpl->AddRef();
    1.32 +}
    1.33 +
    1.34 +DeviceHandle::DeviceHandle(const DeviceHandle& src) : pImpl(src.pImpl)
    1.35 +{
    1.36 +    if (pImpl)
    1.37 +        pImpl->AddRef();
    1.38 +}    
    1.39 +
    1.40 +DeviceHandle::~DeviceHandle()
    1.41 +{
    1.42 +    if (pImpl)
    1.43 +        pImpl->Release();
    1.44 +}
    1.45 +
    1.46 +void DeviceHandle::operator = (const DeviceHandle& src)
    1.47 +{
    1.48 +    if (src.pImpl)
    1.49 +        src.pImpl->AddRef();
    1.50 +    if (pImpl)
    1.51 +        pImpl->Release();
    1.52 +    pImpl = src.pImpl;
    1.53 +}
    1.54 +
    1.55 +DeviceBase* DeviceHandle::GetDevice_AddRef() const
    1.56 +{ 
    1.57 +    if (pImpl && pImpl->pDevice)
    1.58 +    {
    1.59 +        pImpl->pDevice->AddRef();
    1.60 +        return pImpl->pDevice;
    1.61 +    }
    1.62 +    return NULL;
    1.63 +}
    1.64 +
    1.65 +// Returns true, if the handle contains the same device ptr
    1.66 +// as specified in the parameter.
    1.67 +bool DeviceHandle::IsDevice(DeviceBase* pdev) const
    1.68 +{
    1.69 +    return (pdev && pImpl && pImpl->pDevice) ? 
    1.70 +        pImpl->pDevice == pdev : false;
    1.71 +}
    1.72 +
    1.73 +DeviceType  DeviceHandle::GetType() const
    1.74 +{
    1.75 +    return pImpl ? pImpl->Type : Device_None;
    1.76 +}
    1.77 +
    1.78 +bool DeviceHandle::GetDeviceInfo(DeviceInfo* info) const
    1.79 +{
    1.80 +    return pImpl ? pImpl->GetDeviceInfo(info) : false;
    1.81 +}
    1.82 +bool DeviceHandle::IsAvailable() const
    1.83 +{
    1.84 +    // This isn't "atomically safe", but the function only returns the
    1.85 +    // recent state that may change.
    1.86 +    return pImpl ? (pImpl->Enumerated && pImpl->pLock->pManager) : false;
    1.87 +}
    1.88 +
    1.89 +bool DeviceHandle::IsCreated() const
    1.90 +{
    1.91 +    return pImpl ? (pImpl->pDevice != 0) : false;
    1.92 +}
    1.93 +
    1.94 +DeviceBase* DeviceHandle::CreateDevice()
    1.95 +{       
    1.96 +    if (!pImpl)
    1.97 +        return 0;
    1.98 +    
    1.99 +    DeviceBase*            device = 0;
   1.100 +    Ptr<DeviceManagerImpl> manager= 0;
   1.101 +
   1.102 +    // Since both manager and device pointers can only be destroyed during a lock,
   1.103 +    // hold it while checking for availability.
   1.104 +    // AddRef to manager so that it doesn't get released on us.
   1.105 +    {
   1.106 +        Lock::Locker deviceLockScope(pImpl->GetLock());
   1.107 +
   1.108 +        if (pImpl->pDevice)
   1.109 +        {
   1.110 +            pImpl->pDevice->AddRef();
   1.111 +            return pImpl->pDevice;
   1.112 +        }
   1.113 +        manager = pImpl->GetManagerImpl();
   1.114 +    }
   1.115 +
   1.116 +    if (manager)
   1.117 +    {
   1.118 +        if (manager->GetThreadId() != OVR::GetCurrentThreadId())
   1.119 +        {
   1.120 +            // Queue up a CreateDevice request. This fills in '&device' with AddRefed value,
   1.121 +            // or keep it at null.
   1.122 +            manager->GetThreadQueue()->PushCallAndWaitResult(
   1.123 +                manager.GetPtr(), &DeviceManagerImpl::CreateDevice_MgrThread,
   1.124 +                &device, pImpl, (DeviceBase*)0);
   1.125 +        }
   1.126 +        else
   1.127 +            device = manager->CreateDevice_MgrThread(pImpl, (DeviceBase*)0);
   1.128 +    }
   1.129 +    return device;
   1.130 +}
   1.131 +
   1.132 +void DeviceHandle::Clear()
   1.133 +{
   1.134 +    if (pImpl)
   1.135 +    {
   1.136 +        pImpl->Release();
   1.137 +        pImpl = 0;
   1.138 +    }
   1.139 +}
   1.140 +
   1.141 +bool DeviceHandle::enumerateNext(const DeviceEnumerationArgs& args)
   1.142 +{
   1.143 +    if (GetType() == Device_None)
   1.144 +        return false;
   1.145 +    
   1.146 +    Ptr<DeviceManagerImpl> managerKeepAlive;
   1.147 +    Lock::Locker           lockScope(pImpl->GetLock());
   1.148 +    
   1.149 +    DeviceCreateDesc* next = pImpl;
   1.150 +    // If manager was destroyed, we get removed from the list.
   1.151 +    if (!pImpl->pNext)
   1.152 +        return false;
   1.153 +
   1.154 +    managerKeepAlive = next->GetManagerImpl();
   1.155 +    OVR_ASSERT(managerKeepAlive);
   1.156 +    
   1.157 +    do {
   1.158 +        next = next->pNext;
   1.159 +
   1.160 +        if (managerKeepAlive->Devices.IsNull(next))
   1.161 +        {
   1.162 +            pImpl->Release();
   1.163 +            pImpl = 0;
   1.164 +            return false;
   1.165 +        }
   1.166 +
   1.167 +    } while(!args.MatchRule(next->Type, next->Enumerated));
   1.168 +
   1.169 +    next->AddRef();
   1.170 +    pImpl->Release();
   1.171 +    pImpl = next;
   1.172 +
   1.173 +    return true;
   1.174 +}
   1.175 +
   1.176 +} // namespace OVR
   1.177 +