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    #[ignore_malloc_size_of = "Layer ids don't heap-allocate"]
26    #[no_trace]
27    layer_id: Option<LayerId>,
28}
29
30impl XRLayer {
31    #[allow(dead_code)]
32    pub(crate) fn new_inherited(
33        session: &XRSession,
34        context: &WebGLRenderingContext,
35        layer_id: Option<LayerId>,
36    ) -> XRLayer {
37        XRLayer {
38            event_target: EventTarget::new_inherited(),
39            session: Dom::from_ref(session),
40            context: Dom::from_ref(context),
41            layer_id,
42        }
43    }
44
45    pub(crate) fn layer_id(&self) -> Option<LayerId> {
46        self.layer_id
47    }
48
49    pub(crate) fn context_id(&self) -> WebGLContextId {
50        self.context.context_id()
51    }
52
53    pub(crate) fn context(&self) -> &WebGLRenderingContext {
54        &self.context
55    }
56
57    pub(crate) fn session(&self) -> &XRSession {
58        &self.session
59    }
60
61    pub(crate) fn begin_frame(&self, frame: &XRFrame) -> Option<()> {
62        // TODO: Implement this for other layer types
63        if let Some(this) = self.downcast::<XRWebGLLayer>() {
64            this.begin_frame(frame)
65        } else {
66            unimplemented!()
67        }
68    }
69
70    pub(crate) fn end_frame(&self, frame: &XRFrame) -> Option<()> {
71        // TODO: Implement this for other layer types
72        if let Some(this) = self.downcast::<XRWebGLLayer>() {
73            this.end_frame(frame)
74        } else {
75            unimplemented!()
76        }
77    }
78}