script: Do not run layout in a thread (#31346)

* script: Do not run layout in a thread

Instead of spawning a thread for layout that almost always runs
synchronously with script, simply run layout in the script thread.

This is a resurrection of #28708, taking just the bits that remove the
layout thread. It's a complex change and thus is just a first step
toward cleaning up the interface between script and layout. Messages are
still passed from script to layout via a `process()` method and script
proxies some messages to layout from other threads as well.

Big changes:

1. Layout is created in the script thread on Document load, thus every
   live document is guaranteed to have a layout. This isn't completely
   hidden in the interface, but we can safely `unwrap()` on a Document's
   layout.
2. Layout configuration is abstracted away into a LayoutConfig struct
   and the LayoutFactory is a struct passed around by the Constellation.
   This is to avoid having to monomorphize the entire script thread
   for each layout.
3. Instead of having the Constellation block on the layout thread to
   figure out the current epoch and whether there are pending web fonts
   loading, updates are sent synchronously to the Constellation when
   rendering to a screenshot. This practically only used by the WPT.

A couple tests start to fail, which is probably inevitable since removing
the layout thread has introduced timing changes in "exit after load" and
screenshot behavior.

Co-authored-by: Josh Matthews <josh@joshmatthews.net>

* Update test expectations

* Fix some issues found during review

* Clarify some comments

* Address review comments

---------

Co-authored-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Martin Robinson 2024-02-23 09:14:10 +01:00 committed by GitHub
parent 4849ba901e
commit 9c0561536d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
44 changed files with 636 additions and 1253 deletions

View file

@ -2,8 +2,8 @@
* 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/. */
//! The script thread is the thread that owns the DOM in memory, runs JavaScript, and spawns parsing
//! and layout threads. It's in charge of processing events for all same-origin pages in a frame
//! The script thread is the thread that owns the DOM in memory, runs JavaScript, and triggers
//! layout. It's in charge of processing events for all same-origin pages in a frame
//! tree, and manages the entire lifetime of pages in the frame tree from initial request to
//! teardown.
//!
@ -41,6 +41,7 @@ use devtools_traits::{
use embedder_traits::EmbedderMsg;
use euclid::default::{Point2D, Rect};
use euclid::Vector2D;
use gfx::font_cache_thread::FontCacheThread;
use headers::{HeaderMapExt, LastModified, ReferrerPolicy as ReferrerPolicyHeader};
use html5ever::{local_name, namespace_url, ns};
use hyper_serde::Serde;
@ -56,10 +57,9 @@ use media::WindowGLContext;
use metrics::{PaintTimeMetrics, MAX_TASK_NS};
use mime::{self, Mime};
use msg::constellation_msg::{
BackgroundHangMonitor, BackgroundHangMonitorExitSignal, BackgroundHangMonitorRegister,
BrowsingContextId, HangAnnotation, HistoryStateId, MonitoredComponentId,
MonitoredComponentType, PipelineId, PipelineNamespace, ScriptHangAnnotation,
TopLevelBrowsingContextId,
BackgroundHangMonitor, BackgroundHangMonitorExitSignal, BrowsingContextId, HangAnnotation,
HistoryStateId, MonitoredComponentId, MonitoredComponentType, PipelineId, PipelineNamespace,
ScriptHangAnnotation, TopLevelBrowsingContextId,
};
use net_traits::image_cache::{ImageCache, PendingImageResponse};
use net_traits::request::{CredentialsMode, Destination, RedirectMode, RequestBuilder};
@ -72,7 +72,8 @@ use parking_lot::Mutex;
use percent_encoding::percent_decode;
use profile_traits::mem::{self as profile_mem, OpaqueSender, ReportsChan};
use profile_traits::time::{self as profile_time, profile, ProfilerCategory};
use script_layout_interface::message::{self, LayoutThreadInit, Msg, ReflowGoal};
use script_layout_interface::message::{Msg, ReflowGoal};
use script_layout_interface::{Layout, LayoutConfig, LayoutFactory, ScriptThreadFactory};
use script_traits::webdriver_msg::WebDriverScriptCommand;
use script_traits::CompositorEvent::{
CompositionEvent, GamepadEvent, IMEDismissedEvent, KeyboardEvent, MouseButtonEvent,
@ -81,8 +82,8 @@ use script_traits::CompositorEvent::{
use script_traits::{
AnimationTickType, CompositorEvent, ConstellationControlMsg, DiscardBrowsingContext,
DocumentActivity, EventResult, HistoryEntryReplacement, InitialScriptState, JsEvalResult,
LayoutMsg, LoadData, LoadOrigin, MediaSessionActionType, MouseButton, MouseEventType,
NewLayoutInfo, Painter, ProgressiveWebMetricType, ScriptMsg, ScriptThreadFactory,
LayoutControlMsg, LayoutMsg, LoadData, LoadOrigin, MediaSessionActionType, MouseButton,
MouseEventType, NewLayoutInfo, Painter, ProgressiveWebMetricType, ScriptMsg,
ScriptToConstellationChan, StructuredSerializedData, TimerSchedulerMsg, TouchEventType,
TouchId, UntrustedNodeAddress, UpdatePipelineIdReason, WebrenderIpcSender, WheelDelta,
WindowSizeData, WindowSizeType,
@ -202,9 +203,6 @@ struct InProgressLoad {
/// The current window size associated with this pipeline.
#[no_trace]
window_size: WindowSizeData,
/// Channel to the layout thread associated with this pipeline.
#[no_trace]
layout_chan: Sender<message::Msg>,
/// The activity level of the document (inactive, active or fully active).
#[no_trace]
activity: DocumentActivity,
@ -222,8 +220,6 @@ struct InProgressLoad {
navigation_start_precise: u64,
/// For cancelling the fetch
canceller: FetchCanceller,
/// Flag for sharing with the layout thread that is not yet created.
layout_is_busy: Arc<AtomicBool>,
/// If inheriting the security context
inherited_secure_context: Option<bool>,
}
@ -236,11 +232,9 @@ impl InProgressLoad {
top_level_browsing_context_id: TopLevelBrowsingContextId,
parent_info: Option<PipelineId>,
opener: Option<BrowsingContextId>,
layout_chan: Sender<message::Msg>,
window_size: WindowSizeData,
url: ServoUrl,
origin: MutableOrigin,
layout_is_busy: Arc<AtomicBool>,
inherited_secure_context: Option<bool>,
) -> InProgressLoad {
let duration = SystemTime::now()
@ -248,18 +242,12 @@ impl InProgressLoad {
.unwrap_or_default();
let navigation_start = duration.as_millis();
let navigation_start_precise = precise_time_ns();
layout_chan
.send(message::Msg::SetNavigationStart(
navigation_start_precise as u64,
))
.unwrap();
InProgressLoad {
pipeline_id: id,
browsing_context_id: browsing_context_id,
top_level_browsing_context_id: top_level_browsing_context_id,
parent_info: parent_info,
opener: opener,
layout_chan: layout_chan,
window_size: window_size,
activity: DocumentActivity::FullyActive,
is_visible: true,
@ -268,7 +256,6 @@ impl InProgressLoad {
navigation_start: navigation_start as u64,
navigation_start_precise: navigation_start_precise,
canceller: Default::default(),
layout_is_busy: layout_is_busy,
inherited_secure_context: inherited_secure_context,
}
}
@ -554,9 +541,6 @@ pub struct ScriptThread {
/// A queue of tasks to be executed in this script-thread.
task_queue: TaskQueue<MainThreadScriptMsg>,
/// A handle to register associated layout threads for hang-monitoring.
#[no_trace]
background_hang_monitor_register: Box<dyn BackgroundHangMonitorRegister>,
/// The dedicated means of communication with the background-hang-monitor for this script-thread.
#[no_trace]
background_hang_monitor: Box<dyn BackgroundHangMonitor>,
@ -596,7 +580,7 @@ pub struct ScriptThread {
#[no_trace]
control_chan: IpcSender<ConstellationControlMsg>,
/// The port on which the constellation and layout threads can communicate with the
/// The port on which the constellation and layout can communicate with the
/// script thread.
#[no_trace]
control_port: Receiver<ConstellationControlMsg>,
@ -605,10 +589,14 @@ pub struct ScriptThread {
#[no_trace]
script_sender: IpcSender<(PipelineId, ScriptMsg)>,
/// A sender for new layout threads to communicate to the constellation.
/// A sender for layout to communicate to the constellation.
#[no_trace]
layout_to_constellation_chan: IpcSender<LayoutMsg>,
/// The font cache thread to use for layout that happens in this [`ScriptThread`].
#[no_trace]
font_cache_thread: FontCacheThread,
/// The port on which we receive messages from the image cache
#[no_trace]
image_cache_port: Receiver<ImageCacheMsg>,
@ -738,6 +726,14 @@ pub struct ScriptThread {
// Secure context
inherited_secure_context: Option<bool>,
/// The layouts that we control.
#[no_trace]
layouts: RefCell<HashMap<PipelineId, Box<dyn Layout>>>,
/// A factory for making new layouts. This allows layout to depend on script.
#[no_trace]
layout_factory: Arc<dyn LayoutFactory>,
}
struct BHMExitSignal {
@ -791,21 +787,18 @@ impl<'a> Drop for ScriptMemoryFailsafe<'a> {
}
impl ScriptThreadFactory for ScriptThread {
type Message = message::Msg;
fn create(
state: InitialScriptState,
layout_factory: Arc<dyn LayoutFactory>,
font_cache_thread: FontCacheThread,
load_data: LoadData,
user_agent: Cow<'static, str>,
) -> (Sender<message::Msg>, Receiver<message::Msg>) {
) {
let (script_chan, script_port) = unbounded();
let (sender, receiver) = unbounded();
let layout_chan = sender.clone();
thread::Builder::new()
.name(format!("Script{:?}", state.id))
.spawn(move || {
thread_state::initialize(ThreadState::SCRIPT);
thread_state::initialize(ThreadState::SCRIPT | ThreadState::LAYOUT);
PipelineNamespace::install(state.pipeline_namespace_id);
TopLevelBrowsingContextId::install(state.top_level_browsing_context_id);
let roots = RootCollection::new();
@ -818,10 +811,15 @@ impl ScriptThreadFactory for ScriptThread {
let secure = load_data.inherited_secure_context.clone();
let mem_profiler_chan = state.mem_profiler_chan.clone();
let window_size = state.window_size;
let layout_is_busy = state.layout_is_busy.clone();
let script_thread =
ScriptThread::new(state, script_port, script_chan.clone(), user_agent);
let script_thread = ScriptThread::new(
state,
script_port,
script_chan.clone(),
layout_factory,
font_cache_thread,
user_agent,
);
SCRIPT_THREAD_ROOT.with(|root| {
root.set(Some(&script_thread as *const _));
@ -836,11 +834,9 @@ impl ScriptThreadFactory for ScriptThread {
top_level_browsing_context_id,
parent_info,
opener,
layout_chan,
window_size,
load_data.url.clone(),
origin,
layout_is_busy,
secure,
);
script_thread.pre_page_load(new_load, load_data);
@ -860,12 +856,26 @@ impl ScriptThreadFactory for ScriptThread {
failsafe.neuter();
})
.expect("Thread spawning failed");
(sender, receiver)
}
}
impl ScriptThread {
pub fn with_layout<'a, T>(
pipeline_id: PipelineId,
call: impl FnOnce(&mut dyn Layout) -> T,
) -> Result<T, ()> {
SCRIPT_THREAD_ROOT.with(|root| {
let script_thread = unsafe { &*root.get().unwrap() };
let mut layouts = script_thread.layouts.borrow_mut();
if let Some(ref mut layout) = layouts.get_mut(&pipeline_id) {
Ok(call(&mut ***layout))
} else {
warn!("No layout found for {}", pipeline_id);
Err(())
}
})
}
pub fn runtime_handle() -> ParentRuntime {
SCRIPT_THREAD_ROOT.with(|root| {
let script_thread = unsafe { &*root.get().unwrap() };
@ -1201,12 +1211,8 @@ impl ScriptThread {
},
};
match window.layout_chan() {
Some(chan) => chan
.send(Msg::RegisterPaint(name, properties, painter))
.unwrap(),
None => warn!("Layout channel unavailable"),
}
let _ = window
.with_layout(|layout| layout.process(Msg::RegisterPaint(name, properties, painter)));
}
pub fn push_new_element_queue() {
@ -1292,6 +1298,8 @@ impl ScriptThread {
state: InitialScriptState,
port: Receiver<MainThreadScriptMsg>,
chan: Sender<MainThreadScriptMsg>,
layout_factory: Arc<dyn LayoutFactory>,
font_cache_thread: FontCacheThread,
user_agent: Cow<'static, str>,
) -> ScriptThread {
let opts = opts::get();
@ -1351,7 +1359,6 @@ impl ScriptThread {
task_queue,
background_hang_monitor_register: state.background_hang_monitor_register,
background_hang_monitor,
closing,
@ -1394,6 +1401,7 @@ impl ScriptThread {
mutation_observers: Default::default(),
layout_to_constellation_chan: state.layout_to_constellation_chan,
font_cache_thread,
webgl_chan: state.webgl_chan,
webxr_registry: state.webxr_registry,
@ -1426,6 +1434,8 @@ impl ScriptThread {
gpu_id_hub: Arc::new(Mutex::new(Identities::new())),
webgpu_port: RefCell::new(None),
inherited_secure_context: state.inherited_secure_context,
layouts: Default::default(),
layout_factory,
}
}
@ -1847,6 +1857,8 @@ impl ScriptThread {
ExitFullScreen(id, ..) => Some(id),
MediaSessionAction(..) => None,
SetWebGPUPort(..) => None,
ForLayoutFromConstellation(_, id) => Some(id),
ForLayoutFromFontCache(id) => Some(id),
},
MixedMessage::FromDevtools(_) => None,
MixedMessage::FromScript(ref inner_msg) => match *inner_msg {
@ -2081,9 +2093,23 @@ impl ScriptThread {
msg @ ConstellationControlMsg::ExitScriptThread => {
panic!("should have handled {:?} already", msg)
},
ConstellationControlMsg::ForLayoutFromConstellation(msg, pipeline_id) => {
self.handle_layout_message(msg, pipeline_id)
},
ConstellationControlMsg::ForLayoutFromFontCache(pipeline_id) => {
self.handle_font_cache(pipeline_id)
},
}
}
fn handle_layout_message(&self, msg: LayoutControlMsg, pipeline_id: PipelineId) {
let _ = Self::with_layout(pipeline_id, |layout| layout.handle_constellation_msg(msg));
}
fn handle_font_cache(&self, pipeline_id: PipelineId) {
let _ = Self::with_layout(pipeline_id, |layout| layout.handle_font_cache_msg());
}
fn handle_msg_from_webgpu_server(&self, msg: WebGPUMsg) {
match msg {
WebGPUMsg::FreeAdapter(id) => self.gpu_id_hub.lock().kill_adapter_id(id),
@ -2509,55 +2535,8 @@ impl ScriptThread {
opener,
load_data,
window_size,
pipeline_port,
} = new_layout_info;
let layout_pair = unbounded();
let layout_chan = layout_pair.0.clone();
let layout_is_busy = Arc::new(AtomicBool::new(false));
let msg = message::Msg::CreateLayoutThread(LayoutThreadInit {
id: new_pipeline_id,
url: load_data.url.clone(),
is_parent: false,
layout_pair: layout_pair,
pipeline_port: pipeline_port,
background_hang_monitor_register: self.background_hang_monitor_register.clone(),
constellation_chan: self.layout_to_constellation_chan.clone(),
script_chan: self.control_chan.clone(),
image_cache: self.image_cache.clone(),
paint_time_metrics: PaintTimeMetrics::new(
new_pipeline_id,
self.time_profiler_chan.clone(),
self.layout_to_constellation_chan.clone(),
self.control_chan.clone(),
load_data.url.clone(),
),
layout_is_busy: layout_is_busy.clone(),
window_size,
});
// Pick a layout thread, any layout thread
let current_layout_chan: Option<Sender<Msg>> = self
.documents
.borrow()
.iter()
.next()
.and_then(|(_, document)| document.window().layout_chan().cloned())
.or_else(|| {
self.incomplete_loads
.borrow()
.first()
.map(|load| load.layout_chan.clone())
});
match current_layout_chan {
None => panic!("Layout attached to empty script thread."),
// Tell the layout thread factory to actually spawn the thread.
Some(layout_chan) => layout_chan.send(msg).unwrap(),
};
// Kick off the fetch for the new resource.
let new_load = InProgressLoad::new(
new_pipeline_id,
@ -2565,11 +2544,9 @@ impl ScriptThread {
top_level_browsing_context_id,
parent_info,
opener,
layout_chan,
window_size,
load_data.url.clone(),
origin,
layout_is_busy.clone(),
load_data.inherited_secure_context.clone(),
);
if load_data.url.as_str() == "about:blank" {
@ -2926,60 +2903,37 @@ impl ScriptThread {
/// Handles a request to exit a pipeline and shut down layout.
fn handle_exit_pipeline_msg(&self, id: PipelineId, discard_bc: DiscardBrowsingContext) {
debug!("Exiting pipeline {}.", id);
debug!("{id}: Starting pipeline exit.");
self.closed_pipelines.borrow_mut().insert(id);
// Check if the exit message is for an in progress load.
let idx = self
.incomplete_loads
.borrow()
.iter()
.position(|load| load.pipeline_id == id);
let document = self.documents.borrow_mut().remove(id);
// Abort the parser, if any,
// to prevent any further incoming networking messages from being handled.
if let Some(document) = document.as_ref() {
let document = self.documents.borrow_mut().remove(id);
if let Some(document) = document {
// We should never have a pipeline that's still an incomplete load, but also has a Document.
debug_assert!(!self
.incomplete_loads
.borrow()
.iter()
.any(|load| load.pipeline_id == id));
if let Some(parser) = document.get_current_parser() {
parser.abort();
}
}
// We should never have a pipeline that's still an incomplete load,
// but also has a Document.
debug_assert!(idx.is_none() || document.is_none());
debug!("{id}: Shutting down layout");
let _ = document.window().with_layout(|layout| {
layout.process(Msg::ExitNow);
});
// Remove any incomplete load.
let chan = if let Some(idx) = idx {
let load = self.incomplete_loads.borrow_mut().remove(idx);
load.layout_chan.clone()
} else if let Some(ref document) = document {
match document.window().layout_chan() {
Some(chan) => chan.clone(),
None => return warn!("Layout channel unavailable"),
}
} else {
return warn!("Exiting nonexistant pipeline {}.", id);
};
debug!("{id}: Sending PipelineExited message to constellation");
self.script_sender
.send((id, ScriptMsg::PipelineExited))
.ok();
// We shut down layout before removing the document,
// since layout might still be in the middle of laying it out.
debug!("preparing to shut down layout for page {}", id);
let (response_chan, response_port) = unbounded();
chan.send(message::Msg::PrepareToExit(response_chan)).ok();
let _ = response_port.recv();
debug!("shutting down layout for page {}", id);
chan.send(message::Msg::ExitNow).ok();
self.script_sender
.send((id, ScriptMsg::PipelineExited))
.ok();
// Now that layout is shut down, it's OK to remove the document.
if let Some(document) = document {
// Clear any active animations and unroot all of the associated DOM objects.
debug!("{id}: Clearing animations");
document.animations().clear();
// We don't want to dispatch `mouseout` event pointing to non-existing element
@ -2995,10 +2949,12 @@ impl ScriptThread {
if discard_bc == DiscardBrowsingContext::Yes {
window.discard_browsing_context();
}
debug!("{id}: Clearing JavaScript runtime");
window.clear_js_runtime();
}
debug!("Exited pipeline {}.", id);
debug!("{id}: Finished pipeline exit");
}
/// Handles a request to exit the script thread and shut down layout.
@ -3045,7 +3001,7 @@ impl ScriptThread {
});
}
/// Handles when layout thread finishes all animation in one tick
/// Handles when layout finishes all animation in one tick
fn handle_tick_all_animations(&self, id: PipelineId, tick_type: AnimationTickType) {
let document = match self.documents.borrow().find_document(id) {
Some(document) => document,
@ -3255,13 +3211,6 @@ impl ScriptThread {
fn load(&self, metadata: Metadata, incomplete: InProgressLoad) -> DomRoot<ServoParser> {
let final_url = metadata.final_url.clone();
{
// send the final url to the layout thread.
incomplete
.layout_chan
.send(message::Msg::SetFinalUrl(final_url.clone()))
.unwrap();
// update the pipeline url
self.script_sender
.send((
incomplete.pipeline_id,
@ -3304,6 +3253,33 @@ impl ScriptThread {
self.websocket_task_source(incomplete.pipeline_id),
);
let paint_time_metrics = PaintTimeMetrics::new(
incomplete.pipeline_id,
self.time_profiler_chan.clone(),
self.layout_to_constellation_chan.clone(),
self.control_chan.clone(),
final_url.clone(),
incomplete.navigation_start_precise,
);
let layout_config = LayoutConfig {
id: incomplete.pipeline_id,
url: final_url.clone(),
is_iframe: incomplete.parent_info.is_some(),
constellation_chan: self.layout_to_constellation_chan.clone(),
script_chan: self.control_chan.clone(),
image_cache: self.image_cache.clone(),
font_cache_thread: self.font_cache_thread.clone(),
time_profiler_chan: self.time_profiler_chan.clone(),
webrender_api_sender: self.webrender_api_sender.clone(),
paint_time_metrics,
window_size: incomplete.window_size.clone(),
};
self.layouts.borrow_mut().insert(
incomplete.pipeline_id,
self.layout_factory.create(layout_config),
);
// Create the window and document objects.
let window = Window::new(
self.js_runtime.clone(),
@ -3319,7 +3295,6 @@ impl ScriptThread {
script_to_constellation_chan,
self.control_chan.clone(),
self.scheduler_chan.clone(),
incomplete.layout_chan,
incomplete.pipeline_id,
incomplete.parent_info,
incomplete.window_size,
@ -3332,7 +3307,6 @@ impl ScriptThread {
self.microtask_queue.clone(),
self.webrender_document,
self.webrender_api_sender.clone(),
incomplete.layout_is_busy,
self.relayout_event,
self.prepare_for_screenshot,
self.unminify_js,