servo/components/script/dom/webgpu/gpu.rs
Martin Robinson 6031a12fd1
Move ScriptToConstellationMsg to constellation_traits (#36364)
This is the last big change necessary to create the
`constellation_traits` crate. This moves the data structure for messages
that originate from the `ScriptThread` and are sent to the
`Contellation` to `constellation_traits`, effectively splitting
`script_traits` in half. Before, `script_traits` was responsible for
exposing the API of both the `ScriptThread` and the `Constellation` to
the rest of Servo.

- Data structures that are used by `ScriptToConstellationMsg` are moved
  to `constellation_traits`. The dependency graph looks a bit like this:
  `script_layout_interface` depends on `script_traits` depends on
  `constellation_traits` depends on `embedder_traits`.
- Data structures that are used in the embedding layer
  (`UntrustedNodeAddress`, `CompositorHitTestResult`, `TouchEventResult`
  and `AnimationState`) are moved to embedder_traits, to avoid a
  dependency cycle between `webrender_traits` and
  `constellation_traits`.
- Types dealing with MessagePorts and serialization are moved to
  `constellation_traits::message_port`.

Testing: This is covered by existing tests as it just moves types
around.
Signed-off-by: Martin Robinson <mrobinson@igalia.com>

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-04-05 22:13:29 +00:00

133 lines
4.7 KiB
Rust

/* 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::rc::Rc;
use constellation_traits::ScriptToConstellationMessage;
use dom_struct::dom_struct;
use js::jsapi::Heap;
use webgpu_traits::WebGPUAdapterResponse;
use wgpu_types::PowerPreference;
use super::wgsllanguagefeatures::WGSLLanguageFeatures;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUMethods, GPUPowerPreference, GPURequestAdapterOptions, GPUTextureFormat,
};
use crate::dom::bindings::error::Error;
use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::dom::webgpu::gpuadapter::GPUAdapter;
use crate::realms::InRealm;
use crate::routed_promise::{RoutedPromiseListener, route_promise};
use crate::script_runtime::CanGc;
#[dom_struct]
#[allow(clippy::upper_case_acronyms)]
pub(crate) struct GPU {
reflector_: Reflector,
/// Same object for <https://www.w3.org/TR/webgpu/#dom-gpu-wgsllanguagefeatures>
wgsl_language_features: MutNullableDom<WGSLLanguageFeatures>,
}
impl GPU {
pub(crate) fn new_inherited() -> GPU {
GPU {
reflector_: Reflector::new(),
wgsl_language_features: MutNullableDom::default(),
}
}
pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<GPU> {
reflect_dom_object(Box::new(GPU::new_inherited()), global, can_gc)
}
}
impl GPUMethods<crate::DomTypeHolder> for GPU {
// https://gpuweb.github.io/gpuweb/#dom-gpu-requestadapter
fn RequestAdapter(
&self,
options: &GPURequestAdapterOptions,
comp: InRealm,
can_gc: CanGc,
) -> Rc<Promise> {
let global = &self.global();
let promise = Promise::new_in_current_realm(comp, can_gc);
let sender = route_promise(&promise, self);
let power_preference = match options.powerPreference {
Some(GPUPowerPreference::Low_power) => PowerPreference::LowPower,
Some(GPUPowerPreference::High_performance) => PowerPreference::HighPerformance,
None => PowerPreference::default(),
};
let ids = global.wgpu_id_hub().create_adapter_id();
let script_to_constellation_chan = global.script_to_constellation_chan();
if script_to_constellation_chan
.send(ScriptToConstellationMessage::RequestAdapter(
sender,
wgpu_core::instance::RequestAdapterOptions {
power_preference,
compatible_surface: None,
force_fallback_adapter: options.forceFallbackAdapter,
},
ids,
))
.is_err()
{
promise.reject_error(Error::Operation, can_gc);
}
promise
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpu-getpreferredcanvasformat>
fn GetPreferredCanvasFormat(&self) -> GPUTextureFormat {
// TODO: real implementation
GPUTextureFormat::Rgba8unorm
}
/// <https://www.w3.org/TR/webgpu/#dom-gpu-wgsllanguagefeatures>
fn WgslLanguageFeatures(&self, can_gc: CanGc) -> DomRoot<WGSLLanguageFeatures> {
self.wgsl_language_features
.or_init(|| WGSLLanguageFeatures::new(&self.global(), None, can_gc))
}
}
impl RoutedPromiseListener<WebGPUAdapterResponse> for GPU {
fn handle_response(
&self,
response: WebGPUAdapterResponse,
promise: &Rc<Promise>,
can_gc: CanGc,
) {
match response {
Some(Ok(adapter)) => {
let adapter = GPUAdapter::new(
&self.global(),
adapter.channel,
DOMString::from(format!(
"{} ({:?})",
adapter.adapter_info.name, adapter.adapter_id.0
)),
Heap::default(),
adapter.features,
adapter.limits,
adapter.adapter_info,
adapter.adapter_id,
can_gc,
);
promise.resolve_native(&adapter, can_gc);
},
Some(Err(e)) => {
warn!("Could not get GPUAdapter ({:?})", e);
promise.resolve_native(&None::<GPUAdapter>, can_gc);
},
None => {
warn!("Couldn't get a response, because WebGPU is disabled");
promise.resolve_native(&None::<GPUAdapter>, can_gc);
},
}
}
}