Skip to main content

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