Skip to main content

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