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