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