script/dom/webxr/
xrlayer.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 canvas_traits::webgl::WebGLContextId;
6use dom_struct::dom_struct;
7use webxr_api::LayerId;
8
9use crate::canvas_context::CanvasContext as _;
10use crate::dom::bindings::inheritance::Castable;
11use crate::dom::bindings::root::Dom;
12use crate::dom::eventtarget::EventTarget;
13use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
14use crate::dom::xrframe::XRFrame;
15use crate::dom::xrsession::XRSession;
16use crate::dom::xrwebgllayer::XRWebGLLayer;
17
18#[dom_struct]
19pub(crate) struct XRLayer {
20    event_target: EventTarget,
21    session: Dom<XRSession>,
22    context: Dom<WebGLRenderingContext>,
23    /// If none, the session is inline (the composition disabled flag is true)
24    /// and this is a XRWebGLLayer.
25    #[no_trace]
26    layer_id: Option<LayerId>,
27}
28
29impl XRLayer {
30    pub(crate) fn new_inherited(
31        session: &XRSession,
32        context: &WebGLRenderingContext,
33        layer_id: Option<LayerId>,
34    ) -> XRLayer {
35        XRLayer {
36            event_target: EventTarget::new_inherited(),
37            session: Dom::from_ref(session),
38            context: Dom::from_ref(context),
39            layer_id,
40        }
41    }
42
43    pub(crate) fn layer_id(&self) -> Option<LayerId> {
44        self.layer_id
45    }
46
47    pub(crate) fn context_id(&self) -> WebGLContextId {
48        self.context.context_id()
49    }
50
51    pub(crate) fn context(&self) -> &WebGLRenderingContext {
52        &self.context
53    }
54
55    pub(crate) fn session(&self) -> &XRSession {
56        &self.session
57    }
58
59    pub(crate) fn begin_frame(&self, frame: &XRFrame) -> Option<()> {
60        // TODO: Implement this for other layer types
61        if let Some(this) = self.downcast::<XRWebGLLayer>() {
62            this.begin_frame(frame)
63        } else {
64            unimplemented!()
65        }
66    }
67
68    pub(crate) fn end_frame(&self, frame: &XRFrame) -> Option<()> {
69        // TODO: Implement this for other layer types
70        if let Some(this) = self.downcast::<XRWebGLLayer>() {
71            this.end_frame(frame)
72        } else {
73            unimplemented!()
74        }
75    }
76}