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

@ -18,14 +18,13 @@ pub mod webdriver_msg;
use std::borrow::Cow;
use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use bitflags::bitflags;
use bluetooth_traits::BluetoothRequest;
use canvas_traits::webgl::WebGLPipeline;
use compositor::ScrollTreeNodeId;
use crossbeam_channel::{Receiver, RecvTimeoutError, Sender};
use crossbeam_channel::{RecvTimeoutError, Sender};
use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg, WorkerId};
use embedder_traits::{CompositorEventVariant, Cursor};
use euclid::default::Point2D;
@ -110,19 +109,14 @@ impl UntrustedNodeAddress {
}
}
/// Messages sent to the layout thread from the constellation and/or compositor.
/// Messages sent to layout from the constellation and/or compositor.
#[derive(Debug, Deserialize, Serialize)]
pub enum LayoutControlMsg {
/// Requests that this layout thread exit.
/// Requests that this layout clean up before exit.
ExitNow,
/// Requests the current epoch (layout counter) from this layout.
GetCurrentEpoch(IpcSender<Epoch>),
/// Tells layout about the new scrolling offsets of each scrollable stacking context.
SetScrollStates(Vec<ScrollState>),
/// Requests the current load state of Web fonts. `true` is returned if fonts are still loading
/// and `false` is returned if all fonts have loaded.
GetWebFontLoadState(IpcSender<bool>),
/// Send the paint time for a specific epoch to the layout thread.
/// Send the paint time for a specific epoch to layout.
PaintMetric(Epoch, u64),
}
@ -233,8 +227,6 @@ pub struct NewLayoutInfo {
pub load_data: LoadData,
/// Information about the initial window size.
pub window_size: WindowSizeData,
/// A port on which layout can receive messages from the pipeline.
pub pipeline_port: IpcReceiver<LayoutControlMsg>,
}
/// When a pipeline is closed, should its browsing context be discarded too?
@ -293,7 +285,7 @@ pub enum ConstellationControlMsg {
/// Sends the final response to script thread for fetching after all redirections
/// have been resolved
NavigationResponse(PipelineId, FetchResponseMsg),
/// Gives a channel and ID to a layout thread, as well as the ID of that layout's parent
/// Gives a channel and ID to a layout, as well as the ID of that layout's parent
AttachLayout(NewLayoutInfo),
/// Window resized. Sends a DOM event eventually, but first we combine events.
Resize(PipelineId, WindowSizeData, WindowSizeType),
@ -401,6 +393,10 @@ pub enum ConstellationControlMsg {
MediaSessionAction(PipelineId, MediaSessionActionType),
/// Notifies script thread that WebGPU server has started
SetWebGPUPort(IpcReceiver<WebGPUMsg>),
/// A mesage for a layout from the constellation.
ForLayoutFromConstellation(LayoutControlMsg, PipelineId),
/// A message for a layout from the font cache.
ForLayoutFromFontCache(PipelineId),
}
impl fmt::Debug for ConstellationControlMsg {
@ -439,6 +435,8 @@ impl fmt::Debug for ConstellationControlMsg {
ExitFullScreen(..) => "ExitFullScreen",
MediaSessionAction(..) => "MediaSessionAction",
SetWebGPUPort(..) => "SetWebGPUPort",
ForLayoutFromConstellation(..) => "ForLayoutFromConstellation",
ForLayoutFromFontCache(..) => "ForLayoutFromFontCache",
};
write!(formatter, "ConstellationControlMsg::{}", variant)
}
@ -664,7 +662,7 @@ pub struct InitialScriptState {
pub script_to_constellation_chan: ScriptToConstellationChan,
/// A handle to register script-(and associated layout-)threads for hang monitoring.
pub background_hang_monitor_register: Box<dyn BackgroundHangMonitorRegister>,
/// A sender for the layout thread to communicate to the constellation.
/// A sender layout to communicate to the constellation.
pub layout_to_constellation_chan: IpcSender<LayoutMsg>,
/// A channel to schedule timer events.
pub scheduler_chan: IpcSender<TimerSchedulerMsg>,
@ -694,25 +692,10 @@ pub struct InitialScriptState {
pub webrender_document: DocumentId,
/// FIXME(victor): The Webrender API sender in this constellation's pipeline
pub webrender_api_sender: WebrenderIpcSender,
/// Flag to indicate if the layout thread is busy handling a request.
pub layout_is_busy: Arc<AtomicBool>,
/// Application window's GL Context for Media player
pub player_context: WindowGLContext,
}
/// This trait allows creating a `ScriptThread` without depending on the `script`
/// crate.
pub trait ScriptThreadFactory {
/// Type of message sent from script to layout.
type Message;
/// Create a `ScriptThread`.
fn create(
state: InitialScriptState,
load_data: LoadData,
user_agent: Cow<'static, str>,
) -> (Sender<Self::Message>, Receiver<Self::Message>);
}
/// This trait allows creating a `ServiceWorkerManager` without depending on the `script`
/// crate.
pub trait ServiceWorkerManagerFactory {