Skip to main content

script/dom/webgpu/
mod.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 std::cell::Ref;
6use std::rc::Rc;
7use std::sync::Arc;
8
9use euclid::default::Size2D;
10use js::context::NoGC;
11use pixels::Snapshot;
12use script_bindings::DomTypes;
13use script_bindings::callback::{CallbackContainer, RootedCallback};
14use script_bindings::error::{Error, Fallible};
15use script_bindings::reflector::{DomGlobalGeneric, DomObject};
16use script_webgpu::traits::{
17    EventTargetTrait, HtmlCanvasElementTrait, HtmlImageElementTrait, ImageBitmapTrait,
18    ImageDataTrait, OffscreenCanvasTrait, OriginIsCleanTrait, WebGPUGlobalTrait,
19    WebGPUHTMLVideoTrait, WebGPUPromiseCallbackTrait,
20};
21use serde::Serialize;
22use serde::de::DeserializeOwned;
23use servo_base::generic_channel::GenericCallback;
24use servo_url::MutableOrigin;
25
26use crate::dom::GlobalScope;
27use crate::dom::bindings::reflector::DomGlobal;
28use crate::dom::promise::RootedPromise;
29use crate::dom::types::{
30    EventTarget, HTMLCanvasElement, HTMLImageElement, HTMLVideoElement, ImageBitmap, ImageData,
31    OffscreenCanvas,
32};
33use crate::routed_promise::{RoutedPromiseListener, callback_promise};
34use crate::tasks::task::TaskOnce;
35
36pub(crate) mod gpu {
37    #[expect(clippy::upper_case_acronyms)]
38    pub(crate) type GPU = script_webgpu::gpu::GPU<crate::DomTypeHolder>;
39}
40pub(crate) mod gpuadapter {
41    pub(crate) type GPUAdapter = script_webgpu::gpuadapter::GPUAdapter<crate::DomTypeHolder>;
42}
43pub(crate) mod gpuadapterinfo {
44    pub(crate) type GPUAdapterInfo =
45        script_webgpu::gpuadapterinfo::GPUAdapterInfo<crate::DomTypeHolder>;
46}
47pub(crate) mod gpubindgroup {
48    pub(crate) type GPUBindGroup = script_webgpu::gpubindgroup::GPUBindGroup<crate::DomTypeHolder>;
49}
50pub(crate) mod gpubindgrouplayout {
51    pub(crate) type GPUBindGroupLayout =
52        script_webgpu::gpubindgrouplayout::GPUBindGroupLayout<crate::DomTypeHolder>;
53}
54pub(crate) mod gpubuffer {
55    pub(crate) type GPUBuffer = script_webgpu::gpubuffer::GPUBuffer<crate::DomTypeHolder>;
56}
57pub(crate) mod gpubufferusage {
58    pub(crate) type GPUBufferUsage =
59        script_webgpu::gpubufferusage::GPUBufferUsage<crate::DomTypeHolder>;
60}
61pub(crate) mod gpucanvascontext;
62pub(crate) mod gpucolorwrite {
63    pub(crate) type GPUColorWrite =
64        script_webgpu::gpucolorwrite::GPUColorWrite<crate::DomTypeHolder>;
65}
66pub(crate) mod gpucommandbuffer {
67    pub(crate) type GPUCommandBuffer =
68        script_webgpu::gpucommandbuffer::GPUCommandBuffer<crate::DomTypeHolder>;
69}
70pub(crate) mod gpucommandencoder {
71    pub(crate) type GPUCommandEncoder =
72        script_webgpu::gpucommandencoder::GPUCommandEncoder<crate::DomTypeHolder>;
73}
74pub(crate) mod gpucompilationinfo {
75    pub(crate) type GPUCompilationInfo =
76        script_webgpu::gpucompilationinfo::GPUCompilationInfo<crate::DomTypeHolder>;
77}
78pub(crate) mod gpucompilationmessage {
79    pub(crate) type GPUCompilationMessage =
80        script_webgpu::gpucompilationmessage::GPUCompilationMessage<crate::DomTypeHolder>;
81}
82pub(crate) mod gpucomputepassencoder {
83    pub(crate) type GPUComputePassEncoder =
84        script_webgpu::gpucomputepassencoder::GPUComputePassEncoder<crate::DomTypeHolder>;
85}
86pub(crate) mod gpucomputepipeline {
87    pub(crate) type GPUComputePipeline =
88        script_webgpu::gpucomputepipeline::GPUComputePipeline<crate::DomTypeHolder>;
89}
90pub(crate) mod gpudevice {
91    pub(crate) type GPUDevice = script_webgpu::gpudevice::GPUDevice<crate::DomTypeHolder>;
92}
93pub(crate) mod gpudevicelostinfo {
94    pub(crate) type GPUDeviceLostInfo =
95        script_webgpu::gpudevicelostinfo::GPUDeviceLostInfo<crate::DomTypeHolder>;
96}
97pub(crate) mod gpuerror {
98    pub(crate) type GPUError = script_webgpu::gpuerror::GPUError<crate::DomTypeHolder>;
99}
100pub(crate) mod gpuexternaltexture {
101    pub(crate) type GPUExternalTexture =
102        script_webgpu::gpuexternaltexture::GPUExternalTexture<crate::DomTypeHolder>;
103    pub(crate) type PlanarTexture =
104        script_webgpu::gpuexternaltexture::PlanarTexture<crate::DomTypeHolder>;
105}
106pub(crate) mod gpuinternalerror {
107    pub(crate) type GPUInternalError =
108        script_webgpu::gpuinternalerror::GPUInternalError<crate::DomTypeHolder>;
109}
110pub(crate) mod gpumapmode {
111    pub(crate) type GPUMapMode = script_webgpu::gpumapmode::GPUMapMode<crate::DomTypeHolder>;
112}
113pub(crate) mod gpuoutofmemoryerror {
114    pub(crate) type GPUOutOfMemoryError =
115        script_webgpu::gpuoutofmemoryerror::GPUOutOfMemoryError<crate::DomTypeHolder>;
116}
117pub(crate) mod gpupipelineerror {
118    pub(crate) type GPUPipelineError =
119        script_webgpu::gpupipelineerror::GPUPipelineError<crate::DomTypeHolder>;
120}
121pub(crate) mod gpupipelinelayout {
122    pub(crate) type GPUPipelineLayout =
123        script_webgpu::gpupipelinelayout::GPUPipelineLayout<crate::DomTypeHolder>;
124}
125pub(crate) mod gpuqueryset {
126    pub(crate) type GPUQuerySet = script_webgpu::gpuqueryset::GPUQuerySet<crate::DomTypeHolder>;
127}
128pub(crate) mod gpuqueue {
129    pub(crate) type GPUQueue = script_webgpu::gpuqueue::GPUQueue<crate::DomTypeHolder>;
130}
131pub(crate) mod gpurenderbundle {
132    pub(crate) type GPURenderBundle =
133        script_webgpu::gpurenderbundle::GPURenderBundle<crate::DomTypeHolder>;
134}
135pub(crate) mod gpurenderbundleencoder {
136    pub(crate) type GPURenderBundleEncoder =
137        script_webgpu::gpurenderbundleencoder::GPURenderBundleEncoder<crate::DomTypeHolder>;
138}
139pub(crate) mod gpurenderpassencoder {
140    pub(crate) type GPURenderPassEncoder =
141        script_webgpu::gpurenderpassencoder::GPURenderPassEncoder<crate::DomTypeHolder>;
142}
143pub(crate) mod gpurenderpipeline {
144    pub(crate) type GPURenderPipeline =
145        script_webgpu::gpurenderpipeline::GPURenderPipeline<crate::DomTypeHolder>;
146}
147pub(crate) mod gpusampler {
148    pub(crate) type GPUSampler = script_webgpu::gpusampler::GPUSampler<crate::DomTypeHolder>;
149}
150pub(crate) mod gpushadermodule {
151    pub(crate) type GPUShaderModule =
152        script_webgpu::gpushadermodule::GPUShaderModule<crate::DomTypeHolder>;
153}
154pub(crate) mod gpushaderstage {
155    pub(crate) type GPUShaderStage =
156        script_webgpu::gpushaderstage::GPUShaderStage<crate::DomTypeHolder>;
157}
158pub(crate) mod gpusupportedfeatures {
159    pub(crate) type GPUSupportedFeatures =
160        script_webgpu::gpusupportedfeatures::GPUSupportedFeatures<crate::DomTypeHolder>;
161}
162pub(crate) mod gpusupportedlimits {
163    pub(crate) type GPUSupportedLimits =
164        script_webgpu::gpusupportedlimits::GPUSupportedLimits<crate::DomTypeHolder>;
165}
166pub(crate) mod gputexture {
167    pub(crate) type GPUTexture = script_webgpu::gputexture::GPUTexture<crate::DomTypeHolder>;
168}
169pub(crate) mod gputextureusage {
170    pub(crate) type GPUTextureUsage =
171        script_webgpu::gputextureusage::GPUTextureUsage<crate::DomTypeHolder>;
172}
173pub(crate) mod gputextureview {
174    pub(crate) type GPUTextureView =
175        script_webgpu::gputextureview::GPUTextureView<crate::DomTypeHolder>;
176}
177pub(crate) mod gpuuncapturederrorevent {
178    pub(crate) type GPUUncapturedErrorEvent =
179        script_webgpu::gpuuncapturederrorevent::GPUUncapturedErrorEvent<crate::DomTypeHolder>;
180}
181pub(crate) mod gpuvalidationerror {
182    pub(crate) type GPUValidationError =
183        script_webgpu::gpuvalidationerror::GPUValidationError<crate::DomTypeHolder>;
184}
185pub(crate) mod identityhub {
186    pub(crate) type IdentityHub = script_webgpu::identityhub::IdentityHub;
187}
188pub(crate) mod wgsllanguagefeatures {
189    pub(crate) type WGSLLanguageFeatures =
190        script_webgpu::wgsllanguagefeatures::WGSLLanguageFeatures<crate::DomTypeHolder>;
191}
192
193impl<S, T> WebGPUPromiseCallbackTrait<crate::DomTypeHolder, S, T> for RootedPromise
194where
195    S: DomObject
196        + DomGlobalGeneric<crate::DomTypeHolder>
197        + RoutedPromiseListener<crate::DomTypeHolder, T>,
198    T: Serialize + 'static + Send + DeserializeOwned,
199{
200    fn callback_promise_dom_manipulation_task_source(&self, d: &S) -> GenericCallback<T> {
201        let task_manager = <S as DomGlobal>::global(d).task_manager();
202        callback_promise(self, d, task_manager.dom_manipulation_task_source())
203    }
204}
205
206impl WebGPUGlobalTrait<crate::DomTypeHolder> for GlobalScope {
207    fn global_wgpu_id_hub(&self) -> Arc<script_webgpu::identityhub::IdentityHub> {
208        self.wgpu_id_hub()
209    }
210
211    fn queue_webgpu_task_source(&self, task: impl TaskOnce + 'static) {
212        self.task_manager().webgpu_task_source().queue(task);
213    }
214
215    fn add_webgpu_device(
216        &self,
217        device: &script_webgpu::gpudevice::GPUDevice<crate::DomTypeHolder>,
218    ) {
219        self.add_gpu_device(device)
220    }
221}
222
223impl WebGPUHTMLVideoTrait<crate::DomTypeHolder> for HTMLVideoElement {
224    fn planar_video_for_webgpu(
225        &self,
226        device: &script_webgpu::gpudevice::GPUDevice<crate::DomTypeHolder>,
227    ) -> Fallible<(
228        Size2D<u32>,
229        Option<Rc<script_webgpu::gpuexternaltexture::PlanarTexture<crate::DomTypeHolder>>>,
230    )> {
231        HTMLVideoElement::planar_video_for_webgpu(self, device)
232    }
233
234    fn is_usable(&self) -> bool {
235        HTMLVideoElement::is_usable(self)
236    }
237
238    fn get_current_frame_data(&self) -> Option<pixels::Snapshot> {
239        HTMLVideoElement::get_current_frame_data(self)
240    }
241}
242
243impl EventTargetTrait<crate::DomTypeHolder> for EventTarget {
244    fn new_inherited() -> <crate::DomTypeHolder as DomTypes>::EventTarget {
245        EventTarget::new_inherited()
246    }
247
248    fn get_event_handler_common<T: CallbackContainer<crate::DomTypeHolder>>(
249        &self,
250        cx: &mut js::context::JSContext,
251        ty: &str,
252    ) -> Option<RootedCallback<T>> {
253        EventTarget::get_event_handler_common(self, cx, ty)
254    }
255
256    fn set_event_handler_common<T: CallbackContainer<crate::DomTypeHolder>>(
257        &self,
258        cx: &mut js::context::JSContext,
259        ty: &str,
260        listener: Option<RootedCallback<T>>,
261    ) {
262        EventTarget::set_event_handler_common(self, cx, ty, listener);
263    }
264}
265
266impl OriginIsCleanTrait for HTMLVideoElement {
267    fn origin_is_clean(&self) -> bool {
268        HTMLVideoElement::origin_is_clean(self)
269    }
270}
271
272impl OriginIsCleanTrait for ImageBitmap {
273    fn origin_is_clean(&self) -> bool {
274        ImageBitmap::origin_is_clean(self)
275    }
276}
277
278impl ImageBitmapTrait for ImageBitmap {
279    fn bitmap_data(&self) -> Ref<'_, Option<Snapshot>> {
280        ImageBitmap::bitmap_data(self)
281    }
282}
283
284impl ImageDataTrait for ImageData {
285    fn is_detached(&self, cx: &mut js::context::JSContext) -> bool {
286        ImageData::is_detached(self, cx)
287    }
288    fn get_snapshot(&self, no_gc: &NoGC) -> Snapshot {
289        ImageData::get_snapshot(self, no_gc)
290    }
291}
292
293impl HtmlImageElementTrait for HTMLImageElement {
294    fn is_usable(&self) -> Result<bool, Error> {
295        HTMLImageElement::is_usable(self)
296    }
297    fn get_raster_image_data(&self) -> Option<Snapshot> {
298        HTMLImageElement::get_raster_image_data(self)
299    }
300    fn same_origin(&self, origin: &MutableOrigin) -> bool {
301        HTMLImageElement::same_origin(self, origin)
302    }
303}
304
305impl OriginIsCleanTrait for OffscreenCanvas {
306    fn origin_is_clean(&self) -> bool {
307        OffscreenCanvas::origin_is_clean(self)
308    }
309}
310
311impl OffscreenCanvasTrait for OffscreenCanvas {
312    fn get_image_data(&self) -> Option<Snapshot> {
313        OffscreenCanvas::get_image_data(self)
314    }
315}
316
317impl OriginIsCleanTrait for HTMLCanvasElement {
318    fn origin_is_clean(&self) -> bool {
319        HTMLCanvasElement::origin_is_clean(self)
320    }
321}
322
323impl HtmlCanvasElementTrait for HTMLCanvasElement {
324    fn is_valid(&self) -> bool {
325        HTMLCanvasElement::is_valid(self)
326    }
327    fn get_image_data(&self) -> Option<Snapshot> {
328        HTMLCanvasElement::get_image_data(self)
329    }
330}