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