/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cell::Ref; use std::rc::Rc; use std::sync::Arc; use euclid::default::Size2D; use js::context::NoGC; use pixels::Snapshot; use script_bindings::DomTypes; use script_bindings::callback::{CallbackContainer, HasCallbackHolder, RootedCallback}; use script_bindings::error::{Error, Fallible}; use script_bindings::reflector::{DomGlobalGeneric, DomObject}; use script_webgpu::traits::{ EventTargetTrait, HtmlCanvasElementTrait, HtmlImageElementTrait, ImageBitmapTrait, ImageDataTrait, OffscreenCanvasTrait, OriginIsCleanTrait, WebGPUGlobalTrait, WebGPUHTMLVideoTrait, WebGPUPromiseCallbackTrait, }; use serde::Serialize; use serde::de::DeserializeOwned; use servo_base::generic_channel::GenericCallback; use servo_url::MutableOrigin; use crate::dom::GlobalScope; use crate::dom::bindings::reflector::DomGlobal; use crate::dom::promise::RootedPromise; use crate::dom::types::{ EventTarget, HTMLCanvasElement, HTMLImageElement, HTMLVideoElement, ImageBitmap, ImageData, OffscreenCanvas, }; use crate::routed_promise::{RoutedPromiseListener, callback_promise}; use crate::tasks::task::TaskOnce; pub(crate) mod gpu { #[expect(clippy::upper_case_acronyms)] pub(crate) type GPU = script_webgpu::gpu::GPU; } pub(crate) mod gpuadapter { pub(crate) type GPUAdapter = script_webgpu::gpuadapter::GPUAdapter; } pub(crate) mod gpuadapterinfo { pub(crate) type GPUAdapterInfo = script_webgpu::gpuadapterinfo::GPUAdapterInfo; } pub(crate) mod gpubindgroup { pub(crate) type GPUBindGroup = script_webgpu::gpubindgroup::GPUBindGroup; } pub(crate) mod gpubindgrouplayout { pub(crate) type GPUBindGroupLayout = script_webgpu::gpubindgrouplayout::GPUBindGroupLayout; } pub(crate) mod gpubuffer { pub(crate) type GPUBuffer = script_webgpu::gpubuffer::GPUBuffer; } pub(crate) mod gpubufferusage { pub(crate) type GPUBufferUsage = script_webgpu::gpubufferusage::GPUBufferUsage; } pub(crate) mod gpucanvascontext; pub(crate) mod gpucolorwrite { pub(crate) type GPUColorWrite = script_webgpu::gpucolorwrite::GPUColorWrite; } pub(crate) mod gpucommandbuffer { pub(crate) type GPUCommandBuffer = script_webgpu::gpucommandbuffer::GPUCommandBuffer; } pub(crate) mod gpucommandencoder { pub(crate) type GPUCommandEncoder = script_webgpu::gpucommandencoder::GPUCommandEncoder; } pub(crate) mod gpucompilationinfo { pub(crate) type GPUCompilationInfo = script_webgpu::gpucompilationinfo::GPUCompilationInfo; } pub(crate) mod gpucompilationmessage { pub(crate) type GPUCompilationMessage = script_webgpu::gpucompilationmessage::GPUCompilationMessage; } pub(crate) mod gpucomputepassencoder { pub(crate) type GPUComputePassEncoder = script_webgpu::gpucomputepassencoder::GPUComputePassEncoder; } pub(crate) mod gpucomputepipeline { pub(crate) type GPUComputePipeline = script_webgpu::gpucomputepipeline::GPUComputePipeline; } pub(crate) mod gpudevice { pub(crate) type GPUDevice = script_webgpu::gpudevice::GPUDevice; } pub(crate) mod gpudevicelostinfo { pub(crate) type GPUDeviceLostInfo = script_webgpu::gpudevicelostinfo::GPUDeviceLostInfo; } pub(crate) mod gpuerror { pub(crate) type GPUError = script_webgpu::gpuerror::GPUError; } pub(crate) mod gpuexternaltexture { pub(crate) type GPUExternalTexture = script_webgpu::gpuexternaltexture::GPUExternalTexture; pub(crate) type PlanarTexture = script_webgpu::gpuexternaltexture::PlanarTexture; } pub(crate) mod gpuinternalerror { pub(crate) type GPUInternalError = script_webgpu::gpuinternalerror::GPUInternalError; } pub(crate) mod gpumapmode { pub(crate) type GPUMapMode = script_webgpu::gpumapmode::GPUMapMode; } pub(crate) mod gpuoutofmemoryerror { pub(crate) type GPUOutOfMemoryError = script_webgpu::gpuoutofmemoryerror::GPUOutOfMemoryError; } pub(crate) mod gpupipelineerror { pub(crate) type GPUPipelineError = script_webgpu::gpupipelineerror::GPUPipelineError; } pub(crate) mod gpupipelinelayout { pub(crate) type GPUPipelineLayout = script_webgpu::gpupipelinelayout::GPUPipelineLayout; } pub(crate) mod gpuqueryset { pub(crate) type GPUQuerySet = script_webgpu::gpuqueryset::GPUQuerySet; } pub(crate) mod gpuqueue { pub(crate) type GPUQueue = script_webgpu::gpuqueue::GPUQueue; } pub(crate) mod gpurenderbundle { pub(crate) type GPURenderBundle = script_webgpu::gpurenderbundle::GPURenderBundle; } pub(crate) mod gpurenderbundleencoder { pub(crate) type GPURenderBundleEncoder = script_webgpu::gpurenderbundleencoder::GPURenderBundleEncoder; } pub(crate) mod gpurenderpassencoder { pub(crate) type GPURenderPassEncoder = script_webgpu::gpurenderpassencoder::GPURenderPassEncoder; } pub(crate) mod gpurenderpipeline { pub(crate) type GPURenderPipeline = script_webgpu::gpurenderpipeline::GPURenderPipeline; } pub(crate) mod gpusampler { pub(crate) type GPUSampler = script_webgpu::gpusampler::GPUSampler; } pub(crate) mod gpushadermodule { pub(crate) type GPUShaderModule = script_webgpu::gpushadermodule::GPUShaderModule; } pub(crate) mod gpushaderstage { pub(crate) type GPUShaderStage = script_webgpu::gpushaderstage::GPUShaderStage; } pub(crate) mod gpusupportedfeatures { pub(crate) type GPUSupportedFeatures = script_webgpu::gpusupportedfeatures::GPUSupportedFeatures; } pub(crate) mod gpusupportedlimits { pub(crate) type GPUSupportedLimits = script_webgpu::gpusupportedlimits::GPUSupportedLimits; } pub(crate) mod gputexture { pub(crate) type GPUTexture = script_webgpu::gputexture::GPUTexture; } pub(crate) mod gputextureusage { pub(crate) type GPUTextureUsage = script_webgpu::gputextureusage::GPUTextureUsage; } pub(crate) mod gputextureview { pub(crate) type GPUTextureView = script_webgpu::gputextureview::GPUTextureView; } pub(crate) mod gpuuncapturederrorevent { pub(crate) type GPUUncapturedErrorEvent = script_webgpu::gpuuncapturederrorevent::GPUUncapturedErrorEvent; } pub(crate) mod gpuvalidationerror { pub(crate) type GPUValidationError = script_webgpu::gpuvalidationerror::GPUValidationError; } pub(crate) mod identityhub { pub(crate) type IdentityHub = script_webgpu::identityhub::IdentityHub; } pub(crate) mod wgsllanguagefeatures { pub(crate) type WGSLLanguageFeatures = script_webgpu::wgsllanguagefeatures::WGSLLanguageFeatures; } impl WebGPUPromiseCallbackTrait for RootedPromise where S: DomObject + DomGlobalGeneric + RoutedPromiseListener, T: Serialize + 'static + Send + DeserializeOwned, { fn callback_promise_dom_manipulation_task_source(&self, d: &S) -> GenericCallback { let task_manager = ::global(d).task_manager(); callback_promise(self, d, task_manager.dom_manipulation_task_source()) } } impl WebGPUGlobalTrait for GlobalScope { fn global_wgpu_id_hub(&self) -> Arc { self.wgpu_id_hub() } fn queue_webgpu_task_source(&self, task: impl TaskOnce + 'static) { self.task_manager().webgpu_task_source().queue(task); } fn add_webgpu_device( &self, device: &script_webgpu::gpudevice::GPUDevice, ) { self.add_gpu_device(device) } } impl WebGPUHTMLVideoTrait for HTMLVideoElement { fn planar_video_for_webgpu( &self, device: &script_webgpu::gpudevice::GPUDevice, ) -> Fallible<( Size2D, Option>>, )> { HTMLVideoElement::planar_video_for_webgpu(self, device) } fn is_usable(&self) -> bool { HTMLVideoElement::is_usable(self) } fn get_current_frame_data(&self) -> Option { HTMLVideoElement::get_current_frame_data(self) } } impl EventTargetTrait for EventTarget { fn new_inherited() -> ::EventTarget { EventTarget::new_inherited() } fn get_event_handler_common( &self, cx: &mut js::context::JSContext, ty: &str, ) -> Option> { EventTarget::get_event_handler_common(self, cx, ty) } fn set_event_handler_common( &self, cx: &mut js::context::JSContext, ty: &str, listener: Option>, ) { EventTarget::set_event_handler_common(self, cx, ty, listener); } } impl OriginIsCleanTrait for HTMLVideoElement { fn origin_is_clean(&self) -> bool { HTMLVideoElement::origin_is_clean(self) } } impl OriginIsCleanTrait for ImageBitmap { fn origin_is_clean(&self) -> bool { ImageBitmap::origin_is_clean(self) } } impl ImageBitmapTrait for ImageBitmap { fn bitmap_data(&self) -> Ref<'_, Option> { ImageBitmap::bitmap_data(self) } } impl ImageDataTrait for ImageData { fn is_detached(&self, cx: &mut js::context::JSContext) -> bool { ImageData::is_detached(self, cx) } fn get_snapshot(&self, no_gc: &NoGC) -> Snapshot { ImageData::get_snapshot(self, no_gc) } } impl HtmlImageElementTrait for HTMLImageElement { fn is_usable(&self) -> Result { HTMLImageElement::is_usable(self) } fn get_raster_image_data(&self) -> Option { HTMLImageElement::get_raster_image_data(self) } fn same_origin(&self, origin: &MutableOrigin) -> bool { HTMLImageElement::same_origin(self, origin) } } impl OriginIsCleanTrait for OffscreenCanvas { fn origin_is_clean(&self) -> bool { OffscreenCanvas::origin_is_clean(self) } } impl OffscreenCanvasTrait for OffscreenCanvas { fn get_image_data(&self) -> Option { OffscreenCanvas::get_image_data(self) } } impl OriginIsCleanTrait for HTMLCanvasElement { fn origin_is_clean(&self) -> bool { HTMLCanvasElement::origin_is_clean(self) } } impl HtmlCanvasElementTrait for HTMLCanvasElement { fn is_valid(&self) -> bool { HTMLCanvasElement::is_valid(self) } fn get_image_data(&self) -> Option { HTMLCanvasElement::get_image_data(self) } }