1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::cell::Cell;

use dom_struct::dom_struct;
use euclid::RigidTransform3D;
use js::typedarray::{Float32, Float32Array};
use webxr_api::{ApiSpace, View};

use crate::dom::bindings::buffer_source::HeapBufferSource;
use crate::dom::bindings::codegen::Bindings::XRViewBinding::{XREye, XRViewMethods};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::globalscope::GlobalScope;
use crate::dom::xrrigidtransform::XRRigidTransform;
use crate::dom::xrsession::{cast_transform, BaseSpace, BaseTransform, XRSession};
use crate::script_runtime::{CanGc, JSContext};

#[dom_struct]
pub struct XRView {
    reflector_: Reflector,
    session: Dom<XRSession>,
    eye: XREye,
    viewport_index: usize,
    #[ignore_malloc_size_of = "mozjs"]
    proj: HeapBufferSource<Float32>,
    #[ignore_malloc_size_of = "defined in rust-webxr"]
    #[no_trace]
    view: View<ApiSpace>,
    transform: Dom<XRRigidTransform>,
    requested_viewport_scale: Cell<f64>,
}

impl XRView {
    fn new_inherited(
        session: &XRSession,
        transform: &XRRigidTransform,
        eye: XREye,
        viewport_index: usize,
        view: View<ApiSpace>,
    ) -> XRView {
        XRView {
            reflector_: Reflector::new(),
            session: Dom::from_ref(session),
            eye,
            viewport_index,
            proj: HeapBufferSource::default(),
            view,
            transform: Dom::from_ref(transform),
            requested_viewport_scale: Cell::new(1.0),
        }
    }

    pub fn new<V: Copy>(
        global: &GlobalScope,
        session: &XRSession,
        view: &View<V>,
        eye: XREye,
        viewport_index: usize,
        to_base: &BaseTransform,
        can_gc: CanGc,
    ) -> DomRoot<XRView> {
        let transform: RigidTransform3D<f32, V, BaseSpace> = view.transform.then(to_base);
        let transform = XRRigidTransform::new(global, cast_transform(transform), can_gc);

        reflect_dom_object(
            Box::new(XRView::new_inherited(
                session,
                &transform,
                eye,
                viewport_index,
                view.cast_unit(),
            )),
            global,
        )
    }

    pub fn session(&self) -> &XRSession {
        &self.session
    }

    pub fn viewport_index(&self) -> usize {
        self.viewport_index
    }
}

impl XRViewMethods<crate::DomTypeHolder> for XRView {
    /// <https://immersive-web.github.io/webxr/#dom-xrview-eye>
    fn Eye(&self) -> XREye {
        self.eye
    }

    /// <https://immersive-web.github.io/webxr/#dom-xrview-projectionmatrix>
    fn ProjectionMatrix(&self, _cx: JSContext) -> Float32Array {
        if !self.proj.is_initialized() {
            let cx = GlobalScope::get_cx();
            // row_major since euclid uses row vectors
            let proj = self.view.projection.to_array();
            self.proj
                .set_data(cx, &proj)
                .expect("Failed to set projection matrix.")
        }
        self.proj
            .get_buffer()
            .expect("Failed to get projection matrix.")
    }

    /// <https://immersive-web.github.io/webxr/#dom-xrview-transform>
    fn Transform(&self) -> DomRoot<XRRigidTransform> {
        DomRoot::from_ref(&self.transform)
    }

    /// <https://www.w3.org/TR/webxr/#dom-xrview-recommendedviewportscale>
    fn GetRecommendedViewportScale(&self) -> Option<Finite<f64>> {
        // Just return 1.0 since we currently will always use full-sized viewports
        Finite::new(1.0)
    }

    /// <https://www.w3.org/TR/webxr/#dom-xrview-requestviewportscale>
    fn RequestViewportScale(&self, scale: Option<Finite<f64>>) {
        if let Some(scale) = scale {
            if *scale > 0.0 {
                let clamped_scale = scale.clamp(0.0, 1.0);
                self.requested_viewport_scale.set(clamped_scale);
            }
        }
    }

    /// <https://www.w3.org/TR/webxr-ar-module-1/#dom-xrview-isfirstpersonobserver>
    fn IsFirstPersonObserver(&self) -> bool {
        // Servo is not currently supported anywhere that supports this, so return false
        false
    }
}