1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef DEVICE_VR_OCULUS_OCULUS_DEVICE_H_
6 #define DEVICE_VR_OCULUS_OCULUS_DEVICE_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "base/single_thread_task_runner.h"
12 #include "device/vr/public/mojom/vr_service.mojom.h"
13 #include "device/vr/vr_device_base.h"
14 #include "mojo/public/cpp/bindings/pending_receiver.h"
15 #include "mojo/public/cpp/bindings/pending_remote.h"
16 #include "mojo/public/cpp/bindings/receiver.h"
17 #include "third_party/libovr/src/Include/OVR_CAPI.h"
18 
19 namespace device {
20 
21 class XRCompositorCommon;
22 
23 class DEVICE_VR_EXPORT OculusDevice
24     : public VRDeviceBase,
25       public mojom::XRSessionController,
26       public mojom::XRCompositorHost {
27  public:
28   OculusDevice();
29   ~OculusDevice() override;
30 
31   static bool IsHwAvailable();
32   static bool IsApiAvailable();
33 
34   // VRDeviceBase
35   void RequestSession(
36       mojom::XRRuntimeSessionOptionsPtr options,
37       mojom::XRRuntime::RequestSessionCallback callback) override;
38   void OnRequestSessionResult(mojom::XRRuntime::RequestSessionCallback callback,
39                               bool result,
40                               mojom::XRSessionPtr session);
41 
42   bool IsAvailable();
43 
44   mojo::PendingRemote<mojom::XRCompositorHost> BindCompositorHost();
45 
46  private:
47   // XRSessionController
48   void SetFrameDataRestricted(bool restricted) override;
49 
50   void OnPresentingControllerMojoConnectionError();
51 
52   // XRCompositorHost
53   void CreateImmersiveOverlay(
54       mojo::PendingReceiver<mojom::ImmersiveOverlay> overlay_receiver) override;
55 
56   void OnPresentationEnded();
57   bool EnsureValidDisplayInfo();
58   void StartOvrSession();
59   void StopOvrSession();
60 
61   int outstanding_session_requests_count_ = 0;
62   bool have_real_display_info_ = false;
63   std::unique_ptr<XRCompositorCommon> render_loop_;
64   ovrSession session_ = nullptr;
65   scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
66 
67   mojo::Receiver<mojom::XRSessionController> exclusive_controller_receiver_{
68       this};
69 
70   mojo::Receiver<mojom::XRCompositorHost> compositor_host_receiver_{this};
71   mojo::PendingReceiver<mojom::ImmersiveOverlay> overlay_receiver_;
72 
73   base::WeakPtrFactory<OculusDevice> weak_ptr_factory_;
74 
75   DISALLOW_COPY_AND_ASSIGN(OculusDevice);
76 };
77 
78 }  // namespace device
79 
80 #endif  // DEVICE_VR_OCULUS_OCULUS_DEVICE_H_
81