script_webgpu/
gpucommandbuffer.rs1use std::marker::PhantomData;
6
7use dom_struct::dom_struct;
8use js::context::{JSContext, NoGC};
9use jstraceable_derive::JSTraceable;
10use log::warn;
11use malloc_size_of_derive::MallocSizeOf;
12use script_bindings::DomTypes;
13use script_bindings::cell::DomRefCell;
14use script_bindings::codegen::GenericBindings::WebGPUBinding::{
15 GPUCommandBufferMethods, GPUCommandBufferWrap,
16};
17use script_bindings::reflector::{Reflector, reflect_dom_object_with_wrap};
18use webgpu_traits::{WebGPU, WebGPUCommandBuffer, WebGPURequest};
19
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::bindings::str::USVString;
22
23#[derive(JSTraceable, MallocSizeOf)]
24struct DroppableGPUCommandBuffer {
25 #[no_trace]
26 channel: WebGPU,
27 #[no_trace]
28 command_buffer: WebGPUCommandBuffer,
29}
30
31impl Drop for DroppableGPUCommandBuffer {
32 fn drop(&mut self) {
33 if let Err(e) = self
34 .channel
35 .0
36 .send(WebGPURequest::DropCommandBuffer(self.command_buffer.0))
37 {
38 warn!(
39 "Failed to send DropCommandBuffer({:?}) ({})",
40 self.command_buffer.0, e
41 );
42 }
43 }
44}
45
46#[dom_struct]
47pub struct GPUCommandBuffer<D: DomTypes> {
48 reflector_: Reflector,
49 label: DomRefCell<USVString>,
50 droppable: DroppableGPUCommandBuffer,
51 #[no_trace = "PhantomData does not exist"]
52 phantom: PhantomData<D>,
53}
54
55impl<D> GPUCommandBuffer<D>
56where
57 D: DomTypes<GPUCommandBuffer = GPUCommandBuffer<D>>,
58{
59 fn new_inherited(
60 channel: WebGPU,
61 command_buffer: WebGPUCommandBuffer,
62 label: USVString,
63 ) -> Self {
64 Self {
65 reflector_: Reflector::new(),
66 label: DomRefCell::new(label),
67 droppable: DroppableGPUCommandBuffer {
68 channel,
69 command_buffer,
70 },
71 phantom: PhantomData,
72 }
73 }
74
75 pub fn new(
76 cx: &mut JSContext,
77 global: &D::GlobalScope,
78 channel: WebGPU,
79 command_buffer: WebGPUCommandBuffer,
80 label: USVString,
81 ) -> DomRoot<Self> {
82 reflect_dom_object_with_wrap::<D, _, _>(
83 Box::new(GPUCommandBuffer::new_inherited(
84 channel,
85 command_buffer,
86 label,
87 )),
88 global,
89 cx,
90 GPUCommandBufferWrap::<D>,
91 )
92 }
93}
94
95impl<D: DomTypes> GPUCommandBuffer<D> {
96 pub fn id(&self) -> WebGPUCommandBuffer {
97 self.droppable.command_buffer
98 }
99}
100
101impl<D: DomTypes> GPUCommandBufferMethods<D> for GPUCommandBuffer<D> {
102 fn Label(&self) -> USVString {
104 self.label.borrow().clone()
105 }
106
107 fn SetLabel(&self, no_gc: &NoGC, value: USVString) {
109 *self.label.safe_borrow_mut(no_gc) = value;
110 }
111}