script/dom/webgl/
webglobject.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::{WebGLCommand, WebGLContextId, WebGLMsgSender};
6// https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl
7use dom_struct::dom_struct;
8use script_bindings::root::DomRoot;
9use script_bindings::weakref::WeakRef;
10
11use crate::dom::bindings::cell::DomRefCell;
12use crate::dom::bindings::codegen::Bindings::WebGLObjectBinding::WebGLObjectMethods;
13use crate::dom::bindings::reflector::Reflector;
14use crate::dom::bindings::str::USVString;
15use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
16use crate::dom::webglrenderingcontext::{Operation, capture_webgl_backtrace};
17
18#[dom_struct]
19pub(crate) struct WebGLObject {
20    reflector_: Reflector,
21    #[no_trace]
22    webgl_sender: WebGLMsgSender,
23    context: WeakRef<WebGLRenderingContext>,
24    label: DomRefCell<USVString>,
25}
26
27impl WebGLObject {
28    pub(crate) fn new_inherited(context: &WebGLRenderingContext) -> WebGLObject {
29        WebGLObject {
30            reflector_: Reflector::new(),
31            webgl_sender: context.sender().clone(),
32            context: WeakRef::new(context),
33            label: DomRefCell::new(USVString::default()),
34        }
35    }
36
37    /// Get the [`WebGLRenderingContext`] that created this object.
38    ///
39    /// If `None` is returned the [`WebGLRenderingContext`] has already been garbage collected.
40    pub(crate) fn context(&self) -> Option<DomRoot<WebGLRenderingContext>> {
41        self.context.root()
42    }
43
44    #[inline]
45    pub(crate) fn context_id(&self) -> WebGLContextId {
46        self.webgl_sender.context_id()
47    }
48
49    #[inline]
50    pub(crate) fn send_with_fallibility(&self, command: WebGLCommand, fallibility: Operation) {
51        let result = self.webgl_sender.send(command, capture_webgl_backtrace());
52        if matches!(fallibility, Operation::Infallible) {
53            result.expect("Operation failed");
54        }
55    }
56
57    #[inline]
58    pub(crate) fn send_command(&self, command: WebGLCommand) {
59        self.send_with_fallibility(command, Operation::Infallible);
60    }
61}
62
63impl WebGLObjectMethods<crate::DomTypeHolder> for WebGLObject {
64    /// <https://registry.khronos.org/webgl/specs/latest/1.0/#5.3>
65    fn Label(&self) -> USVString {
66        self.label.borrow().clone()
67    }
68
69    /// <https://registry.khronos.org/webgl/specs/latest/1.0/#5.3>
70    fn SetLabel(&self, value: USVString) {
71        *self.label.borrow_mut() = value;
72    }
73}