script/dom/webgpu/
gpucommandbuffer.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 webgpu_traits::{WebGPU, WebGPUCommandBuffer, WebGPURequest};
7
8use crate::dom::bindings::cell::DomRefCell;
9use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUCommandBufferMethods;
10use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::bindings::str::USVString;
13use crate::dom::globalscope::GlobalScope;
14use crate::script_runtime::CanGc;
15
16#[dom_struct]
17pub(crate) struct GPUCommandBuffer {
18    reflector_: Reflector,
19    #[ignore_malloc_size_of = "defined in webgpu"]
20    #[no_trace]
21    channel: WebGPU,
22    label: DomRefCell<USVString>,
23    #[no_trace]
24    command_buffer: WebGPUCommandBuffer,
25}
26
27impl GPUCommandBuffer {
28    fn new_inherited(
29        channel: WebGPU,
30        command_buffer: WebGPUCommandBuffer,
31        label: USVString,
32    ) -> Self {
33        Self {
34            channel,
35            reflector_: Reflector::new(),
36            label: DomRefCell::new(label),
37            command_buffer,
38        }
39    }
40
41    pub(crate) fn new(
42        global: &GlobalScope,
43        channel: WebGPU,
44        command_buffer: WebGPUCommandBuffer,
45        label: USVString,
46        can_gc: CanGc,
47    ) -> DomRoot<Self> {
48        reflect_dom_object(
49            Box::new(GPUCommandBuffer::new_inherited(
50                channel,
51                command_buffer,
52                label,
53            )),
54            global,
55            can_gc,
56        )
57    }
58}
59
60impl Drop for GPUCommandBuffer {
61    fn drop(&mut self) {
62        if let Err(e) = self
63            .channel
64            .0
65            .send(WebGPURequest::DropCommandBuffer(self.command_buffer.0))
66        {
67            warn!(
68                "Failed to send DropCommandBuffer({:?}) ({})",
69                self.command_buffer.0, e
70            );
71        }
72    }
73}
74
75impl GPUCommandBuffer {
76    pub(crate) fn id(&self) -> WebGPUCommandBuffer {
77        self.command_buffer
78    }
79}
80
81impl GPUCommandBufferMethods<crate::DomTypeHolder> for GPUCommandBuffer {
82    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
83    fn Label(&self) -> USVString {
84        self.label.borrow().clone()
85    }
86
87    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
88    fn SetLabel(&self, value: USVString) {
89        *self.label.borrow_mut() = value;
90    }
91}