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;
22use crate::traits::Equivalence;
23
24#[derive(JSTraceable, MallocSizeOf)]
25struct DroppableGPUCommandBuffer {
26 #[no_trace]
27 channel: WebGPU,
28 #[no_trace]
29 command_buffer: WebGPUCommandBuffer,
30}
31
32impl Drop for DroppableGPUCommandBuffer {
33 fn drop(&mut self) {
34 if let Err(e) = self
35 .channel
36 .0
37 .send(WebGPURequest::DropCommandBuffer(self.command_buffer.0))
38 {
39 warn!(
40 "Failed to send DropCommandBuffer({:?}) ({})",
41 self.command_buffer.0, e
42 );
43 }
44 }
45}
46
47#[dom_struct]
48pub struct GPUCommandBuffer<D: DomTypes> {
49 reflector_: Reflector,
50 label: DomRefCell<USVString>,
51 droppable: DroppableGPUCommandBuffer,
52 #[no_trace = "PhantomData does not exist"]
53 phantom: PhantomData<D>,
54}
55
56impl<D: Equivalence> GPUCommandBuffer<D> {
57 fn new_inherited(
58 channel: WebGPU,
59 command_buffer: WebGPUCommandBuffer,
60 label: USVString,
61 ) -> Self {
62 Self {
63 reflector_: Reflector::new(),
64 label: DomRefCell::new(label),
65 droppable: DroppableGPUCommandBuffer {
66 channel,
67 command_buffer,
68 },
69 phantom: PhantomData,
70 }
71 }
72
73 pub fn new(
74 cx: &mut JSContext,
75 global: &D::GlobalScope,
76 channel: WebGPU,
77 command_buffer: WebGPUCommandBuffer,
78 label: USVString,
79 ) -> DomRoot<Self> {
80 reflect_dom_object_with_wrap::<D, _, _>(
81 Box::new(GPUCommandBuffer::new_inherited(
82 channel,
83 command_buffer,
84 label,
85 )),
86 global,
87 cx,
88 GPUCommandBufferWrap::<D>,
89 )
90 }
91}
92
93impl<D: DomTypes> GPUCommandBuffer<D> {
94 pub fn id(&self) -> WebGPUCommandBuffer {
95 self.droppable.command_buffer
96 }
97}
98
99impl<D: DomTypes> GPUCommandBufferMethods<D> for GPUCommandBuffer<D> {
100 fn Label(&self) -> USVString {
102 self.label.borrow().clone()
103 }
104
105 fn SetLabel(&self, no_gc: &NoGC, value: USVString) {
107 *self.label.safe_borrow_mut(no_gc) = value;
108 }
109}