1/*
2 * Copyright (C) 2017 Igalia, S.L. All right reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22#include "VRPlatformManagerOpenVR.h"
23
24#include "VRPlatformDisplayOpenVR.h"
25
26#if USE(OPENVR)
27
28#include "JSVRDisplay.h"
29#include "VRDisplay.h"
30
31namespace WebCore {
32
33std::unique_ptr<VRPlatformManagerOpenVR> VRPlatformManagerOpenVR::create()
34{
35 if (!vr::VR_IsRuntimeInstalled())
36 return nullptr;
37
38 return std::make_unique<VRPlatformManagerOpenVR>();
39}
40
41VRPlatformManagerOpenVR::~VRPlatformManagerOpenVR()
42{
43 vr::VR_Shutdown();
44}
45
46bool VRPlatformManagerOpenVR::initOpenVR()
47{
48 vr::HmdError error;
49 m_system = vr::VR_Init(&error, vr::VRApplication_Scene);
50 if (error)
51 return false;
52
53 return true;
54}
55
56Vector<WeakPtr<VRPlatformDisplay>> VRPlatformManagerOpenVR::getVRDisplays()
57{
58 // Quickly check for HMDs. Much faster than initializing the whole OpenVR API.
59 if (!vr::VR_IsHmdPresent()) {
60 m_system = nullptr;
61 return { };
62 }
63
64 if (!m_system && !initOpenVR()) {
65 m_system = nullptr;
66 vr::VR_Shutdown();
67 return { };
68 }
69
70 vr::HmdError error;
71 vr::IVRChaperone* chaperone = static_cast<vr::IVRChaperone*>(vr::VR_GetGenericInterface(vr::IVRChaperone_Version, &error));
72 if (error || !chaperone)
73 return { };
74
75 vr::IVRCompositor* compositor = static_cast<vr::IVRCompositor*>(vr::VR_GetGenericInterface(vr::IVRCompositor_Version, &error));
76 if (error || !compositor)
77 return { };
78
79 Vector<WeakPtr<VRPlatformDisplay>> displays;
80 if (!m_display)
81 m_display = std::make_unique<VRPlatformDisplayOpenVR>(m_system, chaperone, compositor);
82 displays.append(makeWeakPtr(*m_display));
83 return displays;
84}
85
86}; // namespace WebCore
87
88#endif // USE(OPENVR)
89