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