Skip to content

Commit

Permalink
Share single gpu_id_hub among all threads in a process
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalmohan committed May 14, 2020
1 parent 6c506ba commit e5065c7
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 8 deletions.
4 changes: 2 additions & 2 deletions components/script/dom/bindings/trace.rs
Expand Up @@ -93,7 +93,7 @@ use net_traits::response::HttpsState;
use net_traits::response::{Response, ResponseBody};
use net_traits::storage_thread::StorageType;
use net_traits::{Metadata, NetworkError, ReferrerPolicy, ResourceFetchTiming, ResourceThreads};
use parking_lot::RwLock;
use parking_lot::{Mutex as ParkMutex, RwLock};
use profile_traits::mem::ProfilerChan as MemProfilerChan;
use profile_traits::time::ProfilerChan as TimeProfilerChan;
use script_layout_interface::message::PendingRestyle;
Expand Down Expand Up @@ -544,7 +544,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!(Identities);
unsafe_no_jsmanaged_fields!(Arc<ParkMutex<Identities>>);
unsafe_no_jsmanaged_fields!(WebGPU);
unsafe_no_jsmanaged_fields!(WebGPUAdapter);
unsafe_no_jsmanaged_fields!(WebGPUBuffer);
Expand Down
8 changes: 8 additions & 0 deletions components/script/dom/dedicatedworkerglobalscope.rs
Expand Up @@ -22,6 +22,7 @@ use crate::dom::errorevent::ErrorEvent;
use crate::dom::event::{Event, EventBubbles, EventCancelable, EventStatus};
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::identityhub::Identities;
use crate::dom::messageevent::MessageEvent;
use crate::dom::worker::{TrustedWorkerAddress, Worker};
use crate::dom::workerglobalscope::WorkerGlobalScope;
Expand All @@ -48,6 +49,7 @@ use net_traits::image_cache::ImageCache;
use net_traits::request::{CredentialsMode, Destination, ParserMetadata};
use net_traits::request::{Referrer, RequestBuilder, RequestMode};
use net_traits::IpcSend;
use parking_lot::Mutex;
use script_traits::{WorkerGlobalScopeInit, WorkerScriptLoadOrigin};
use servo_rand::random;
use servo_url::ServoUrl;
Expand Down Expand Up @@ -223,6 +225,7 @@ impl DedicatedWorkerGlobalScope {
closing: Arc<AtomicBool>,
image_cache: Arc<dyn ImageCache>,
browsing_context: Option<BrowsingContextId>,
gpu_id_hub: Arc<Mutex<Identities>>,
) -> DedicatedWorkerGlobalScope {
DedicatedWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope::new_inherited(
Expand All @@ -233,6 +236,7 @@ impl DedicatedWorkerGlobalScope {
runtime,
from_devtools_receiver,
Some(closing),
gpu_id_hub,
),
task_queue: TaskQueue::new(receiver, own_sender.clone()),
own_sender: own_sender,
Expand All @@ -257,6 +261,7 @@ impl DedicatedWorkerGlobalScope {
closing: Arc<AtomicBool>,
image_cache: Arc<dyn ImageCache>,
browsing_context: Option<BrowsingContextId>,
gpu_id_hub: Arc<Mutex<Identities>>,
) -> DomRoot<DedicatedWorkerGlobalScope> {
let cx = runtime.cx();
let scope = Box::new(DedicatedWorkerGlobalScope::new_inherited(
Expand All @@ -272,6 +277,7 @@ impl DedicatedWorkerGlobalScope {
closing,
image_cache,
browsing_context,
gpu_id_hub,
));
unsafe { DedicatedWorkerGlobalScopeBinding::Wrap(SafeJSContext::from_ptr(cx), scope) }
}
Expand All @@ -292,6 +298,7 @@ impl DedicatedWorkerGlobalScope {
closing: Arc<AtomicBool>,
image_cache: Arc<dyn ImageCache>,
browsing_context: Option<BrowsingContextId>,
gpu_id_hub: Arc<Mutex<Identities>>,
) {
let serialized_worker_url = worker_url.to_string();
let name = format!("WebWorker for {}", serialized_worker_url);
Expand Down Expand Up @@ -361,6 +368,7 @@ impl DedicatedWorkerGlobalScope {
closing,
image_cache,
browsing_context,
gpu_id_hub,
);
// FIXME(njn): workers currently don't have a unique ID suitable for using in reporter
// registration (#6631), so we instead use a random number and cross our fingers.
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/dissimilaroriginwindow.rs
Expand Up @@ -62,6 +62,7 @@ impl DissimilarOriginWindow {
global_to_clone_from.microtask_queue().clone(),
global_to_clone_from.is_headless(),
global_to_clone_from.get_user_agent(),
global_to_clone_from.wgpu_id_hub(),
),
window_proxy: Dom::from_ref(window_proxy),
location: Default::default(),
Expand Down
12 changes: 7 additions & 5 deletions components/script/dom/globalscope.rs
Expand Up @@ -88,6 +88,7 @@ use net_traits::filemanager_thread::{
};
use net_traits::image_cache::ImageCache;
use net_traits::{CoreResourceMsg, CoreResourceThread, IpcSend, ResourceThreads};
use parking_lot::Mutex;
use profile_traits::{ipc as profile_ipc, mem as profile_mem, time as profile_time};
use script_traits::serializable::{BlobData, BlobImpl, FileBlob};
use script_traits::transferable::MessagePortImpl;
Expand All @@ -98,7 +99,7 @@ use script_traits::{
use script_traits::{TimerEventId, TimerSchedulerMsg, TimerSource};
use servo_url::{MutableOrigin, ServoUrl};
use std::borrow::Cow;
use std::cell::{Cell, RefCell, RefMut};
use std::cell::Cell;
use std::collections::hash_map::Entry;
use std::collections::{HashMap, VecDeque};
use std::ffi::CString;
Expand Down Expand Up @@ -232,7 +233,7 @@ pub struct GlobalScope {
user_agent: Cow<'static, str>,

#[ignore_malloc_size_of = "defined in wgpu"]
gpu_id_hub: RefCell<Identities>,
gpu_id_hub: Arc<Mutex<Identities>>,

// https://w3c.github.io/performance-timeline/#supportedentrytypes-attribute
#[ignore_malloc_size_of = "mozjs"]
Expand Down Expand Up @@ -554,6 +555,7 @@ impl GlobalScope {
microtask_queue: Rc<MicrotaskQueue>,
is_headless: bool,
user_agent: Cow<'static, str>,
gpu_id_hub: Arc<Mutex<Identities>>,
) -> Self {
Self {
message_port_state: DomRefCell::new(MessagePortState::UnManaged),
Expand Down Expand Up @@ -584,7 +586,7 @@ impl GlobalScope {
consumed_rejections: Default::default(),
is_headless,
user_agent,
gpu_id_hub: RefCell::new(Identities::new()),
gpu_id_hub,
frozen_supported_performance_entry_types: DomRefCell::new(Default::default()),
}
}
Expand Down Expand Up @@ -2510,8 +2512,8 @@ impl GlobalScope {
None
}

pub fn wgpu_id_hub(&self) -> RefMut<Identities> {
self.gpu_id_hub.borrow_mut()
pub fn wgpu_id_hub(&self) -> Arc<Mutex<Identities>> {
self.gpu_id_hub.clone()
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/gpu.rs
Expand Up @@ -115,7 +115,7 @@ impl GPUMethods for GPU {
},
None => wgpu::instance::PowerPreference::Default,
};
let ids = global.wgpu_id_hub().create_adapter_ids();
let ids = global.wgpu_id_hub().lock().create_adapter_ids();

let script_to_constellation_chan = global.script_to_constellation_chan();
if script_to_constellation_chan
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/gpuadapter.rs
Expand Up @@ -91,6 +91,7 @@ impl GPUAdapterMethods for GPUAdapter {
let id = self
.global()
.wgpu_id_hub()
.lock()
.create_device_id(self.adapter.0.backend());
if self
.channel
Expand Down
8 changes: 8 additions & 0 deletions components/script/dom/gpudevice.rs
Expand Up @@ -171,6 +171,7 @@ impl GPUDeviceMethods for GPUDevice {
let id = self
.global()
.wgpu_id_hub()
.lock()
.create_buffer_id(self.device.0.backend());
self.channel
.0
Expand Down Expand Up @@ -208,6 +209,7 @@ impl GPUDeviceMethods for GPUDevice {
let buffer_id = self
.global()
.wgpu_id_hub()
.lock()
.create_buffer_id(self.device.0.backend());
self.channel
.0
Expand Down Expand Up @@ -393,6 +395,7 @@ impl GPUDeviceMethods for GPUDevice {
let bind_group_layout_id = self
.global()
.wgpu_id_hub()
.lock()
.create_bind_group_layout_id(self.device.0.backend());
self.channel
.0
Expand Down Expand Up @@ -472,6 +475,7 @@ impl GPUDeviceMethods for GPUDevice {
let pipeline_layout_id = self
.global()
.wgpu_id_hub()
.lock()
.create_pipeline_layout_id(self.device.0.backend());
self.channel
.0
Expand Down Expand Up @@ -532,6 +536,7 @@ impl GPUDeviceMethods for GPUDevice {
let bind_group_id = self
.global()
.wgpu_id_hub()
.lock()
.create_bind_group_id(self.device.0.backend());
self.channel
.0
Expand Down Expand Up @@ -561,6 +566,7 @@ impl GPUDeviceMethods for GPUDevice {
let program_id = self
.global()
.wgpu_id_hub()
.lock()
.create_shader_module_id(self.device.0.backend());
self.channel
.0
Expand All @@ -587,6 +593,7 @@ impl GPUDeviceMethods for GPUDevice {
let compute_pipeline_id = self
.global()
.wgpu_id_hub()
.lock()
.create_compute_pipeline_id(self.device.0.backend());
let (sender, receiver) = ipc::channel().unwrap();
self.channel
Expand All @@ -613,6 +620,7 @@ impl GPUDeviceMethods for GPUDevice {
let command_encoder_id = self
.global()
.wgpu_id_hub()
.lock()
.create_command_encoder_id(self.device.0.backend());
self.channel
.0
Expand Down
4 changes: 4 additions & 0 deletions components/script/dom/serviceworkerglobalscope.rs
Expand Up @@ -18,6 +18,7 @@ use crate::dom::eventtarget::EventTarget;
use crate::dom::extendableevent::ExtendableEvent;
use crate::dom::extendablemessageevent::ExtendableMessageEvent;
use crate::dom::globalscope::GlobalScope;
use crate::dom::identityhub::Identities;
use crate::dom::messageevent::MessageEvent;
use crate::dom::worker::TrustedWorkerAddress;
use crate::dom::workerglobalscope::WorkerGlobalScope;
Expand All @@ -38,10 +39,12 @@ use js::jsval::UndefinedValue;
use msg::constellation_msg::PipelineId;
use net_traits::request::{CredentialsMode, Destination, ParserMetadata, Referrer, RequestBuilder};
use net_traits::{CustomResponseMediator, IpcSend};
use parking_lot::Mutex;
use script_traits::{ScopeThings, ServiceWorkerMsg, WorkerGlobalScopeInit, WorkerScriptLoadOrigin};
use servo_config::pref;
use servo_rand::random;
use servo_url::ServoUrl;
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use style::thread_state::{self, ThreadState};
Expand Down Expand Up @@ -210,6 +213,7 @@ impl ServiceWorkerGlobalScope {
runtime,
from_devtools_receiver,
None,
Arc::new(Mutex::new(Identities::new())),
),
task_queue: TaskQueue::new(receiver, own_sender.clone()),
own_sender: own_sender,
Expand Down
4 changes: 4 additions & 0 deletions components/script/dom/window.rs
Expand Up @@ -40,6 +40,7 @@ use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::hashchangeevent::HashChangeEvent;
use crate::dom::history::History;
use crate::dom::identityhub::Identities;
use crate::dom::location::Location;
use crate::dom::mediaquerylist::{MediaQueryList, MediaQueryListMatchState};
use crate::dom::mediaquerylistevent::MediaQueryListEvent;
Expand Down Expand Up @@ -100,6 +101,7 @@ use net_traits::image_cache::{PendingImageId, PendingImageResponse};
use net_traits::storage_thread::StorageType;
use net_traits::ResourceThreads;
use num_traits::ToPrimitive;
use parking_lot::Mutex as ParkMutex;
use profile_traits::ipc as ProfiledIpc;
use profile_traits::mem::ProfilerChan as MemProfilerChan;
use profile_traits::time::{ProfilerChan as TimeProfilerChan, ProfilerMsg};
Expand Down Expand Up @@ -2322,6 +2324,7 @@ impl Window {
user_agent: Cow<'static, str>,
player_context: WindowGLContext,
event_loop_waker: Option<Box<dyn EventLoopWaker>>,
gpu_id_hub: Arc<ParkMutex<Identities>>,
) -> DomRoot<Self> {
let layout_rpc: Box<dyn LayoutRPC + Send> = {
let (rpc_send, rpc_recv) = unbounded();
Expand All @@ -2345,6 +2348,7 @@ impl Window {
microtask_queue,
is_headless,
user_agent,
gpu_id_hub,
),
script_chan,
task_manager,
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/worker.rs
Expand Up @@ -139,6 +139,7 @@ impl Worker {
closing,
global.image_cache(),
browsing_context,
global.wgpu_id_hub(),
);

Ok(worker)
Expand Down
4 changes: 4 additions & 0 deletions components/script/dom/workerglobalscope.rs
Expand Up @@ -21,6 +21,7 @@ use crate::dom::bindings::trace::RootedTraceableBox;
use crate::dom::crypto::Crypto;
use crate::dom::dedicatedworkerglobalscope::DedicatedWorkerGlobalScope;
use crate::dom::globalscope::GlobalScope;
use crate::dom::identityhub::Identities;
use crate::dom::performance::Performance;
use crate::dom::promise::Promise;
use crate::dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
Expand Down Expand Up @@ -53,6 +54,7 @@ use net_traits::request::{
CredentialsMode, Destination, ParserMetadata, RequestBuilder as NetRequestInit,
};
use net_traits::IpcSend;
use parking_lot::Mutex;
use script_traits::WorkerGlobalScopeInit;
use servo_url::{MutableOrigin, ServoUrl};
use std::default::Default;
Expand Down Expand Up @@ -125,6 +127,7 @@ impl WorkerGlobalScope {
runtime: Runtime,
from_devtools_receiver: Receiver<DevtoolScriptControlMsg>,
closing: Option<Arc<AtomicBool>>,
gpu_id_hub: Arc<Mutex<Identities>>,
) -> Self {
// Install a pipeline-namespace in the current thread.
PipelineNamespace::auto_install();
Expand All @@ -141,6 +144,7 @@ impl WorkerGlobalScope {
runtime.microtask_queue.clone(),
init.is_headless,
init.user_agent,
gpu_id_hub,
),
worker_id: init.worker_id,
worker_name,
Expand Down
5 changes: 5 additions & 0 deletions components/script/dom/workletglobalscope.rs
Expand Up @@ -5,6 +5,7 @@
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::dom::identityhub::Identities;
use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
use crate::dom::paintworkletglobalscope::PaintWorkletTask;
use crate::dom::testworkletglobalscope::TestWorkletGlobalScope;
Expand All @@ -21,6 +22,7 @@ use js::rust::Runtime;
use msg::constellation_msg::PipelineId;
use net_traits::image_cache::ImageCache;
use net_traits::ResourceThreads;
use parking_lot::Mutex;
use profile_traits::mem;
use profile_traits::time;
use script_traits::{Painter, ScriptMsg};
Expand Down Expand Up @@ -71,6 +73,7 @@ impl WorkletGlobalScope {
Default::default(),
init.is_headless,
init.user_agent.clone(),
init.gpu_id_hub.clone(),
),
base_url,
to_script_thread_sender: init.to_script_thread_sender.clone(),
Expand Down Expand Up @@ -156,6 +159,8 @@ pub struct WorkletGlobalScopeInit {
pub is_headless: bool,
/// An optional string allowing the user agent to be set for testing
pub user_agent: Cow<'static, str>,
/// Channel to WebGPU
pub gpu_id_hub: Arc<Mutex<Identities>>,
}

/// <https://drafts.css-houdini.org/worklets/#worklet-global-scope-type>
Expand Down

0 comments on commit e5065c7

Please sign in to comment.