script/dom/webxr/
xrwebglbinding.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 dom_struct::dom_struct;
6use js::rust::HandleObject;
7
8use crate::dom::bindings::codegen::Bindings::XRViewBinding::XREye;
9use crate::dom::bindings::codegen::Bindings::XRWebGLBindingBinding::XRWebGLBinding_Binding::XRWebGLBindingMethods;
10use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContext_Binding::WebGLRenderingContextMethods;
11use crate::dom::bindings::codegen::Bindings::XRWebGLBindingBinding::{
12    XRCubeLayerInit, XRCylinderLayerInit, XREquirectLayerInit, XRProjectionLayerInit,
13    XRQuadLayerInit, XRTextureType,
14};
15use crate::dom::bindings::codegen::UnionTypes::WebGLRenderingContextOrWebGL2RenderingContext;
16use crate::dom::bindings::error::{Error, Fallible};
17use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
20use crate::dom::window::Window;
21use crate::dom::xrcompositionlayer::XRCompositionLayer;
22use crate::dom::xrcubelayer::XRCubeLayer;
23use crate::dom::xrcylinderlayer::XRCylinderLayer;
24use crate::dom::xrequirectlayer::XREquirectLayer;
25use crate::dom::xrframe::XRFrame;
26use crate::dom::xrprojectionlayer::XRProjectionLayer;
27use crate::dom::xrquadlayer::XRQuadLayer;
28use crate::dom::xrsession::XRSession;
29use crate::dom::xrview::XRView;
30use crate::dom::xrwebglsubimage::XRWebGLSubImage;
31use crate::script_runtime::CanGc;
32
33#[dom_struct]
34pub(crate) struct XRWebGLBinding {
35    reflector: Reflector,
36    session: Dom<XRSession>,
37    context: Dom<WebGLRenderingContext>,
38}
39
40impl XRWebGLBinding {
41    pub(crate) fn new_inherited(
42        session: &XRSession,
43        context: &WebGLRenderingContext,
44    ) -> XRWebGLBinding {
45        XRWebGLBinding {
46            reflector: Reflector::new(),
47            session: Dom::from_ref(session),
48            context: Dom::from_ref(context),
49        }
50    }
51
52    fn new(
53        global: &Window,
54        proto: Option<HandleObject>,
55        session: &XRSession,
56        context: &WebGLRenderingContext,
57        can_gc: CanGc,
58    ) -> DomRoot<XRWebGLBinding> {
59        reflect_dom_object_with_proto(
60            Box::new(XRWebGLBinding::new_inherited(session, context)),
61            global,
62            proto,
63            can_gc,
64        )
65    }
66}
67
68impl XRWebGLBindingMethods<crate::DomTypeHolder> for XRWebGLBinding {
69    /// <https://immersive-web.github.io/layers/#dom-xrwebglbinding-xrwebglbinding>
70    fn Constructor(
71        global: &Window,
72        proto: Option<HandleObject>,
73        can_gc: CanGc,
74        session: &XRSession,
75        context: WebGLRenderingContextOrWebGL2RenderingContext,
76    ) -> Fallible<DomRoot<XRWebGLBinding>> {
77        let context = match context {
78            WebGLRenderingContextOrWebGL2RenderingContext::WebGLRenderingContext(ctx) => ctx,
79            WebGLRenderingContextOrWebGL2RenderingContext::WebGL2RenderingContext(ctx) => {
80                ctx.base_context()
81            },
82        };
83        // Step 2
84        if session.is_ended() {
85            return Err(Error::InvalidState);
86        }
87
88        // step 3
89        if context.IsContextLost() {
90            return Err(Error::InvalidState);
91        }
92
93        // Step 4
94        if !session.is_immersive() {
95            return Err(Error::InvalidState);
96        };
97
98        // Step 5 throw an InvalidStateError If context’s XR compatible boolean is false.
99
100        Ok(XRWebGLBinding::new(
101            global, proto, session, &context, can_gc,
102        ))
103    }
104
105    /// <https://immersive-web.github.io/layers/#dom-xrwebglbinding-createprojectionlayer>
106    fn CreateProjectionLayer(
107        &self,
108        _: XRTextureType,
109        _: &XRProjectionLayerInit,
110    ) -> Fallible<DomRoot<XRProjectionLayer>> {
111        // https://github.com/servo/servo/issues/27468
112        Err(Error::NotSupported)
113    }
114
115    /// <https://immersive-web.github.io/layers/#dom-xrwebglbinding-createquadlayer>
116    fn CreateQuadLayer(
117        &self,
118        _: XRTextureType,
119        _: &Option<XRQuadLayerInit>,
120    ) -> Fallible<DomRoot<XRQuadLayer>> {
121        // https://github.com/servo/servo/issues/27493
122        Err(Error::NotSupported)
123    }
124
125    /// <https://immersive-web.github.io/layers/#dom-xrwebglbinding-createcylinderlayer>
126    fn CreateCylinderLayer(
127        &self,
128        _: XRTextureType,
129        _: &Option<XRCylinderLayerInit>,
130    ) -> Fallible<DomRoot<XRCylinderLayer>> {
131        // https://github.com/servo/servo/issues/27493
132        Err(Error::NotSupported)
133    }
134
135    /// <https://immersive-web.github.io/layers/#dom-xrwebglbinding-createequirectlayer>
136    fn CreateEquirectLayer(
137        &self,
138        _: XRTextureType,
139        _: &Option<XREquirectLayerInit>,
140    ) -> Fallible<DomRoot<XREquirectLayer>> {
141        // https://github.com/servo/servo/issues/27493
142        Err(Error::NotSupported)
143    }
144
145    /// <https://immersive-web.github.io/layers/#dom-xrwebglbinding-createcubelayer>
146    fn CreateCubeLayer(&self, _: &Option<XRCubeLayerInit>) -> Fallible<DomRoot<XRCubeLayer>> {
147        // https://github.com/servo/servo/issues/27493
148        Err(Error::NotSupported)
149    }
150
151    /// <https://immersive-web.github.io/layers/#dom-xrwebglbinding-getsubimage>
152    fn GetSubImage(
153        &self,
154        _: &XRCompositionLayer,
155        _: &XRFrame,
156        _: XREye,
157    ) -> Fallible<DomRoot<XRWebGLSubImage>> {
158        // https://github.com/servo/servo/issues/27468
159        Err(Error::NotSupported)
160    }
161
162    /// <https://immersive-web.github.io/layers/#dom-xrwebglbinding-getviewsubimage>
163    fn GetViewSubImage(
164        &self,
165        _: &XRProjectionLayer,
166        _: &XRView,
167    ) -> Fallible<DomRoot<XRWebGLSubImage>> {
168        // https://github.com/servo/servo/issues/27468
169        Err(Error::NotSupported)
170    }
171}