Revert "compositor: Unify the cross process and in-process API (#36443)" (#36533)

This reverts commit 4c55104b36.

This commit introduced an issue where messages from script to the
compositor no longer woke up the embedder. There is a larger issue
here, but this change exacerbated it.

Fixes #36528.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-04-15 11:15:14 +02:00 committed by GitHub
parent f8b6b9f7b6
commit fe4306fc30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 215 additions and 95 deletions

View file

@ -19,7 +19,8 @@ use bitflags::bitflags;
use compositing_traits::display_list::{CompositorDisplayListInfo, HitTestInfo, ScrollTree}; use compositing_traits::display_list::{CompositorDisplayListInfo, HitTestInfo, ScrollTree};
use compositing_traits::rendering_context::RenderingContext; use compositing_traits::rendering_context::RenderingContext;
use compositing_traits::{ use compositing_traits::{
CompositionPipeline, CompositorMsg, ImageUpdate, RendererWebView, SendableFrameTree, CompositionPipeline, CompositorMsg, CompositorReceiver, CrossProcessCompositorMessage,
ImageUpdate, RendererWebView, SendableFrameTree,
}; };
use constellation_traits::{AnimationTickType, EmbedderToConstellationMessage, PaintMetricEvent}; use constellation_traits::{AnimationTickType, EmbedderToConstellationMessage, PaintMetricEvent};
use crossbeam_channel::Sender; use crossbeam_channel::Sender;
@ -30,7 +31,7 @@ use embedder_traits::{
}; };
use euclid::{Point2D, Rect, Scale, Size2D, Transform3D}; use euclid::{Point2D, Rect, Scale, Size2D, Transform3D};
use fnv::FnvHashMap; use fnv::FnvHashMap;
use ipc_channel::ipc::{self, IpcReceiver, IpcSharedMemory}; use ipc_channel::ipc::{self, IpcSharedMemory};
use libc::c_void; use libc::c_void;
use log::{debug, info, trace, warn}; use log::{debug, info, trace, warn};
use pixels::{CorsStatus, Image, ImageFrame, PixelFormat}; use pixels::{CorsStatus, Image, ImageFrame, PixelFormat};
@ -89,7 +90,7 @@ pub struct ServoRenderer {
shutdown_state: Rc<Cell<ShutdownState>>, shutdown_state: Rc<Cell<ShutdownState>>,
/// The port on which we receive messages. /// The port on which we receive messages.
compositor_receiver: IpcReceiver<CompositorMsg>, compositor_receiver: CompositorReceiver,
/// The channel on which messages can be sent to the constellation. /// The channel on which messages can be sent to the constellation.
pub(crate) constellation_sender: Sender<EmbedderToConstellationMessage>, pub(crate) constellation_sender: Sender<EmbedderToConstellationMessage>,
@ -466,8 +467,8 @@ impl IOCompositor {
.global .global
.borrow_mut() .borrow_mut()
.compositor_receiver .compositor_receiver
.try_recv() .try_recv_compositor_msg()
.is_ok() .is_some()
{} {}
// Tell the profiler, memory profiler, and scrolling timer to shut down. // Tell the profiler, memory profiler, and scrolling timer to shut down.
@ -629,14 +630,33 @@ impl IOCompositor {
webview.dispatch_input_event(InputEvent::MouseMove(MouseMoveEvent { point })); webview.dispatch_input_event(InputEvent::MouseMove(MouseMoveEvent { point }));
}, },
CompositorMsg::SendInitialTransaction(pipeline) => { CompositorMsg::CrossProcess(cross_proces_message) => {
self.handle_cross_process_message(cross_proces_message);
},
}
}
/// Accept messages from content processes that need to be relayed to the WebRender
/// instance in the parent process.
#[cfg_attr(
feature = "tracing",
tracing::instrument(skip_all, fields(servo_profiling = true), level = "trace")
)]
fn handle_cross_process_message(&mut self, msg: CrossProcessCompositorMessage) {
match msg {
CrossProcessCompositorMessage::SendInitialTransaction(pipeline) => {
let mut txn = Transaction::new(); let mut txn = Transaction::new();
txn.set_display_list(WebRenderEpoch(0), (pipeline, Default::default())); txn.set_display_list(WebRenderEpoch(0), (pipeline, Default::default()));
self.generate_frame(&mut txn, RenderReasons::SCENE); self.generate_frame(&mut txn, RenderReasons::SCENE);
self.global.borrow_mut().send_transaction(txn); self.global.borrow_mut().send_transaction(txn);
}, },
CompositorMsg::SendScrollNode(webview_id, pipeline_id, point, external_scroll_id) => { CrossProcessCompositorMessage::SendScrollNode(
webview_id,
pipeline_id,
point,
external_scroll_id,
) => {
let Some(webview) = self.webviews.get_mut(webview_id) else { let Some(webview) = self.webviews.get_mut(webview_id) else {
return; return;
}; };
@ -670,7 +690,7 @@ impl IOCompositor {
self.global.borrow_mut().send_transaction(txn); self.global.borrow_mut().send_transaction(txn);
}, },
CompositorMsg::SendDisplayList { CrossProcessCompositorMessage::SendDisplayList {
webview_id, webview_id,
display_list_descriptor, display_list_descriptor,
display_list_receiver, display_list_receiver,
@ -759,7 +779,7 @@ impl IOCompositor {
self.global.borrow_mut().send_transaction(transaction); self.global.borrow_mut().send_transaction(transaction);
}, },
CompositorMsg::HitTest(pipeline, point, flags, sender) => { CrossProcessCompositorMessage::HitTest(pipeline, point, flags, sender) => {
// When a display list is sent to WebRender, it starts scene building in a // When a display list is sent to WebRender, it starts scene building in a
// separate thread and then that display list is available for hit testing. // separate thread and then that display list is available for hit testing.
// Without flushing scene building, any hit test we do might be done against // Without flushing scene building, any hit test we do might be done against
@ -785,11 +805,11 @@ impl IOCompositor {
let _ = sender.send(result); let _ = sender.send(result);
}, },
CompositorMsg::GenerateImageKey(sender) => { CrossProcessCompositorMessage::GenerateImageKey(sender) => {
let _ = sender.send(self.global.borrow().webrender_api.generate_image_key()); let _ = sender.send(self.global.borrow().webrender_api.generate_image_key());
}, },
CompositorMsg::UpdateImages(updates) => { CrossProcessCompositorMessage::UpdateImages(updates) => {
let mut txn = Transaction::new(); let mut txn = Transaction::new();
for update in updates { for update in updates {
match update { match update {
@ -805,21 +825,26 @@ impl IOCompositor {
self.global.borrow_mut().send_transaction(txn); self.global.borrow_mut().send_transaction(txn);
}, },
CompositorMsg::AddFont(font_key, data, index) => { CrossProcessCompositorMessage::AddFont(font_key, data, index) => {
self.add_font(font_key, index, data); self.add_font(font_key, index, data);
}, },
CompositorMsg::AddSystemFont(font_key, native_handle) => { CrossProcessCompositorMessage::AddSystemFont(font_key, native_handle) => {
let mut transaction = Transaction::new(); let mut transaction = Transaction::new();
transaction.add_native_font(font_key, native_handle); transaction.add_native_font(font_key, native_handle);
self.global.borrow_mut().send_transaction(transaction); self.global.borrow_mut().send_transaction(transaction);
}, },
CompositorMsg::AddFontInstance(font_instance_key, font_key, size, flags) => { CrossProcessCompositorMessage::AddFontInstance(
font_instance_key,
font_key,
size,
flags,
) => {
self.add_font_instance(font_instance_key, font_key, size, flags); self.add_font_instance(font_instance_key, font_key, size, flags);
}, },
CompositorMsg::RemoveFonts(keys, instance_keys) => { CrossProcessCompositorMessage::RemoveFonts(keys, instance_keys) => {
let mut transaction = Transaction::new(); let mut transaction = Transaction::new();
for instance in instance_keys.into_iter() { for instance in instance_keys.into_iter() {
@ -832,13 +857,13 @@ impl IOCompositor {
self.global.borrow_mut().send_transaction(transaction); self.global.borrow_mut().send_transaction(transaction);
}, },
CompositorMsg::AddImage(key, desc, data) => { CrossProcessCompositorMessage::AddImage(key, desc, data) => {
let mut txn = Transaction::new(); let mut txn = Transaction::new();
txn.add_image(key, desc, data.into(), None); txn.add_image(key, desc, data.into(), None);
self.global.borrow_mut().send_transaction(txn); self.global.borrow_mut().send_transaction(txn);
}, },
CompositorMsg::GenerateFontKeys( CrossProcessCompositorMessage::GenerateFontKeys(
number_of_font_keys, number_of_font_keys,
number_of_font_instance_keys, number_of_font_instance_keys,
result_sender, result_sender,
@ -856,7 +881,7 @@ impl IOCompositor {
.collect(); .collect();
let _ = result_sender.send((font_keys, font_instance_keys)); let _ = result_sender.send((font_keys, font_instance_keys));
}, },
CompositorMsg::GetClientWindowRect(webview_id, response_sender) => { CrossProcessCompositorMessage::GetClientWindowRect(webview_id, response_sender) => {
let client_window_rect = self let client_window_rect = self
.webviews .webviews
.get(webview_id) .get(webview_id)
@ -866,7 +891,7 @@ impl IOCompositor {
warn!("Sending response to get client window failed ({error:?})."); warn!("Sending response to get client window failed ({error:?}).");
} }
}, },
CompositorMsg::GetScreenSize(webview_id, response_sender) => { CrossProcessCompositorMessage::GetScreenSize(webview_id, response_sender) => {
let screen_size = self let screen_size = self
.webviews .webviews
.get(webview_id) .get(webview_id)
@ -876,7 +901,7 @@ impl IOCompositor {
warn!("Sending response to get screen size failed ({error:?})."); warn!("Sending response to get screen size failed ({error:?}).");
} }
}, },
CompositorMsg::GetAvailableScreenSize(webview_id, response_sender) => { CrossProcessCompositorMessage::GetAvailableScreenSize(webview_id, response_sender) => {
let available_screen_size = self let available_screen_size = self
.webviews .webviews
.get(webview_id) .get(webview_id)
@ -910,14 +935,16 @@ impl IOCompositor {
} }
let _ = sender.send(()); let _ = sender.send(());
}, },
CompositorMsg::GenerateImageKey(sender) => { CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GenerateImageKey(
sender,
)) => {
let _ = sender.send(self.global.borrow().webrender_api.generate_image_key()); let _ = sender.send(self.global.borrow().webrender_api.generate_image_key());
}, },
CompositorMsg::GenerateFontKeys( CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GenerateFontKeys(
number_of_font_keys, number_of_font_keys,
number_of_font_instance_keys, number_of_font_instance_keys,
result_sender, result_sender,
) => { )) => {
let font_keys = (0..number_of_font_keys) let font_keys = (0..number_of_font_keys)
.map(|_| self.global.borrow().webrender_api.generate_font_key()) .map(|_| self.global.borrow().webrender_api.generate_font_key())
.collect(); .collect();
@ -931,17 +958,26 @@ impl IOCompositor {
.collect(); .collect();
let _ = result_sender.send((font_keys, font_instance_keys)); let _ = result_sender.send((font_keys, font_instance_keys));
}, },
CompositorMsg::GetClientWindowRect(_, response_sender) => { CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GetClientWindowRect(
_,
response_sender,
)) => {
if let Err(error) = response_sender.send(Default::default()) { if let Err(error) = response_sender.send(Default::default()) {
warn!("Sending response to get client window failed ({error:?})."); warn!("Sending response to get client window failed ({error:?}).");
} }
}, },
CompositorMsg::GetScreenSize(_, response_sender) => { CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GetScreenSize(
_,
response_sender,
)) => {
if let Err(error) = response_sender.send(Default::default()) { if let Err(error) = response_sender.send(Default::default()) {
warn!("Sending response to get client window failed ({error:?})."); warn!("Sending response to get client window failed ({error:?}).");
} }
}, },
CompositorMsg::GetAvailableScreenSize(_, response_sender) => { CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GetAvailableScreenSize(
_,
response_sender,
)) => {
if let Err(error) = response_sender.send(Default::default()) { if let Err(error) = response_sender.send(Default::default()) {
warn!("Sending response to get client window failed ({error:?})."); warn!("Sending response to get client window failed ({error:?}).");
} }
@ -1564,7 +1600,12 @@ impl IOCompositor {
// Check for new messages coming from the other threads in the system. // Check for new messages coming from the other threads in the system.
let mut compositor_messages = vec![]; let mut compositor_messages = vec![];
let mut found_recomposite_msg = false; let mut found_recomposite_msg = false;
while let Ok(msg) = self.global.borrow_mut().compositor_receiver.try_recv() { while let Some(msg) = self
.global
.borrow_mut()
.compositor_receiver
.try_recv_compositor_msg()
{
match msg { match msg {
CompositorMsg::NewWebRenderFrameReady(..) if found_recomposite_msg => { CompositorMsg::NewWebRenderFrameReady(..) if found_recomposite_msg => {
// Only take one of duplicate NewWebRendeFrameReady messages, but do subtract // Only take one of duplicate NewWebRendeFrameReady messages, but do subtract

View file

@ -8,11 +8,10 @@ use std::cell::Cell;
use std::rc::Rc; use std::rc::Rc;
use compositing_traits::rendering_context::RenderingContext; use compositing_traits::rendering_context::RenderingContext;
use compositing_traits::{CompositorMsg, CompositorProxy}; use compositing_traits::{CompositorProxy, CompositorReceiver};
use constellation_traits::EmbedderToConstellationMessage; use constellation_traits::EmbedderToConstellationMessage;
use crossbeam_channel::Sender; use crossbeam_channel::Sender;
use embedder_traits::ShutdownState; use embedder_traits::ShutdownState;
use ipc_channel::ipc::IpcReceiver;
use profile_traits::{mem, time}; use profile_traits::{mem, time};
use webrender::RenderApi; use webrender::RenderApi;
use webrender_api::DocumentId; use webrender_api::DocumentId;
@ -33,7 +32,7 @@ pub struct InitialCompositorState {
/// A channel to the compositor. /// A channel to the compositor.
pub sender: CompositorProxy, pub sender: CompositorProxy,
/// A port on which messages inbound to the compositor can be received. /// A port on which messages inbound to the compositor can be received.
pub receiver: IpcReceiver<CompositorMsg>, pub receiver: CompositorReceiver,
/// A channel to the constellation. /// A channel to the constellation.
pub constellation_chan: Sender<EmbedderToConstellationMessage>, pub constellation_chan: Sender<EmbedderToConstellationMessage>,
/// A channel to the time profiler thread. /// A channel to the time profiler thread.

View file

@ -42,21 +42,7 @@ mod from_constellation {
Self::LoadComplete(..) => target!("LoadComplete"), Self::LoadComplete(..) => target!("LoadComplete"),
Self::WebDriverMouseButtonEvent(..) => target!("WebDriverMouseButtonEvent"), Self::WebDriverMouseButtonEvent(..) => target!("WebDriverMouseButtonEvent"),
Self::WebDriverMouseMoveEvent(..) => target!("WebDriverMouseMoveEvent"), Self::WebDriverMouseMoveEvent(..) => target!("WebDriverMouseMoveEvent"),
Self::SendInitialTransaction(..) => target!("SendInitialTransaction"), Self::CrossProcess(_) => target!("CrossProcess"),
Self::SendScrollNode(..) => target!("SendScrollNode"),
Self::SendDisplayList { .. } => target!("SendDisplayList"),
Self::HitTest(..) => target!("HitTest"),
Self::GenerateImageKey(..) => target!("GenerateImageKey"),
Self::AddImage(..) => target!("AddImage"),
Self::UpdateImages(..) => target!("UpdateImages"),
Self::GenerateFontKeys(..) => target!("GenerateFontKeys"),
Self::AddFont(..) => target!("AddFont"),
Self::AddSystemFont(..) => target!("AddSystemFont"),
Self::AddFontInstance(..) => target!("AddFontInstance"),
Self::RemoveFonts(..) => target!("RemoveFonts"),
Self::GetClientWindowRect(..) => target!("GetClientWindowRect"),
Self::GetScreenSize(..) => target!("GetScreenSize"),
Self::GetAvailableScreenSize(..) => target!("GetAvailableScreenSize"),
} }
} }
} }

View file

@ -291,7 +291,8 @@ impl Pipeline {
webrender_document: state.webrender_document, webrender_document: state.webrender_document,
cross_process_compositor_api: state cross_process_compositor_api: state
.compositor_proxy .compositor_proxy
.cross_process_compositor_api(), .cross_process_compositor_api
.clone(),
webgl_chan: state.webgl_chan, webgl_chan: state.webgl_chan,
webxr_registry: state.webxr_registry, webxr_registry: state.webxr_registry,
player_context: state.player_context, player_context: state.player_context,

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use compositing_traits::CompositorMsg; use compositing_traits::CrossProcessCompositorMessage;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use euclid::Size2D; use euclid::Size2D;
use profile_traits::ipc; use profile_traits::ipc;
@ -41,7 +41,7 @@ impl Screen {
self.window self.window
.compositor_api() .compositor_api()
.sender() .sender()
.send(CompositorMsg::GetScreenSize( .send(CrossProcessCompositorMessage::GetScreenSize(
self.window.webview_id(), self.window.webview_id(),
sender, sender,
)) ))
@ -57,7 +57,7 @@ impl Screen {
self.window self.window
.compositor_api() .compositor_api()
.sender() .sender()
.send(CompositorMsg::GetAvailableScreenSize( .send(CrossProcessCompositorMessage::GetAvailableScreenSize(
self.window.webview_id(), self.window.webview_id(),
sender, sender,
)) ))

View file

@ -1899,7 +1899,10 @@ impl Window {
let (sender, receiver) = let (sender, receiver) =
ProfiledIpc::channel::<DeviceIndependentIntRect>(timer_profile_chan).unwrap(); ProfiledIpc::channel::<DeviceIndependentIntRect>(timer_profile_chan).unwrap();
let _ = self.compositor_api.sender().send( let _ = self.compositor_api.sender().send(
compositing_traits::CompositorMsg::GetClientWindowRect(self.webview_id(), sender), compositing_traits::CrossProcessCompositorMessage::GetClientWindowRect(
self.webview_id(),
sender,
),
); );
let rect = receiver.recv().unwrap_or_default(); let rect = receiver.recv().unwrap_or_default();
( (

View file

@ -48,8 +48,8 @@ pub use compositing_traits::rendering_context::{
OffscreenRenderingContext, RenderingContext, SoftwareRenderingContext, WindowRenderingContext, OffscreenRenderingContext, RenderingContext, SoftwareRenderingContext, WindowRenderingContext,
}; };
use compositing_traits::{ use compositing_traits::{
CompositorMsg, CompositorProxy, WebrenderExternalImageHandlers, WebrenderExternalImageRegistry, CompositorMsg, CompositorProxy, CompositorReceiver, CrossProcessCompositorApi,
WebrenderImageHandlerType, WebrenderExternalImageHandlers, WebrenderExternalImageRegistry, WebrenderImageHandlerType,
}; };
#[cfg(all( #[cfg(all(
not(target_os = "windows"), not(target_os = "windows"),
@ -82,6 +82,7 @@ use gaol::sandbox::{ChildSandbox, ChildSandboxMethods};
pub use gleam::gl; pub use gleam::gl;
use gleam::gl::RENDERER; use gleam::gl::RENDERER;
use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
pub use keyboard_types::*; pub use keyboard_types::*;
use log::{Log, Metadata, Record, debug, warn}; use log::{Log, Metadata, Record, debug, warn};
use media::{GlApi, NativeDisplay, WindowGLContext}; use media::{GlApi, NativeDisplay, WindowGLContext};
@ -297,6 +298,8 @@ impl Servo {
// messages to client may need to pump a platform-specific event loop // messages to client may need to pump a platform-specific event loop
// to deliver the message. // to deliver the message.
let event_loop_waker = embedder.create_event_loop_waker(); let event_loop_waker = embedder.create_event_loop_waker();
let (compositor_proxy, compositor_receiver) =
create_compositor_channel(event_loop_waker.clone());
let (embedder_proxy, embedder_receiver) = create_embedder_channel(event_loop_waker.clone()); let (embedder_proxy, embedder_receiver) = create_embedder_channel(event_loop_waker.clone());
let time_profiler_chan = profile_time::Profiler::create( let time_profiler_chan = profile_time::Profiler::create(
&opts.time_profiling, &opts.time_profiling,
@ -304,12 +307,6 @@ impl Servo {
); );
let mem_profiler_chan = profile_mem::Profiler::create(); let mem_profiler_chan = profile_mem::Profiler::create();
let (compositor_sender, compositor_receiver) = ipc::channel().expect("ipc channel failure");
let compositor_proxy = CompositorProxy {
sender: compositor_sender,
event_loop_waker: event_loop_waker.clone(),
};
let devtools_sender = if pref!(devtools_server_enabled) { let devtools_sender = if pref!(devtools_server_enabled) {
Some(devtools::start_server( Some(devtools::start_server(
pref!(devtools_server_port) as u16, pref!(devtools_server_port) as u16,
@ -993,6 +990,34 @@ fn create_embedder_channel(
) )
} }
fn create_compositor_channel(
event_loop_waker: Box<dyn EventLoopWaker>,
) -> (CompositorProxy, CompositorReceiver) {
let (sender, receiver) = unbounded();
let (compositor_ipc_sender, compositor_ipc_receiver) =
ipc::channel().expect("ipc channel failure");
let cross_process_compositor_api = CrossProcessCompositorApi(compositor_ipc_sender);
let compositor_proxy = CompositorProxy {
sender,
cross_process_compositor_api,
event_loop_waker,
};
let compositor_proxy_clone = compositor_proxy.clone();
ROUTER.add_typed_route(
compositor_ipc_receiver,
Box::new(move |message| {
compositor_proxy_clone.send(CompositorMsg::CrossProcess(
message.expect("Could not convert Compositor message"),
));
}),
);
(compositor_proxy, CompositorReceiver { receiver })
}
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn create_constellation( fn create_constellation(
config_dir: Option<PathBuf>, config_dir: Option<PathBuf>,
@ -1029,11 +1054,11 @@ fn create_constellation(
); );
let system_font_service = Arc::new( let system_font_service = Arc::new(
SystemFontService::spawn(compositor_proxy.cross_process_compositor_api()).to_proxy(), SystemFontService::spawn(compositor_proxy.cross_process_compositor_api.clone()).to_proxy(),
); );
let (canvas_create_sender, canvas_ipc_sender) = CanvasPaintThread::start( let (canvas_create_sender, canvas_ipc_sender) = CanvasPaintThread::start(
compositor_proxy.cross_process_compositor_api(), compositor_proxy.cross_process_compositor_api.clone(),
system_font_service.clone(), system_font_service.clone(),
public_resource_threads.clone(), public_resource_threads.clone(),
); );

View file

@ -7,6 +7,7 @@
use std::fmt::{Debug, Error, Formatter}; use std::fmt::{Debug, Error, Formatter};
use base::id::{PipelineId, WebViewId}; use base::id::{PipelineId, WebViewId};
use crossbeam_channel::{Receiver, Sender};
use embedder_traits::{ use embedder_traits::{
AnimationState, EventLoopWaker, MouseButton, MouseButtonAction, TouchEventResult, AnimationState, EventLoopWaker, MouseButton, MouseButtonAction, TouchEventResult,
}; };
@ -21,6 +22,7 @@ use webrender_api::DocumentId;
pub mod display_list; pub mod display_list;
pub mod rendering_context; pub mod rendering_context;
use core::fmt;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -41,7 +43,11 @@ use webrender_api::{
/// Sends messages to the compositor. /// Sends messages to the compositor.
#[derive(Clone)] #[derive(Clone)]
pub struct CompositorProxy { pub struct CompositorProxy {
pub sender: IpcSender<CompositorMsg>, pub sender: Sender<CompositorMsg>,
/// Access to [`Self::sender`] that is possible to send across an IPC
/// channel. These messages are routed via the router thread to
/// [`Self::sender`].
pub cross_process_compositor_api: CrossProcessCompositorApi,
pub event_loop_waker: Box<dyn EventLoopWaker>, pub event_loop_waker: Box<dyn EventLoopWaker>,
} }
@ -52,14 +58,24 @@ impl CompositorProxy {
} }
self.event_loop_waker.wake(); self.event_loop_waker.wake();
} }
}
pub fn cross_process_compositor_api(&self) -> CrossProcessCompositorApi { /// The port that the compositor receives messages on.
CrossProcessCompositorApi(self.sender.clone()) pub struct CompositorReceiver {
pub receiver: Receiver<CompositorMsg>,
}
impl CompositorReceiver {
pub fn try_recv_compositor_msg(&mut self) -> Option<CompositorMsg> {
self.receiver.try_recv().ok()
}
pub fn recv_compositor_msg(&mut self) -> CompositorMsg {
self.receiver.recv().unwrap()
} }
} }
/// Messages from (or via) the constellation thread to the compositor. /// Messages from (or via) the constellation thread to the compositor.
#[derive(Deserialize, IntoStaticStr, Serialize)] #[derive(IntoStaticStr)]
pub enum CompositorMsg { pub enum CompositorMsg {
/// Alerts the compositor that the given pipeline has changed whether it is running animations. /// Alerts the compositor that the given pipeline has changed whether it is running animations.
ChangeRunningAnimationsState(WebViewId, PipelineId, AnimationState), ChangeRunningAnimationsState(WebViewId, PipelineId, AnimationState),
@ -96,6 +112,32 @@ pub enum CompositorMsg {
/// WebDriver mouse move event /// WebDriver mouse move event
WebDriverMouseMoveEvent(WebViewId, f32, f32), WebDriverMouseMoveEvent(WebViewId, f32, f32),
/// Messages forwarded to the compositor by the constellation from other crates. These
/// messages are mainly passed on from the compositor to WebRender.
CrossProcess(CrossProcessCompositorMessage),
}
pub struct SendableFrameTree {
pub pipeline: CompositionPipeline,
pub children: Vec<SendableFrameTree>,
}
/// The subset of the pipeline that is needed for layer composition.
#[derive(Clone)]
pub struct CompositionPipeline {
pub id: PipelineId,
pub webview_id: WebViewId,
}
impl Debug for CompositorMsg {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
let string: &'static str = self.into();
write!(formatter, "{string}")
}
}
#[derive(Deserialize, Serialize)]
pub enum CrossProcessCompositorMessage {
/// Inform WebRender of the existence of this pipeline. /// Inform WebRender of the existence of this pipeline.
SendInitialTransaction(WebRenderPipelineId), SendInitialTransaction(WebRenderPipelineId),
/// Perform a scroll operation. /// Perform a scroll operation.
@ -155,29 +197,31 @@ pub enum CompositorMsg {
GetAvailableScreenSize(WebViewId, IpcSender<DeviceIndependentIntSize>), GetAvailableScreenSize(WebViewId, IpcSender<DeviceIndependentIntSize>),
} }
impl Debug for CompositorMsg { impl fmt::Debug for CrossProcessCompositorMessage {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let string: &'static str = self.into(); match self {
write!(formatter, "{string}") Self::AddImage(..) => f.write_str("AddImage"),
Self::GenerateFontKeys(..) => f.write_str("GenerateFontKeys"),
Self::AddSystemFont(..) => f.write_str("AddSystemFont"),
Self::SendInitialTransaction(..) => f.write_str("SendInitialTransaction"),
Self::SendScrollNode(..) => f.write_str("SendScrollNode"),
Self::SendDisplayList { .. } => f.write_str("SendDisplayList"),
Self::HitTest(..) => f.write_str("HitTest"),
Self::GenerateImageKey(..) => f.write_str("GenerateImageKey"),
Self::UpdateImages(..) => f.write_str("UpdateImages"),
Self::RemoveFonts(..) => f.write_str("RemoveFonts"),
Self::AddFontInstance(..) => f.write_str("AddFontInstance"),
Self::AddFont(..) => f.write_str("AddFont"),
Self::GetClientWindowRect(..) => f.write_str("GetClientWindowRect"),
Self::GetScreenSize(..) => f.write_str("GetScreenSize"),
Self::GetAvailableScreenSize(..) => f.write_str("GetAvailableScreenSize"),
}
} }
}
#[derive(Deserialize, Serialize)]
pub struct SendableFrameTree {
pub pipeline: CompositionPipeline,
pub children: Vec<SendableFrameTree>,
}
/// The subset of the pipeline that is needed for layer composition.
#[derive(Clone, Deserialize, Serialize)]
pub struct CompositionPipeline {
pub id: PipelineId,
pub webview_id: WebViewId,
} }
/// A mechanism to send messages from ScriptThread to the parent process' WebRender instance. /// A mechanism to send messages from ScriptThread to the parent process' WebRender instance.
#[derive(Clone, Deserialize, Serialize)] #[derive(Clone, Deserialize, Serialize)]
pub struct CrossProcessCompositorApi(pub IpcSender<CompositorMsg>); pub struct CrossProcessCompositorApi(pub IpcSender<CrossProcessCompositorMessage>);
impl CrossProcessCompositorApi { impl CrossProcessCompositorApi {
/// Create a new [`CrossProcessCompositorApi`] struct that does not have a listener on the other /// Create a new [`CrossProcessCompositorApi`] struct that does not have a listener on the other
@ -188,13 +232,18 @@ impl CrossProcessCompositorApi {
} }
/// Get the sender for this proxy. /// Get the sender for this proxy.
pub fn sender(&self) -> &IpcSender<CompositorMsg> { pub fn sender(&self) -> &IpcSender<CrossProcessCompositorMessage> {
&self.0 &self.0
} }
/// Inform WebRender of the existence of this pipeline. /// Inform WebRender of the existence of this pipeline.
pub fn send_initial_transaction(&self, pipeline: WebRenderPipelineId) { pub fn send_initial_transaction(&self, pipeline: WebRenderPipelineId) {
if let Err(e) = self.0.send(CompositorMsg::SendInitialTransaction(pipeline)) { if let Err(e) = self
.0
.send(CrossProcessCompositorMessage::SendInitialTransaction(
pipeline,
))
{
warn!("Error sending initial transaction: {}", e); warn!("Error sending initial transaction: {}", e);
} }
} }
@ -207,7 +256,7 @@ impl CrossProcessCompositorApi {
point: LayoutPoint, point: LayoutPoint,
scroll_id: ExternalScrollId, scroll_id: ExternalScrollId,
) { ) {
if let Err(e) = self.0.send(CompositorMsg::SendScrollNode( if let Err(e) = self.0.send(CrossProcessCompositorMessage::SendScrollNode(
webview_id, webview_id,
pipeline_id, pipeline_id,
point, point,
@ -226,7 +275,7 @@ impl CrossProcessCompositorApi {
) { ) {
let (display_list_data, display_list_descriptor) = list.into_data(); let (display_list_data, display_list_descriptor) = list.into_data();
let (display_list_sender, display_list_receiver) = ipc::bytes_channel().unwrap(); let (display_list_sender, display_list_receiver) = ipc::bytes_channel().unwrap();
if let Err(e) = self.0.send(CompositorMsg::SendDisplayList { if let Err(e) = self.0.send(CrossProcessCompositorMessage::SendDisplayList {
webview_id, webview_id,
display_list_descriptor, display_list_descriptor,
display_list_receiver, display_list_receiver,
@ -261,7 +310,9 @@ impl CrossProcessCompositorApi {
) -> Vec<CompositorHitTestResult> { ) -> Vec<CompositorHitTestResult> {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.0 self.0
.send(CompositorMsg::HitTest(pipeline, point, flags, sender)) .send(CrossProcessCompositorMessage::HitTest(
pipeline, point, flags, sender,
))
.expect("error sending hit test"); .expect("error sending hit test");
receiver.recv().expect("error receiving hit test result") receiver.recv().expect("error receiving hit test result")
} }
@ -269,7 +320,9 @@ impl CrossProcessCompositorApi {
/// Create a new image key. Blocks until the key is available. /// Create a new image key. Blocks until the key is available.
pub fn generate_image_key(&self) -> Option<ImageKey> { pub fn generate_image_key(&self) -> Option<ImageKey> {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.0.send(CompositorMsg::GenerateImageKey(sender)).ok()?; self.0
.send(CrossProcessCompositorMessage::GenerateImageKey(sender))
.ok()?;
receiver.recv().ok() receiver.recv().ok()
} }
@ -279,14 +332,19 @@ impl CrossProcessCompositorApi {
descriptor: ImageDescriptor, descriptor: ImageDescriptor,
data: SerializableImageData, data: SerializableImageData,
) { ) {
if let Err(e) = self.0.send(CompositorMsg::AddImage(key, descriptor, data)) { if let Err(e) = self.0.send(CrossProcessCompositorMessage::AddImage(
key, descriptor, data,
)) {
warn!("Error sending image update: {}", e); warn!("Error sending image update: {}", e);
} }
} }
/// Perform an image resource update operation. /// Perform an image resource update operation.
pub fn update_images(&self, updates: Vec<ImageUpdate>) { pub fn update_images(&self, updates: Vec<ImageUpdate>) {
if let Err(e) = self.0.send(CompositorMsg::UpdateImages(updates)) { if let Err(e) = self
.0
.send(CrossProcessCompositorMessage::UpdateImages(updates))
{
warn!("error sending image updates: {}", e); warn!("error sending image updates: {}", e);
} }
} }
@ -299,7 +357,10 @@ impl CrossProcessCompositorApi {
if keys.is_empty() && instance_keys.is_empty() { if keys.is_empty() && instance_keys.is_empty() {
return; return;
} }
let _ = self.0.send(CompositorMsg::RemoveFonts(keys, instance_keys)); let _ = self.0.send(CrossProcessCompositorMessage::RemoveFonts(
keys,
instance_keys,
));
} }
pub fn add_font_instance( pub fn add_font_instance(
@ -309,7 +370,7 @@ impl CrossProcessCompositorApi {
size: f32, size: f32,
flags: FontInstanceFlags, flags: FontInstanceFlags,
) { ) {
let _x = self.0.send(CompositorMsg::AddFontInstance( let _x = self.0.send(CrossProcessCompositorMessage::AddFontInstance(
font_instance_key, font_instance_key,
font_key, font_key,
size, size,
@ -318,11 +379,15 @@ impl CrossProcessCompositorApi {
} }
pub fn add_font(&self, font_key: FontKey, data: Arc<IpcSharedMemory>, index: u32) { pub fn add_font(&self, font_key: FontKey, data: Arc<IpcSharedMemory>, index: u32) {
let _ = self.0.send(CompositorMsg::AddFont(font_key, data, index)); let _ = self.0.send(CrossProcessCompositorMessage::AddFont(
font_key, data, index,
));
} }
pub fn add_system_font(&self, font_key: FontKey, handle: NativeFontHandle) { pub fn add_system_font(&self, font_key: FontKey, handle: NativeFontHandle) {
let _ = self.0.send(CompositorMsg::AddSystemFont(font_key, handle)); let _ = self.0.send(CrossProcessCompositorMessage::AddSystemFont(
font_key, handle,
));
} }
pub fn fetch_font_keys( pub fn fetch_font_keys(
@ -331,7 +396,7 @@ impl CrossProcessCompositorApi {
number_of_font_instance_keys: usize, number_of_font_instance_keys: usize,
) -> (Vec<FontKey>, Vec<FontInstanceKey>) { ) -> (Vec<FontKey>, Vec<FontInstanceKey>) {
let (sender, receiver) = ipc_channel::ipc::channel().expect("Could not create IPC channel"); let (sender, receiver) = ipc_channel::ipc::channel().expect("Could not create IPC channel");
let _ = self.0.send(CompositorMsg::GenerateFontKeys( let _ = self.0.send(CrossProcessCompositorMessage::GenerateFontKeys(
number_of_font_keys, number_of_font_keys,
number_of_font_instance_keys, number_of_font_instance_keys,
sender, sender,