Skip to main content

script/dom/webxr/
xrwebgllayer.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 std::convert::TryInto;
6
7use dom_struct::dom_struct;
8use euclid::{Rect, Size2D};
9use js::rust::HandleObject;
10use script_bindings::reflector::reflect_dom_object_with_proto;
11use servo_canvas_traits::webgl::{WebGLCommand, WebGLContextId, WebGLTextureId};
12use webxr_api::{ContextId as WebXRContextId, LayerId, LayerInit, Viewport};
13
14use crate::canvas_context::CanvasContext;
15use crate::conversions::Convert;
16use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants as constants;
17use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextMethods;
18use crate::dom::bindings::codegen::Bindings::XRWebGLLayerBinding::{
19    XRWebGLLayerInit, XRWebGLLayerMethods, XRWebGLRenderingContext,
20};
21use crate::dom::bindings::error::{Error, Fallible};
22use crate::dom::bindings::inheritance::Castable;
23use crate::dom::bindings::num::Finite;
24use crate::dom::bindings::reflector::DomGlobal;
25use crate::dom::bindings::root::{Dom, DomRoot};
26use crate::dom::globalscope::GlobalScope;
27use crate::dom::webgl::webglframebuffer::WebGLFramebuffer;
28use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
29use crate::dom::webgl::webgltexture::WebGLTexture;
30use crate::dom::window::Window;
31use crate::dom::xrframe::XRFrame;
32use crate::dom::xrlayer::XRLayer;
33use crate::dom::xrsession::XRSession;
34use crate::dom::xrview::XRView;
35use crate::dom::xrviewport::XRViewport;
36use crate::script_runtime::CanGc;
37
38impl Convert<LayerInit> for XRWebGLLayerInit {
39    fn convert(self) -> LayerInit {
40        LayerInit::WebGLLayer {
41            alpha: self.alpha,
42            antialias: self.antialias,
43            depth: self.depth,
44            stencil: self.stencil,
45            framebuffer_scale_factor: *self.framebufferScaleFactor as f32,
46            ignore_depth_values: self.ignoreDepthValues,
47        }
48    }
49}
50
51#[dom_struct]
52pub(crate) struct XRWebGLLayer {
53    xr_layer: XRLayer,
54    antialias: bool,
55    depth: bool,
56    stencil: bool,
57    alpha: bool,
58    ignore_depth_values: bool,
59    /// If none, this is an inline session (the composition disabled flag is true)
60    framebuffer: Option<Dom<WebGLFramebuffer>>,
61}
62
63impl XRWebGLLayer {
64    pub(crate) fn new_inherited(
65        session: &XRSession,
66        context: &WebGLRenderingContext,
67        init: &XRWebGLLayerInit,
68        framebuffer: Option<&WebGLFramebuffer>,
69        layer_id: Option<LayerId>,
70    ) -> XRWebGLLayer {
71        XRWebGLLayer {
72            xr_layer: XRLayer::new_inherited(session, context, layer_id),
73            antialias: init.antialias,
74            depth: init.depth,
75            stencil: init.stencil,
76            alpha: init.alpha,
77            ignore_depth_values: init.ignoreDepthValues,
78            framebuffer: framebuffer.map(Dom::from_ref),
79        }
80    }
81
82    #[expect(clippy::too_many_arguments)]
83    fn new(
84        global: &GlobalScope,
85        proto: Option<HandleObject>,
86        session: &XRSession,
87        context: &WebGLRenderingContext,
88        init: &XRWebGLLayerInit,
89        framebuffer: Option<&WebGLFramebuffer>,
90        layer_id: Option<LayerId>,
91        can_gc: CanGc,
92    ) -> DomRoot<XRWebGLLayer> {
93        reflect_dom_object_with_proto(
94            Box::new(XRWebGLLayer::new_inherited(
95                session,
96                context,
97                init,
98                framebuffer,
99                layer_id,
100            )),
101            global,
102            proto,
103            can_gc,
104        )
105    }
106
107    pub(crate) fn layer_id(&self) -> Option<LayerId> {
108        self.xr_layer.layer_id()
109    }
110
111    pub(crate) fn context_id(&self) -> WebGLContextId {
112        self.xr_layer.context_id()
113    }
114
115    pub(crate) fn session(&self) -> &XRSession {
116        self.xr_layer.session()
117    }
118
119    pub(crate) fn size(&self) -> Size2D<u32, Viewport> {
120        if let Some(framebuffer) = self.framebuffer.as_ref() {
121            let size = framebuffer.size().unwrap_or((0, 0));
122            Size2D::new(
123                size.0.try_into().unwrap_or(0),
124                size.1.try_into().unwrap_or(0),
125            )
126        } else {
127            Size2D::from_untyped(self.context().size())
128        }
129    }
130
131    fn texture_target(&self) -> u32 {
132        if cfg!(target_os = "macos") {
133            glow::TEXTURE_RECTANGLE
134        } else {
135            glow::TEXTURE_2D
136        }
137    }
138
139    pub(crate) fn begin_frame(&self, frame: &XRFrame) -> Option<()> {
140        debug!("XRWebGLLayer begin frame");
141        let framebuffer = self.framebuffer.as_ref()?;
142        let sub_images = frame.get_sub_images(self.layer_id()?)?;
143        let session = self.session();
144        let context = framebuffer.upcast().context()?;
145
146        // TODO: Cache this texture
147        let color_texture_id = WebGLTextureId::new(sub_images.sub_image.as_ref()?.color_texture?);
148        let color_texture = WebGLTexture::new_webxr(
149            &context,
150            color_texture_id,
151            session,
152            CanGc::deprecated_note(),
153        );
154        let target = self.texture_target();
155
156        // Save the current bindings
157        let saved_framebuffer = context.get_draw_framebuffer_slot().get();
158        let saved_framebuffer_target = framebuffer.target();
159        let saved_texture_id = context
160            .textures()
161            .active_texture_slot(target, context.webgl_version())
162            .ok()
163            .and_then(|slot| slot.get().map(|texture| texture.id()));
164
165        // We have to pick a framebuffer target.
166        // If there is a draw framebuffer, we use its target,
167        // otherwise we just use DRAW_FRAMEBUFFER.
168        let framebuffer_target = saved_framebuffer
169            .as_ref()
170            .and_then(|fb| fb.target())
171            .unwrap_or(constants::DRAW_FRAMEBUFFER);
172
173        // Update the attachments
174        context.send_command(WebGLCommand::BindTexture(target, Some(color_texture_id)));
175        framebuffer.bind(framebuffer_target);
176        framebuffer
177            .texture2d_even_if_opaque(
178                constants::COLOR_ATTACHMENT0,
179                self.texture_target(),
180                Some(&color_texture),
181                0,
182            )
183            .ok()?;
184        if let Some(id) = sub_images.sub_image.as_ref()?.depth_stencil_texture {
185            // TODO: Cache this texture
186            let depth_stencil_texture_id = WebGLTextureId::new(id);
187            let depth_stencil_texture = WebGLTexture::new_webxr(
188                &context,
189                depth_stencil_texture_id,
190                session,
191                CanGc::deprecated_note(),
192            );
193            framebuffer
194                .texture2d_even_if_opaque(
195                    constants::DEPTH_STENCIL_ATTACHMENT,
196                    constants::TEXTURE_2D,
197                    Some(&depth_stencil_texture),
198                    0,
199                )
200                .ok()?;
201        }
202
203        // Restore the old bindings
204        context.send_command(WebGLCommand::BindTexture(target, saved_texture_id));
205        if let Some(framebuffer_target) = saved_framebuffer_target {
206            framebuffer.bind(framebuffer_target);
207        }
208        if let Some(framebuffer) = saved_framebuffer {
209            framebuffer.bind(framebuffer_target);
210        }
211        Some(())
212    }
213
214    pub(crate) fn end_frame(&self, _frame: &XRFrame) -> Option<()> {
215        debug!("XRWebGLLayer end frame");
216        // TODO: invalidate the old texture
217        let framebuffer = self.framebuffer.as_ref()?;
218        // TODO: rebind the current bindings
219        framebuffer.bind(constants::FRAMEBUFFER);
220        framebuffer
221            .texture2d_even_if_opaque(constants::COLOR_ATTACHMENT0, self.texture_target(), None, 0)
222            .ok()?;
223        framebuffer
224            .texture2d_even_if_opaque(
225                constants::DEPTH_STENCIL_ATTACHMENT,
226                constants::DEPTH_STENCIL_ATTACHMENT,
227                None,
228                0,
229            )
230            .ok()?;
231
232        if let Some(context) = framebuffer.upcast().context() {
233            context.Flush();
234        }
235
236        Some(())
237    }
238
239    pub(crate) fn context(&self) -> &WebGLRenderingContext {
240        self.xr_layer.context()
241    }
242}
243
244impl XRWebGLLayerMethods<crate::DomTypeHolder> for XRWebGLLayer {
245    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-xrwebgllayer>
246    fn Constructor(
247        global: &Window,
248        proto: Option<HandleObject>,
249        can_gc: CanGc,
250        session: &XRSession,
251        context: XRWebGLRenderingContext,
252        init: &XRWebGLLayerInit,
253    ) -> Fallible<DomRoot<Self>> {
254        let context = match context {
255            XRWebGLRenderingContext::WebGLRenderingContext(ctx) => ctx,
256            XRWebGLRenderingContext::WebGL2RenderingContext(ctx) => ctx.base_context(),
257        };
258
259        // Step 2
260        if session.is_ended() {
261            return Err(Error::InvalidState(None));
262        }
263        // XXXManishearth step 3: throw error if context is lost
264        // XXXManishearth step 4: check XR compat flag for immersive sessions
265
266        let (framebuffer, layer_id) = if session.is_immersive() {
267            // Step 9.2. "Initialize layer’s framebuffer to a new opaque framebuffer created with context."
268            let size = session
269                .with_session(|session| session.recommended_framebuffer_resolution())
270                .ok_or(Error::Operation(None))?;
271            let framebuffer = WebGLFramebuffer::maybe_new_webxr(session, &context, size, can_gc)
272                .ok_or(Error::Operation(None))?;
273
274            // Step 9.3. "Allocate and initialize resources compatible with session’s XR device,
275            // including GPU accessible memory buffers, as required to support the compositing of layer."
276            let context_id = WebXRContextId::from(context.context_id());
277            let layer_init: LayerInit = init.convert();
278            let layer_id = session
279                .with_session(|session| session.create_layer(context_id, layer_init))
280                .map_err(|_| Error::Operation(None))?;
281
282            // Step 9.4: "If layer’s resources were unable to be created for any reason,
283            // throw an OperationError and abort these steps."
284            (Some(framebuffer), Some(layer_id))
285        } else {
286            (None, None)
287        };
288
289        // Ensure that we finish setting up this layer before continuing.
290        context.Finish();
291
292        // Step 10. "Return layer."
293        Ok(XRWebGLLayer::new(
294            &global.global(),
295            proto,
296            session,
297            &context,
298            init,
299            framebuffer.as_deref(),
300            layer_id,
301            can_gc,
302        ))
303    }
304
305    /// <https://www.w3.org/TR/webxr/#dom-xrwebgllayer-getnativeframebufferscalefactor>
306    fn GetNativeFramebufferScaleFactor(_window: &Window, session: &XRSession) -> Finite<f64> {
307        let value: f64 = if session.is_ended() { 0.0 } else { 1.0 };
308        Finite::wrap(value)
309    }
310
311    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-antialias>
312    fn Antialias(&self) -> bool {
313        self.antialias
314    }
315
316    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-ignoredepthvalues>
317    fn IgnoreDepthValues(&self) -> bool {
318        self.ignore_depth_values
319    }
320
321    /// <https://www.w3.org/TR/webxr/#dom-xrwebgllayer-fixedfoveation>
322    fn GetFixedFoveation(&self) -> Option<Finite<f32>> {
323        // Fixed foveation is only available on Quest/Pico headset runtimes
324        None
325    }
326
327    /// <https://www.w3.org/TR/webxr/#dom-xrwebgllayer-fixedfoveation>
328    fn SetFixedFoveation(&self, _value: Option<Finite<f32>>) {
329        // no-op until fixed foveation is supported
330    }
331
332    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-framebuffer>
333    fn GetFramebuffer(&self) -> Option<DomRoot<WebGLFramebuffer>> {
334        self.framebuffer.as_ref().map(|x| DomRoot::from_ref(&**x))
335    }
336
337    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-framebufferwidth>
338    fn FramebufferWidth(&self) -> u32 {
339        self.size().width
340    }
341
342    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-framebufferheight>
343    fn FramebufferHeight(&self) -> u32 {
344        self.size().height
345    }
346
347    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-getviewport>
348    fn GetViewport(&self, view: &XRView) -> Option<DomRoot<XRViewport>> {
349        if self.session() != view.session() {
350            return None;
351        }
352
353        let index = view.viewport_index();
354
355        let viewport = self.session().with_session(|s| {
356            // Inline sessions
357            if s.viewports().is_empty() {
358                Rect::from_size(self.size().to_i32())
359            } else {
360                s.viewports()[index]
361            }
362        });
363
364        // NOTE: According to spec, viewport sizes should be recalculated here if the
365        // requested viewport scale has changed. However, existing browser implementations
366        // don't seem to do this for stereoscopic immersive sessions.
367        // Revisit if Servo gets support for handheld AR/VR via ARCore/ARKit
368
369        Some(XRViewport::new(
370            &self.global(),
371            viewport,
372            CanGc::deprecated_note(),
373        ))
374    }
375}