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