Skip to main content

servo_webxr_api/
device.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
5//! Traits to be implemented by backends
6
7use euclid::{Point2D, RigidTransform3D};
8use profile_traits::generic_callback::GenericCallback as ProfileGenericCallback;
9use servo_base::generic_channel::GenericCallback;
10
11use crate::{
12    ContextId, EnvironmentBlendMode, Error, Event, Floor, Frame, HitTestId, HitTestSource,
13    InputSource, LayerId, LayerInit, Native, Quitter, Session, SessionBuilder, SessionInit,
14    SessionMode, Viewports,
15};
16
17/// A trait for discovering XR devices
18pub trait DiscoveryAPI<GL>: 'static {
19    fn request_session(
20        &mut self,
21        mode: SessionMode,
22        init: &SessionInit,
23        xr: SessionBuilder<GL>,
24    ) -> Result<Session, Error>;
25    fn supports_session(&self, mode: SessionMode) -> bool;
26    fn poll_device_changed(&mut self) -> bool {
27        false
28    }
29    fn set_waker(&mut self, _waker: GenericCallback<()>) {}
30}
31
32/// A trait for using an XR device
33pub trait DeviceAPI: 'static {
34    /// Create a new layer
35    fn create_layer(&mut self, context_id: ContextId, init: LayerInit) -> Result<LayerId, Error>;
36
37    /// Destroy a layer
38    fn destroy_layer(&mut self, context_id: ContextId, layer_id: LayerId);
39
40    /// The transform from native coordinates to the floor.
41    fn floor_transform(&self) -> Option<RigidTransform3D<f32, Native, Floor>>;
42
43    fn viewports(&self) -> Viewports;
44
45    /// Begin an animation frame.
46    fn begin_animation_frame(&mut self, layers: &[(ContextId, LayerId)]) -> Option<Frame>;
47
48    /// End an animation frame, render the layer to the device, and block waiting for the next frame.
49    fn end_animation_frame(&mut self, layers: &[(ContextId, LayerId)]);
50
51    /// Inputs registered with the device on initialization. More may be added, which
52    /// should be communicated through a yet-undecided event mechanism
53    fn initial_inputs(&self) -> Vec<InputSource>;
54
55    /// Sets the event handling channel
56    fn set_event_dest(&mut self, dest: ProfileGenericCallback<Event>);
57
58    /// Quit the session
59    fn quit(&mut self);
60
61    fn set_quitter(&mut self, quitter: Quitter);
62
63    fn update_clip_planes(&mut self, near: f32, far: f32);
64
65    fn environment_blend_mode(&self) -> EnvironmentBlendMode {
66        // for VR devices, override for AR
67        EnvironmentBlendMode::Opaque
68    }
69
70    fn granted_features(&self) -> &[String];
71
72    fn request_hit_test(&mut self, _source: HitTestSource) {
73        panic!("This device does not support requesting hit tests");
74    }
75
76    fn cancel_hit_test(&mut self, _id: HitTestId) {
77        panic!("This device does not support hit tests");
78    }
79
80    fn update_frame_rate(&mut self, rate: f32) -> f32 {
81        rate
82    }
83
84    fn supported_frame_rates(&self) -> Vec<f32> {
85        Vec::new()
86    }
87
88    fn reference_space_bounds(&self) -> Option<Vec<Point2D<f32, Floor>>> {
89        None
90    }
91}
92
93impl<GL: 'static> DiscoveryAPI<GL> for Box<dyn DiscoveryAPI<GL>> {
94    fn request_session(
95        &mut self,
96        mode: SessionMode,
97        init: &SessionInit,
98        xr: SessionBuilder<GL>,
99    ) -> Result<Session, Error> {
100        (**self).request_session(mode, init, xr)
101    }
102
103    fn supports_session(&self, mode: SessionMode) -> bool {
104        (**self).supports_session(mode)
105    }
106
107    fn poll_device_changed(&mut self) -> bool {
108        (**self).poll_device_changed()
109    }
110
111    fn set_waker(&mut self, waker: GenericCallback<()>) {
112        (**self).set_waker(waker);
113    }
114}