mirror of
https://github.com/servo/servo.git
synced 2025-08-01 19:50:30 +01:00
Only start WebGPU thread if an adapter is requested
This commit is contained in:
parent
f8c957dc1b
commit
a751b1c3d7
15 changed files with 200 additions and 147 deletions
|
@ -521,6 +521,7 @@ unsafe_no_jsmanaged_fields!(WebGLTextureId);
|
|||
unsafe_no_jsmanaged_fields!(WebGLVertexArrayId);
|
||||
unsafe_no_jsmanaged_fields!(WebGLVersion);
|
||||
unsafe_no_jsmanaged_fields!(WebGLSLVersion);
|
||||
unsafe_no_jsmanaged_fields!(RefCell<Option<WebGPU>>);
|
||||
unsafe_no_jsmanaged_fields!(RefCell<Identities>);
|
||||
unsafe_no_jsmanaged_fields!(WebGPU);
|
||||
unsafe_no_jsmanaged_fields!(WebGPUAdapter);
|
||||
|
|
|
@ -19,9 +19,10 @@ use dom_struct::dom_struct;
|
|||
use ipc_channel::ipc::{self, IpcSender};
|
||||
use ipc_channel::router::ROUTER;
|
||||
use js::jsapi::Heap;
|
||||
use script_traits::ScriptMsg;
|
||||
use std::rc::Rc;
|
||||
use webgpu::wgpu;
|
||||
use webgpu::{WebGPU, WebGPURequest, WebGPUResponse, WebGPUResponseResult};
|
||||
use webgpu::{WebGPUResponse, WebGPUResponseResult};
|
||||
|
||||
#[dom_struct]
|
||||
pub struct GPU {
|
||||
|
@ -38,10 +39,6 @@ impl GPU {
|
|||
pub fn new(global: &GlobalScope) -> DomRoot<GPU> {
|
||||
reflect_dom_object(Box::new(GPU::new_inherited()), global, GPUBinding::Wrap)
|
||||
}
|
||||
|
||||
fn wgpu_channel(&self) -> Option<WebGPU> {
|
||||
self.global().as_window().webgpu_channel()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AsyncWGPUListener {
|
||||
|
@ -114,7 +111,8 @@ impl GPUMethods for GPU {
|
|||
options: &GPURequestAdapterOptions,
|
||||
comp: InCompartment,
|
||||
) -> Rc<Promise> {
|
||||
let promise = Promise::new_in_current_compartment(&self.global(), comp);
|
||||
let global = &self.global();
|
||||
let promise = Promise::new_in_current_compartment(global, comp);
|
||||
let sender = response_async(&promise, self);
|
||||
let power_preference = match options.powerPreference {
|
||||
Some(GPUPowerPreference::Low_power) => wgpu::instance::PowerPreference::LowPower,
|
||||
|
@ -123,21 +121,21 @@ impl GPUMethods for GPU {
|
|||
},
|
||||
None => wgpu::instance::PowerPreference::Default,
|
||||
};
|
||||
let ids = self.global().as_window().Navigator().create_adapter_ids();
|
||||
let ids = global.as_window().Navigator().create_adapter_ids();
|
||||
|
||||
match self.wgpu_channel() {
|
||||
Some(channel) => {
|
||||
channel
|
||||
.0
|
||||
.send(WebGPURequest::RequestAdapter(
|
||||
sender,
|
||||
wgpu::instance::RequestAdapterOptions { power_preference },
|
||||
ids,
|
||||
))
|
||||
.unwrap();
|
||||
},
|
||||
None => promise.reject_error(Error::Type("No WebGPU thread...".to_owned())),
|
||||
};
|
||||
let script_to_constellation_chan = global.script_to_constellation_chan();
|
||||
if script_to_constellation_chan
|
||||
.send(ScriptMsg::RequestAdapter(
|
||||
sender,
|
||||
wgpu::instance::RequestAdapterOptions { power_preference },
|
||||
ids,
|
||||
))
|
||||
.is_err()
|
||||
{
|
||||
promise.reject_error(Error::Type(
|
||||
"Failed to send adapter request to constellation...".to_owned(),
|
||||
));
|
||||
}
|
||||
promise
|
||||
}
|
||||
}
|
||||
|
@ -145,9 +143,10 @@ impl GPUMethods for GPU {
|
|||
impl AsyncWGPUListener for GPU {
|
||||
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) {
|
||||
match response {
|
||||
WebGPUResponse::RequestAdapter(name, adapter) => {
|
||||
WebGPUResponse::RequestAdapter(name, adapter, channel) => {
|
||||
let adapter = GPUAdapter::new(
|
||||
&self.global(),
|
||||
channel,
|
||||
DOMString::from(format!("{} ({:?})", name, adapter.0.backend())),
|
||||
Heap::default(),
|
||||
adapter,
|
||||
|
|
|
@ -23,11 +23,13 @@ use dom_struct::dom_struct;
|
|||
use js::jsapi::{Heap, JSObject};
|
||||
use std::ptr::NonNull;
|
||||
use std::rc::Rc;
|
||||
use webgpu::{wgpu, WebGPUAdapter, WebGPURequest, WebGPUResponse};
|
||||
use webgpu::{wgpu, WebGPU, WebGPUAdapter, WebGPURequest, WebGPUResponse};
|
||||
|
||||
#[dom_struct]
|
||||
pub struct GPUAdapter {
|
||||
reflector_: Reflector,
|
||||
#[ignore_malloc_size_of = "channels are hard"]
|
||||
channel: WebGPU,
|
||||
name: DOMString,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
extensions: Heap<*mut JSObject>,
|
||||
|
@ -36,12 +38,14 @@ pub struct GPUAdapter {
|
|||
|
||||
impl GPUAdapter {
|
||||
pub fn new_inherited(
|
||||
channel: WebGPU,
|
||||
name: DOMString,
|
||||
extensions: Heap<*mut JSObject>,
|
||||
adapter: WebGPUAdapter,
|
||||
) -> GPUAdapter {
|
||||
GPUAdapter {
|
||||
reflector_: Reflector::new(),
|
||||
channel,
|
||||
name,
|
||||
extensions,
|
||||
adapter,
|
||||
|
@ -50,12 +54,15 @@ impl GPUAdapter {
|
|||
|
||||
pub fn new(
|
||||
global: &GlobalScope,
|
||||
channel: WebGPU,
|
||||
name: DOMString,
|
||||
extensions: Heap<*mut JSObject>,
|
||||
adapter: WebGPUAdapter,
|
||||
) -> DomRoot<GPUAdapter> {
|
||||
reflect_dom_object(
|
||||
Box::new(GPUAdapter::new_inherited(name, extensions, adapter)),
|
||||
Box::new(GPUAdapter::new_inherited(
|
||||
channel, name, extensions, adapter,
|
||||
)),
|
||||
global,
|
||||
GPUAdapterBinding::Wrap,
|
||||
)
|
||||
|
@ -89,12 +96,15 @@ impl GPUAdapterMethods for GPUAdapter {
|
|||
let id = window
|
||||
.Navigator()
|
||||
.create_device_id(self.adapter.0.backend());
|
||||
match window.webgpu_channel() {
|
||||
Some(thread) => thread
|
||||
.0
|
||||
.send(WebGPURequest::RequestDevice(sender, self.adapter, desc, id))
|
||||
.unwrap(),
|
||||
None => promise.reject_error(Error::Type("No WebGPU thread...".to_owned())),
|
||||
if self
|
||||
.channel
|
||||
.0
|
||||
.send(WebGPURequest::RequestDevice(sender, self.adapter, desc, id))
|
||||
.is_err()
|
||||
{
|
||||
promise.reject_error(Error::Type(
|
||||
"Failed to send RequestDevice message...".to_owned(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
promise.reject_error(Error::Type("No WebGPU thread...".to_owned()))
|
||||
|
@ -109,6 +119,7 @@ impl AsyncWGPUListener for GPUAdapter {
|
|||
WebGPUResponse::RequestDevice(device_id, _descriptor) => {
|
||||
let device = GPUDevice::new(
|
||||
&self.global(),
|
||||
self.channel.clone(),
|
||||
&self,
|
||||
Heap::default(),
|
||||
Heap::default(),
|
||||
|
|
|
@ -11,9 +11,8 @@ use crate::dom::bindings::root::DomRoot;
|
|||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use dom_struct::dom_struct;
|
||||
use ipc_channel::ipc::IpcSender;
|
||||
use std::cell::Cell;
|
||||
use webgpu::{WebGPUBuffer, WebGPUDevice, WebGPURequest};
|
||||
use webgpu::{WebGPU, WebGPUBuffer, WebGPUDevice, WebGPURequest};
|
||||
|
||||
#[derive(MallocSizeOf)]
|
||||
pub enum GPUBufferState {
|
||||
|
@ -26,7 +25,7 @@ pub enum GPUBufferState {
|
|||
pub struct GPUBuffer {
|
||||
reflector_: Reflector,
|
||||
#[ignore_malloc_size_of = "channels are hard"]
|
||||
channel: IpcSender<WebGPURequest>,
|
||||
channel: WebGPU,
|
||||
label: DomRefCell<Option<DOMString>>,
|
||||
size: GPUBufferSize,
|
||||
usage: u32,
|
||||
|
@ -38,7 +37,7 @@ pub struct GPUBuffer {
|
|||
|
||||
impl GPUBuffer {
|
||||
fn new_inherited(
|
||||
channel: IpcSender<WebGPURequest>,
|
||||
channel: WebGPU,
|
||||
buffer: WebGPUBuffer,
|
||||
device: WebGPUDevice,
|
||||
state: GPUBufferState,
|
||||
|
@ -62,7 +61,7 @@ impl GPUBuffer {
|
|||
#[allow(unsafe_code)]
|
||||
pub fn new(
|
||||
global: &GlobalScope,
|
||||
channel: IpcSender<WebGPURequest>,
|
||||
channel: WebGPU,
|
||||
buffer: WebGPUBuffer,
|
||||
device: WebGPUDevice,
|
||||
state: GPUBufferState,
|
||||
|
@ -90,6 +89,7 @@ impl GPUBufferMethods for GPUBuffer {
|
|||
/// https://gpuweb.github.io/gpuweb/#dom-gpubuffer-unmap
|
||||
fn Unmap(&self) {
|
||||
self.channel
|
||||
.0
|
||||
.send(WebGPURequest::UnmapBuffer(self.buffer))
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -103,6 +103,7 @@ impl GPUBufferMethods for GPUBuffer {
|
|||
_ => {},
|
||||
};
|
||||
self.channel
|
||||
.0
|
||||
.send(WebGPURequest::DestroyBuffer(self.buffer))
|
||||
.unwrap();
|
||||
*self.state.borrow_mut() = GPUBufferState::Destroyed;
|
||||
|
|
|
@ -25,11 +25,13 @@ use js::jsval::{JSVal, ObjectValue, UndefinedValue};
|
|||
use js::typedarray::{ArrayBuffer, CreateWith};
|
||||
use std::ptr::{self, NonNull};
|
||||
use webgpu::wgpu::resource::{BufferDescriptor, BufferUsage};
|
||||
use webgpu::{WebGPUBuffer, WebGPUDevice, WebGPURequest};
|
||||
use webgpu::{WebGPU, WebGPUBuffer, WebGPUDevice, WebGPURequest};
|
||||
|
||||
#[dom_struct]
|
||||
pub struct GPUDevice {
|
||||
eventtarget: EventTarget,
|
||||
#[ignore_malloc_size_of = "channels are hard"]
|
||||
channel: WebGPU,
|
||||
adapter: Dom<GPUAdapter>,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
extensions: Heap<*mut JSObject>,
|
||||
|
@ -41,6 +43,7 @@ pub struct GPUDevice {
|
|||
|
||||
impl GPUDevice {
|
||||
fn new_inherited(
|
||||
channel: WebGPU,
|
||||
adapter: &GPUAdapter,
|
||||
extensions: Heap<*mut JSObject>,
|
||||
limits: Heap<*mut JSObject>,
|
||||
|
@ -48,6 +51,7 @@ impl GPUDevice {
|
|||
) -> GPUDevice {
|
||||
Self {
|
||||
eventtarget: EventTarget::new_inherited(),
|
||||
channel,
|
||||
adapter: Dom::from_ref(adapter),
|
||||
extensions,
|
||||
limits,
|
||||
|
@ -59,6 +63,7 @@ impl GPUDevice {
|
|||
#[allow(unsafe_code)]
|
||||
pub fn new(
|
||||
global: &GlobalScope,
|
||||
channel: WebGPU,
|
||||
adapter: &GPUAdapter,
|
||||
extensions: Heap<*mut JSObject>,
|
||||
limits: Heap<*mut JSObject>,
|
||||
|
@ -66,7 +71,7 @@ impl GPUDevice {
|
|||
) -> DomRoot<GPUDevice> {
|
||||
reflect_dom_object(
|
||||
Box::new(GPUDevice::new_inherited(
|
||||
adapter, extensions, limits, device,
|
||||
channel, adapter, extensions, limits, device,
|
||||
)),
|
||||
global,
|
||||
GPUDeviceBinding::Wrap,
|
||||
|
@ -78,7 +83,6 @@ impl GPUDevice {
|
|||
unsafe fn resolve_create_buffer_mapped(
|
||||
&self,
|
||||
cx: SafeJSContext,
|
||||
channel: ipc_channel::ipc::IpcSender<WebGPURequest>,
|
||||
gpu_buffer: WebGPUBuffer,
|
||||
array_buffer: Vec<u8>,
|
||||
descriptor: BufferDescriptor,
|
||||
|
@ -95,7 +99,7 @@ impl GPUDevice {
|
|||
|
||||
let buff = GPUBuffer::new(
|
||||
&self.global(),
|
||||
channel,
|
||||
self.channel.clone(),
|
||||
gpu_buffer,
|
||||
self.device,
|
||||
GPUBufferState::Mapped,
|
||||
|
@ -165,25 +169,18 @@ impl GPUDeviceMethods for GPUDevice {
|
|||
/// https://gpuweb.github.io/gpuweb/#dom-gpudevice-createbuffer
|
||||
fn CreateBuffer(&self, descriptor: &GPUBufferDescriptor) -> DomRoot<GPUBuffer> {
|
||||
let (valid, wgpu_descriptor) = self.validate_buffer_descriptor(descriptor);
|
||||
let channel;
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
if let Some(window) = self.global().downcast::<Window>() {
|
||||
let id = window.Navigator().create_buffer_id(self.device.0.backend());
|
||||
match window.webgpu_channel() {
|
||||
Some(thread) => {
|
||||
channel = thread.0.clone();
|
||||
thread
|
||||
.0
|
||||
.send(WebGPURequest::CreateBuffer(
|
||||
sender,
|
||||
self.device,
|
||||
id,
|
||||
wgpu_descriptor,
|
||||
))
|
||||
.unwrap();
|
||||
},
|
||||
None => unimplemented!(),
|
||||
}
|
||||
self.channel
|
||||
.0
|
||||
.send(WebGPURequest::CreateBuffer(
|
||||
sender,
|
||||
self.device,
|
||||
id,
|
||||
wgpu_descriptor,
|
||||
))
|
||||
.unwrap();
|
||||
} else {
|
||||
unimplemented!()
|
||||
};
|
||||
|
@ -192,7 +189,7 @@ impl GPUDeviceMethods for GPUDevice {
|
|||
|
||||
GPUBuffer::new(
|
||||
&self.global(),
|
||||
channel,
|
||||
self.channel.clone(),
|
||||
buffer,
|
||||
self.device,
|
||||
GPUBufferState::Unmapped,
|
||||
|
@ -209,26 +206,19 @@ impl GPUDeviceMethods for GPUDevice {
|
|||
descriptor: &GPUBufferDescriptor,
|
||||
) -> Vec<JSVal> {
|
||||
let (valid, wgpu_descriptor) = self.validate_buffer_descriptor(descriptor);
|
||||
let channel;
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
rooted!(in(*cx) let js_val = UndefinedValue());
|
||||
if let Some(window) = self.global().downcast::<Window>() {
|
||||
let id = window.Navigator().create_buffer_id(self.device.0.backend());
|
||||
match window.webgpu_channel() {
|
||||
Some(thread) => {
|
||||
channel = thread.0.clone();
|
||||
thread
|
||||
.0
|
||||
.send(WebGPURequest::CreateBufferMapped(
|
||||
sender,
|
||||
self.device,
|
||||
id,
|
||||
wgpu_descriptor.clone(),
|
||||
))
|
||||
.unwrap()
|
||||
},
|
||||
None => return vec![js_val.get()],
|
||||
}
|
||||
self.channel
|
||||
.0
|
||||
.send(WebGPURequest::CreateBufferMapped(
|
||||
sender,
|
||||
self.device,
|
||||
id,
|
||||
wgpu_descriptor.clone(),
|
||||
))
|
||||
.unwrap()
|
||||
} else {
|
||||
return vec![js_val.get()];
|
||||
};
|
||||
|
@ -236,14 +226,7 @@ impl GPUDeviceMethods for GPUDevice {
|
|||
let (buffer, array_buffer) = receiver.recv().unwrap();
|
||||
|
||||
unsafe {
|
||||
self.resolve_create_buffer_mapped(
|
||||
cx,
|
||||
channel,
|
||||
buffer,
|
||||
array_buffer,
|
||||
wgpu_descriptor,
|
||||
valid,
|
||||
)
|
||||
self.resolve_create_buffer_mapped(cx, buffer, array_buffer, wgpu_descriptor, valid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -135,7 +135,6 @@ use style::str::HTML_SPACE_CHARACTERS;
|
|||
use style::stylesheets::CssRuleType;
|
||||
use style_traits::{CSSPixel, DevicePixel, ParsingMode};
|
||||
use url::Position;
|
||||
use webgpu::WebGPU;
|
||||
use webrender_api::units::{DeviceIntPoint, DeviceIntSize, LayoutPixel};
|
||||
use webrender_api::{DocumentId, ExternalScrollId};
|
||||
use webvr_traits::WebVRMsg;
|
||||
|
@ -267,10 +266,6 @@ pub struct Window {
|
|||
#[ignore_malloc_size_of = "channels are hard"]
|
||||
webgl_chan: Option<WebGLChan>,
|
||||
|
||||
#[ignore_malloc_size_of = "channels are hard"]
|
||||
/// A handle for communicating messages to the WebGPU threads.
|
||||
webgpu: Option<WebGPU>,
|
||||
|
||||
/// A handle for communicating messages to the webvr thread, if available.
|
||||
#[ignore_malloc_size_of = "channels are hard"]
|
||||
webvr_chan: Option<IpcSender<WebVRMsg>>,
|
||||
|
@ -466,10 +461,6 @@ impl Window {
|
|||
.map(|chan| WebGLCommandSender::new(chan.clone(), self.get_event_loop_waker()))
|
||||
}
|
||||
|
||||
pub fn webgpu_channel(&self) -> Option<WebGPU> {
|
||||
self.webgpu.clone()
|
||||
}
|
||||
|
||||
pub fn webvr_thread(&self) -> Option<IpcSender<WebVRMsg>> {
|
||||
self.webvr_chan.clone()
|
||||
}
|
||||
|
@ -2213,7 +2204,6 @@ impl Window {
|
|||
navigation_start: u64,
|
||||
navigation_start_precise: u64,
|
||||
webgl_chan: Option<WebGLChan>,
|
||||
webgpu: Option<WebGPU>,
|
||||
webvr_chan: Option<IpcSender<WebVRMsg>>,
|
||||
webxr_registry: webxr_api::Registry,
|
||||
microtask_queue: Rc<MicrotaskQueue>,
|
||||
|
@ -2292,7 +2282,6 @@ impl Window {
|
|||
media_query_lists: DOMTracker::new(),
|
||||
test_runner: Default::default(),
|
||||
webgl_chan,
|
||||
webgpu,
|
||||
webvr_chan,
|
||||
webxr_registry,
|
||||
permission_state_invocation_results: Default::default(),
|
||||
|
|
|
@ -164,7 +164,6 @@ use style::dom::OpaqueNode;
|
|||
use style::thread_state::{self, ThreadState};
|
||||
use time::{at_utc, get_time, precise_time_ns, Timespec};
|
||||
use url::Position;
|
||||
use webgpu::WebGPU;
|
||||
use webrender_api::units::LayoutPixel;
|
||||
use webrender_api::DocumentId;
|
||||
use webvr_traits::{WebVREvent, WebVRMsg};
|
||||
|
@ -630,9 +629,6 @@ pub struct ScriptThread {
|
|||
/// A handle to the WebGL thread
|
||||
webgl_chan: Option<WebGLPipeline>,
|
||||
|
||||
/// A handle to the WebGPU threads
|
||||
webgpu: Option<WebGPU>,
|
||||
|
||||
/// A handle to the webvr thread, if available
|
||||
webvr_chan: Option<IpcSender<WebVRMsg>>,
|
||||
|
||||
|
@ -1339,7 +1335,6 @@ impl ScriptThread {
|
|||
layout_to_constellation_chan: state.layout_to_constellation_chan,
|
||||
|
||||
webgl_chan: state.webgl_chan,
|
||||
webgpu: state.webgpu,
|
||||
webvr_chan: state.webvr_chan,
|
||||
webxr_registry: state.webxr_registry,
|
||||
|
||||
|
@ -3201,7 +3196,6 @@ impl ScriptThread {
|
|||
incomplete.navigation_start,
|
||||
incomplete.navigation_start_precise,
|
||||
self.webgl_chan.as_ref().map(|chan| chan.channel()),
|
||||
self.webgpu.clone(),
|
||||
self.webvr_chan.clone(),
|
||||
self.webxr_registry.clone(),
|
||||
self.microtask_queue.clone(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue