Skip to main content

servo_webxr/
inline.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::thread;
6use std::time::Duration;
7
8use euclid::RigidTransform3D;
9use profile_traits::generic_callback::GenericCallback;
10use webxr_api::{
11    ContextId, DeviceAPI, DiscoveryAPI, Error, Event, EventBuffer, Floor, Frame, InputSource,
12    LayerId, LayerInit, Native, Quitter, Session, SessionBuilder, SessionInit, SessionMode,
13    ViewerPose, Viewports, Views,
14};
15
16pub struct InlineDiscovery;
17
18impl<GL: 'static> DiscoveryAPI<GL> for InlineDiscovery {
19    fn request_session(
20        &mut self,
21        mode: SessionMode,
22        init: &SessionInit,
23        xr: SessionBuilder<GL>,
24    ) -> Result<Session, Error> {
25        if mode != SessionMode::Inline {
26            return Err(Error::NoMatchingDevice);
27        }
28
29        let granted_features = init.validate(mode, &[])?;
30        xr.spawn(move |_grand_manager| Ok(InlineDevice::new(granted_features)))
31    }
32
33    fn supports_session(&self, mode: SessionMode) -> bool {
34        mode == SessionMode::Inline
35    }
36}
37
38pub struct InlineDevice {
39    events: EventBuffer,
40    granted_features: Vec<String>,
41}
42
43impl InlineDevice {
44    pub fn new(granted_features: Vec<String>) -> Self {
45        Self {
46            granted_features,
47            events: EventBuffer::default(),
48        }
49    }
50}
51
52impl DeviceAPI for InlineDevice {
53    fn create_layer(&mut self, _context_id: ContextId, _init: LayerInit) -> Result<LayerId, Error> {
54        Err(Error::InlineSession)
55    }
56
57    fn destroy_layer(&mut self, _context_id: ContextId, _layer_id: LayerId) {}
58
59    fn floor_transform(&self) -> Option<RigidTransform3D<f32, Native, Floor>> {
60        None
61    }
62
63    fn viewports(&self) -> Viewports {
64        Viewports { viewports: vec![] }
65    }
66
67    fn begin_animation_frame(&mut self, _layers: &[(ContextId, LayerId)]) -> Option<Frame> {
68        Some(Frame {
69            pose: Some(ViewerPose {
70                transform: RigidTransform3D::identity(),
71                views: Views::Inline,
72            }),
73            inputs: vec![],
74            events: vec![],
75            sub_images: vec![],
76            hit_test_results: vec![],
77            predicted_display_time: 0.0,
78        })
79    }
80
81    fn end_animation_frame(&mut self, _layers: &[(ContextId, LayerId)]) {
82        thread::sleep(Duration::from_millis(15)); // ~60 FPS
83    }
84
85    fn initial_inputs(&self) -> Vec<InputSource> {
86        vec![]
87    }
88
89    fn set_event_dest(&mut self, dest: GenericCallback<Event>) {
90        self.events.upgrade(dest);
91    }
92
93    fn quit(&mut self) {
94        self.events.callback(Event::SessionEnd);
95    }
96
97    fn set_quitter(&mut self, _quitter: Quitter) {}
98
99    fn update_clip_planes(&mut self, _near: f32, _far: f32) {}
100
101    fn granted_features(&self) -> &[String] {
102        &self.granted_features
103    }
104}