webgpu: Add a webgpu_traits crate (#36320)

This breaks the `script_traits` dependency  on `webgpu`. In general, the
`traits` crates shouldn't depend on Servo non-`traits` crates. This is
necessary to move "script to constellation" messages to the
`constellation_traits` crate, making it the entire API for talking to
the
constellation. This will break a circular dependency when that happens.

Testing: Successfully building is enough of a test for this one as
it is mainly moving types around.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-04-04 10:06:07 +02:00 committed by GitHub
parent df9efde1c3
commit 0d693114ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
52 changed files with 640 additions and 568 deletions

View file

@ -133,6 +133,7 @@ utf-8 = "0.7"
uuid = { workspace = true, features = ["serde"] }
webdriver = { workspace = true }
webgpu = { path = "../webgpu" }
webgpu_traits = { workspace = true }
webrender_api = { workspace = true }
webrender_traits = { workspace = true }
webxr-api = { workspace = true, features = ["ipc"], optional = true }

View file

@ -72,7 +72,7 @@ use stylo_atoms::Atom;
use url::Host;
use uuid::Uuid;
#[cfg(feature = "webgpu")]
use webgpu::swapchain::WebGPUContextId;
use webgpu_traits::WebGPUContextId;
use webrender_api::units::DeviceIntRect;
use crate::animation_timeline::AnimationTimeline;

View file

@ -64,7 +64,7 @@ use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use timers::{TimerEventId, TimerEventRequest, TimerSource};
use uuid::Uuid;
#[cfg(feature = "webgpu")]
use webgpu::{DeviceLostReason, WebGPUDevice};
use webgpu_traits::{DeviceLostReason, WebGPUDevice};
use super::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
#[cfg(feature = "webgpu")]
@ -3006,7 +3006,7 @@ impl GlobalScope {
pub(crate) fn handle_uncaptured_gpu_error(
&self,
device: WebGPUDevice,
error: webgpu::Error,
error: webgpu_traits::Error,
can_gc: CanGc,
) {
if let Some(gpu_device) = self

View file

@ -7,8 +7,9 @@ use std::rc::Rc;
use dom_struct::dom_struct;
use js::jsapi::Heap;
use script_traits::ScriptMsg;
use webgpu::wgc;
use webgpu::wgt::PowerPreference;
use webgpu::{WebGPUAdapterResponse, wgc};
use webgpu_traits::WebGPUAdapterResponse;
use super::wgsllanguagefeatures::WGSLLanguageFeatures;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{

View file

@ -7,8 +7,8 @@ use std::rc::Rc;
use dom_struct::dom_struct;
use js::jsapi::{Heap, JSObject};
use webgpu::wgc::instance::RequestDeviceError;
use webgpu::wgt::MemoryHints;
use webgpu::{WebGPU, WebGPUAdapter, WebGPUDeviceResponse, WebGPURequest, wgt};
use webgpu::wgt::{self, MemoryHints};
use webgpu_traits::{WebGPU, WebGPUAdapter, WebGPUDeviceResponse, WebGPURequest};
use super::gpusupportedfeatures::GPUSupportedFeatures;
use super::gpusupportedlimits::set_limit;

View file

@ -6,7 +6,7 @@ use std::borrow::Cow;
use dom_struct::dom_struct;
use webgpu::wgc::binding_model::BindGroupDescriptor;
use webgpu::{WebGPU, WebGPUBindGroup, WebGPUDevice, WebGPURequest};
use webgpu_traits::{WebGPU, WebGPUBindGroup, WebGPUDevice, WebGPURequest};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;

View file

@ -6,7 +6,7 @@ use std::borrow::Cow;
use dom_struct::dom_struct;
use webgpu::wgc::binding_model::BindGroupLayoutDescriptor;
use webgpu::{WebGPU, WebGPUBindGroupLayout, WebGPURequest};
use webgpu_traits::{WebGPU, WebGPUBindGroupLayout, WebGPURequest};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;

View file

@ -10,7 +10,8 @@ use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSharedMemory;
use js::typedarray::ArrayBuffer;
use webgpu::wgc::device::HostMap;
use webgpu::{Mapping, WebGPU, WebGPUBuffer, WebGPURequest, wgc, wgt};
use webgpu::{wgc, wgt};
use webgpu_traits::{Mapping, WebGPU, WebGPUBuffer, WebGPURequest};
use wgc::resource::BufferAccessError;
use crate::conversions::Convert;
@ -263,7 +264,7 @@ impl GPUBufferMethods<crate::DomTypeHolder> for GPUBuffer {
GPUMapModeConstants::WRITE => HostMap::Write,
_ => {
self.device
.dispatch_error(webgpu::Error::Validation(String::from(
.dispatch_error(webgpu_traits::Error::Validation(String::from(
"Invalid MapModeFlags",
)));
self.map_failure(&promise, can_gc);

View file

@ -9,10 +9,10 @@ use arrayvec::ArrayVec;
use dom_struct::dom_struct;
use ipc_channel::ipc::{self, IpcSharedMemory};
use script_layout_interface::HTMLCanvasDataSource;
use webgpu::swapchain::WebGPUContextId;
use webgpu::wgc::id;
use webgpu::{
ContextConfiguration, PRESENTATION_BUFFER_COUNT, WebGPU, WebGPURequest, WebGPUTexture,
use webgpu_traits::{
ContextConfiguration, PRESENTATION_BUFFER_COUNT, WebGPU, WebGPUContextId, WebGPURequest,
WebGPUTexture,
};
use webrender_api::ImageKey;
use webrender_api::units::DeviceIntSize;

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use webgpu::{WebGPU, WebGPUCommandBuffer, WebGPURequest};
use webgpu_traits::{WebGPU, WebGPUCommandBuffer, WebGPURequest};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUCommandBufferMethods;

View file

@ -4,9 +4,10 @@
use dom_struct::dom_struct;
use webgpu::wgc::command as wgpu_com;
use webgpu::{
use webgpu::wgt;
use webgpu_traits::{
WebGPU, WebGPUCommandBuffer, WebGPUCommandEncoder, WebGPUComputePass, WebGPUDevice,
WebGPURenderPass, WebGPURequest, wgt,
WebGPURenderPass, WebGPURequest,
};
use crate::conversions::{Convert, TryConvert};

View file

@ -4,7 +4,7 @@
use dom_struct::dom_struct;
use js::rust::MutableHandleValue;
use webgpu::ShaderCompilationInfo;
use webgpu_traits::ShaderCompilationInfo;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUCompilationInfoMethods;
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};

View file

@ -5,7 +5,7 @@
#![allow(dead_code)] // this file is stub as wgpu does not provide info
use dom_struct::dom_struct;
use webgpu::ShaderCompilationInfo;
use webgpu_traits::ShaderCompilationInfo;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUCompilationMessageMethods, GPUCompilationMessageType,

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use webgpu::{WebGPU, WebGPUComputePass, WebGPURequest};
use webgpu_traits::{WebGPU, WebGPUComputePass, WebGPURequest};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUComputePassEncoderMethods;

View file

@ -5,7 +5,7 @@
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender;
use webgpu::wgc::pipeline::ComputePipelineDescriptor;
use webgpu::{
use webgpu_traits::{
WebGPU, WebGPUBindGroupLayout, WebGPUComputePipeline, WebGPUComputePipelineResponse,
WebGPURequest,
};

View file

@ -509,7 +509,7 @@ impl<'a> Convert<Option<Cow<'a, str>>> for &GPUObjectDescriptorBase {
pub(crate) fn convert_bind_group_layout_entry(
bgle: &GPUBindGroupLayoutEntry,
device: &GPUDevice,
) -> Fallible<Result<wgt::BindGroupLayoutEntry, webgpu::Error>> {
) -> Fallible<Result<wgt::BindGroupLayoutEntry, webgpu_traits::Error>> {
let number_of_provided_bindings = bgle.buffer.is_some() as u8 +
bgle.sampler.is_some() as u8 +
bgle.storageTexture.is_some() as u8 +
@ -569,7 +569,7 @@ pub(crate) fn convert_bind_group_layout_entry(
} else {
ty
}
.ok_or(webgpu::Error::Validation(
.ok_or(webgpu_traits::Error::Validation(
"Exactly on entry type must be provided".to_string(),
));

View file

@ -13,11 +13,11 @@ use js::jsapi::{Heap, JSObject};
use webgpu::wgc::id::{BindGroupLayoutId, PipelineLayoutId};
use webgpu::wgc::pipeline as wgpu_pipe;
use webgpu::wgc::pipeline::RenderPipelineDescriptor;
use webgpu::wgt::TextureFormat;
use webgpu::{
PopError, WebGPU, WebGPUComputePipeline, WebGPUComputePipelineResponse,
WebGPUPoppedErrorScopeResponse, WebGPURenderPipeline, WebGPURenderPipelineResponse,
WebGPURequest, wgt,
use webgpu::wgt::{self, TextureFormat};
use webgpu_traits::{
PopError, WebGPU, WebGPUComputePipeline, WebGPUComputePipelineResponse, WebGPUDevice,
WebGPUPoppedErrorScopeResponse, WebGPUQueue, WebGPURenderPipeline,
WebGPURenderPipelineResponse, WebGPURequest,
};
use super::gpudevicelostinfo::GPUDeviceLostInfo;
@ -78,7 +78,7 @@ pub(crate) struct GPUDevice {
limits: Dom<GPUSupportedLimits>,
label: DomRefCell<USVString>,
#[no_trace]
device: webgpu::WebGPUDevice,
device: WebGPUDevice,
default_queue: Dom<GPUQueue>,
/// <https://gpuweb.github.io/gpuweb/#dom-gpudevice-lost>
#[ignore_malloc_size_of = "promises are hard"]
@ -117,7 +117,7 @@ impl GPUDevice {
extensions: Heap<*mut JSObject>,
features: &GPUSupportedFeatures,
limits: &GPUSupportedLimits,
device: webgpu::WebGPUDevice,
device: WebGPUDevice,
queue: &GPUQueue,
label: String,
lost_promise: Rc<Promise>,
@ -145,8 +145,8 @@ impl GPUDevice {
extensions: Heap<*mut JSObject>,
features: wgt::Features,
limits: wgt::Limits,
device: webgpu::WebGPUDevice,
queue: webgpu::WebGPUQueue,
device: WebGPUDevice,
queue: WebGPUQueue,
label: String,
can_gc: CanGc,
) -> DomRoot<Self> {
@ -175,11 +175,11 @@ impl GPUDevice {
}
impl GPUDevice {
pub(crate) fn id(&self) -> webgpu::WebGPUDevice {
pub(crate) fn id(&self) -> WebGPUDevice {
self.device
}
pub(crate) fn queue_id(&self) -> webgpu::WebGPUQueue {
pub(crate) fn queue_id(&self) -> WebGPUQueue {
self.default_queue.id()
}
@ -187,7 +187,7 @@ impl GPUDevice {
self.channel.clone()
}
pub(crate) fn dispatch_error(&self, error: webgpu::Error) {
pub(crate) fn dispatch_error(&self, error: webgpu_traits::Error) {
if let Err(e) = self.channel.0.send(WebGPURequest::DispatchError {
device_id: self.device.0,
error,
@ -196,7 +196,7 @@ impl GPUDevice {
}
}
pub(crate) fn fire_uncaptured_error(&self, error: webgpu::Error, can_gc: CanGc) {
pub(crate) fn fire_uncaptured_error(&self, error: webgpu_traits::Error, can_gc: CanGc) {
let error = GPUError::from_error(&self.global(), error, can_gc);
let ev = GPUUncapturedErrorEvent::new(
&self.global(),
@ -620,7 +620,7 @@ impl RoutedPromiseListener<WebGPUComputePipelineResponse> for GPUDevice {
),
can_gc,
),
Err(webgpu::Error::Validation(msg)) => promise.reject_native(
Err(webgpu_traits::Error::Validation(msg)) => promise.reject_native(
&GPUPipelineError::new(
&self.global(),
msg.into(),
@ -629,8 +629,8 @@ impl RoutedPromiseListener<WebGPUComputePipelineResponse> for GPUDevice {
),
can_gc,
),
Err(webgpu::Error::OutOfMemory(msg) | webgpu::Error::Internal(msg)) => promise
.reject_native(
Err(webgpu_traits::Error::OutOfMemory(msg) | webgpu_traits::Error::Internal(msg)) => {
promise.reject_native(
&GPUPipelineError::new(
&self.global(),
msg.into(),
@ -638,7 +638,8 @@ impl RoutedPromiseListener<WebGPUComputePipelineResponse> for GPUDevice {
can_gc,
),
can_gc,
),
)
},
}
}
}
@ -661,7 +662,7 @@ impl RoutedPromiseListener<WebGPURenderPipelineResponse> for GPUDevice {
),
can_gc,
),
Err(webgpu::Error::Validation(msg)) => promise.reject_native(
Err(webgpu_traits::Error::Validation(msg)) => promise.reject_native(
&GPUPipelineError::new(
&self.global(),
msg.into(),
@ -670,8 +671,8 @@ impl RoutedPromiseListener<WebGPURenderPipelineResponse> for GPUDevice {
),
can_gc,
),
Err(webgpu::Error::OutOfMemory(msg) | webgpu::Error::Internal(msg)) => promise
.reject_native(
Err(webgpu_traits::Error::OutOfMemory(msg) | webgpu_traits::Error::Internal(msg)) => {
promise.reject_native(
&GPUPipelineError::new(
&self.global(),
msg.into(),
@ -679,7 +680,8 @@ impl RoutedPromiseListener<WebGPURenderPipelineResponse> for GPUDevice {
can_gc,
),
can_gc,
),
)
},
}
}
}

View file

@ -4,7 +4,7 @@
use dom_struct::dom_struct;
use js::rust::HandleObject;
use webgpu::{Error, ErrorFilter};
use webgpu_traits::{Error, ErrorFilter};
use crate::conversions::Convert;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{GPUErrorFilter, GPUErrorMethods};

View file

@ -6,7 +6,7 @@ use std::borrow::Cow;
use dom_struct::dom_struct;
use webgpu::wgc::binding_model::PipelineLayoutDescriptor;
use webgpu::{WebGPU, WebGPUBindGroupLayout, WebGPUPipelineLayout, WebGPURequest};
use webgpu_traits::{WebGPU, WebGPUBindGroupLayout, WebGPUPipelineLayout, WebGPURequest};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;

View file

@ -6,7 +6,8 @@ use std::rc::Rc;
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSharedMemory;
use webgpu::{WebGPU, WebGPUQueue, WebGPURequest, wgt};
use webgpu::wgt;
use webgpu_traits::{WebGPU, WebGPUQueue, WebGPURequest};
use crate::conversions::{Convert, TryConvert};
use crate::dom::bindings::cell::DomRefCell;

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use webgpu::{WebGPU, WebGPUDevice, WebGPURenderBundle, WebGPURequest};
use webgpu_traits::{WebGPU, WebGPUDevice, WebGPURenderBundle, WebGPURequest};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPURenderBundleMethods;

View file

@ -8,7 +8,8 @@ use dom_struct::dom_struct;
use webgpu::wgc::command::{
RenderBundleEncoder, RenderBundleEncoderDescriptor, bundle_ffi as wgpu_bundle,
};
use webgpu::{WebGPU, WebGPURenderBundle, WebGPURequest, wgt};
use webgpu::wgt;
use webgpu_traits::{WebGPU, WebGPURenderBundle, WebGPURequest};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;

View file

@ -3,7 +3,8 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use webgpu::{RenderCommand, WebGPU, WebGPURenderPass, WebGPURequest, wgt};
use webgpu::wgt;
use webgpu_traits::{RenderCommand, WebGPU, WebGPURenderPass, WebGPURequest};
use crate::conversions::TryConvert;
use crate::dom::bindings::cell::DomRefCell;

View file

@ -5,7 +5,7 @@
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender;
use webgpu::wgc::pipeline::RenderPipelineDescriptor;
use webgpu::{
use webgpu_traits::{
WebGPU, WebGPUBindGroupLayout, WebGPURenderPipeline, WebGPURenderPipelineResponse,
WebGPURequest,
};

View file

@ -4,7 +4,7 @@
use dom_struct::dom_struct;
use webgpu::wgc::resource::SamplerDescriptor;
use webgpu::{WebGPU, WebGPUDevice, WebGPURequest, WebGPUSampler};
use webgpu_traits::{WebGPU, WebGPUDevice, WebGPURequest, WebGPUSampler};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;

View file

@ -5,7 +5,7 @@
use std::rc::Rc;
use dom_struct::dom_struct;
use webgpu::{ShaderCompilationInfo, WebGPU, WebGPURequest, WebGPUShaderModule};
use webgpu_traits::{ShaderCompilationInfo, WebGPU, WebGPURequest, WebGPUShaderModule};
use super::gpucompilationinfo::GPUCompilationInfo;
use crate::dom::bindings::cell::DomRefCell;

View file

@ -6,7 +6,8 @@ use std::string::String;
use dom_struct::dom_struct;
use webgpu::wgc::resource;
use webgpu::{WebGPU, WebGPURequest, WebGPUTexture, WebGPUTextureView, wgt};
use webgpu::wgt;
use webgpu_traits::{WebGPU, WebGPURequest, WebGPUTexture, WebGPUTextureView};
use super::gpuconvert::convert_texture_descriptor;
use crate::conversions::Convert;
@ -207,7 +208,7 @@ impl GPUTextureMethods<crate::DomTypeHolder> for GPUTexture {
})
} else {
self.device
.dispatch_error(webgpu::Error::Validation(String::from(
.dispatch_error(webgpu_traits::Error::Validation(String::from(
"arrayLayerCount and mipLevelCount cannot be 0",
)));
None

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use webgpu::{WebGPU, WebGPURequest, WebGPUTextureView};
use webgpu_traits::{WebGPU, WebGPURequest, WebGPUTextureView};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUTextureViewMethods;

View file

@ -2,7 +2,6 @@
* 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 webgpu::identity::{ComputePass, ComputePassId, RenderPass, RenderPassId};
use webgpu::wgc::id::markers::{
Adapter, BindGroup, BindGroupLayout, Buffer, CommandEncoder, ComputePipeline, Device,
PipelineLayout, Queue, RenderBundle, RenderPipeline, Sampler, ShaderModule, Texture,
@ -14,6 +13,7 @@ use webgpu::wgc::id::{
ShaderModuleId, TextureId, TextureViewId,
};
use webgpu::wgc::identity::IdentityManager;
use webgpu_traits::{ComputePass, ComputePassId, RenderPass, RenderPassId};
#[derive(Debug)]
pub(crate) struct IdentityHub {

View file

@ -22,7 +22,7 @@ use script_traits::{Painter, ScriptMsg, ScriptThreadMessage};
use stylo_atoms::Atom;
use timers::TimerScheduler;
#[cfg(feature = "webgpu")]
use webgpu::WebGPUMsg;
use webgpu_traits::WebGPUMsg;
use crate::dom::abstractworker::WorkerScriptMsg;
use crate::dom::bindings::trace::CustomTraceable;

View file

@ -91,7 +91,7 @@ use stylo_atoms::Atom;
use timers::{TimerEventRequest, TimerScheduler};
use url::Position;
#[cfg(feature = "webgpu")]
use webgpu::{WebGPUDevice, WebGPUMsg};
use webgpu_traits::{WebGPUDevice, WebGPUMsg};
use webrender_api::DocumentId;
use webrender_traits::CrossProcessCompositorApi;