Skip to main content

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