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