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