Skip to main content

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