diff --git a/components/compositing/compositor.rs b/components/compositing/compositor.rs index f4ecd82cbc1..461a5a819d5 100644 --- a/components/compositing/compositor.rs +++ b/components/compositing/compositor.rs @@ -19,11 +19,10 @@ use bitflags::bitflags; use compositing_traits::display_list::{CompositorDisplayListInfo, HitTestInfo, ScrollTree}; use compositing_traits::rendering_context::RenderingContext; use compositing_traits::{ - CompositionPipeline, CompositorMsg, CompositorReceiver, CrossProcessCompositorMessage, - ImageUpdate, RendererWebView, SendableFrameTree, + CompositionPipeline, CompositorMsg, ImageUpdate, RendererWebView, SendableFrameTree, }; use constellation_traits::{AnimationTickType, EmbedderToConstellationMessage, PaintMetricEvent}; -use crossbeam_channel::Sender; +use crossbeam_channel::{Receiver, Sender}; use dpi::PhysicalSize; use embedder_traits::{ AnimationState, CompositorHitTestResult, Cursor, InputEvent, MouseButtonEvent, MouseMoveEvent, @@ -90,7 +89,7 @@ pub struct ServoRenderer { shutdown_state: Rc>, /// The port on which we receive messages. - compositor_receiver: CompositorReceiver, + compositor_receiver: Receiver, /// The channel on which messages can be sent to the constellation. pub(crate) constellation_sender: Sender, @@ -467,8 +466,8 @@ impl IOCompositor { .global .borrow_mut() .compositor_receiver - .try_recv_compositor_msg() - .is_some() + .try_recv() + .is_ok() {} // Tell the profiler, memory profiler, and scrolling timer to shut down. @@ -630,33 +629,14 @@ impl IOCompositor { webview.dispatch_input_event(InputEvent::MouseMove(MouseMoveEvent { point })); }, - 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) => { + CompositorMsg::SendInitialTransaction(pipeline) => { let mut txn = Transaction::new(); txn.set_display_list(WebRenderEpoch(0), (pipeline, Default::default())); self.generate_frame(&mut txn, RenderReasons::SCENE); self.global.borrow_mut().send_transaction(txn); }, - CrossProcessCompositorMessage::SendScrollNode( - webview_id, - pipeline_id, - point, - external_scroll_id, - ) => { + CompositorMsg::SendScrollNode(webview_id, pipeline_id, point, external_scroll_id) => { let Some(webview) = self.webviews.get_mut(webview_id) else { return; }; @@ -690,7 +670,7 @@ impl IOCompositor { self.global.borrow_mut().send_transaction(txn); }, - CrossProcessCompositorMessage::SendDisplayList { + CompositorMsg::SendDisplayList { webview_id, display_list_descriptor, display_list_receiver, @@ -779,7 +759,7 @@ impl IOCompositor { self.global.borrow_mut().send_transaction(transaction); }, - CrossProcessCompositorMessage::HitTest(pipeline, point, flags, sender) => { + CompositorMsg::HitTest(pipeline, point, flags, sender) => { // 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. // Without flushing scene building, any hit test we do might be done against @@ -805,11 +785,11 @@ impl IOCompositor { let _ = sender.send(result); }, - CrossProcessCompositorMessage::GenerateImageKey(sender) => { + CompositorMsg::GenerateImageKey(sender) => { let _ = sender.send(self.global.borrow().webrender_api.generate_image_key()); }, - CrossProcessCompositorMessage::UpdateImages(updates) => { + CompositorMsg::UpdateImages(updates) => { let mut txn = Transaction::new(); for update in updates { match update { @@ -825,26 +805,21 @@ impl IOCompositor { self.global.borrow_mut().send_transaction(txn); }, - CrossProcessCompositorMessage::AddFont(font_key, data, index) => { + CompositorMsg::AddFont(font_key, data, index) => { self.add_font(font_key, index, data); }, - CrossProcessCompositorMessage::AddSystemFont(font_key, native_handle) => { + CompositorMsg::AddSystemFont(font_key, native_handle) => { let mut transaction = Transaction::new(); transaction.add_native_font(font_key, native_handle); self.global.borrow_mut().send_transaction(transaction); }, - CrossProcessCompositorMessage::AddFontInstance( - font_instance_key, - font_key, - size, - flags, - ) => { + CompositorMsg::AddFontInstance(font_instance_key, font_key, size, flags) => { self.add_font_instance(font_instance_key, font_key, size, flags); }, - CrossProcessCompositorMessage::RemoveFonts(keys, instance_keys) => { + CompositorMsg::RemoveFonts(keys, instance_keys) => { let mut transaction = Transaction::new(); for instance in instance_keys.into_iter() { @@ -857,13 +832,13 @@ impl IOCompositor { self.global.borrow_mut().send_transaction(transaction); }, - CrossProcessCompositorMessage::AddImage(key, desc, data) => { + CompositorMsg::AddImage(key, desc, data) => { let mut txn = Transaction::new(); txn.add_image(key, desc, data.into(), None); self.global.borrow_mut().send_transaction(txn); }, - CrossProcessCompositorMessage::GenerateFontKeys( + CompositorMsg::GenerateFontKeys( number_of_font_keys, number_of_font_instance_keys, result_sender, @@ -881,7 +856,7 @@ impl IOCompositor { .collect(); let _ = result_sender.send((font_keys, font_instance_keys)); }, - CrossProcessCompositorMessage::GetClientWindowRect(webview_id, response_sender) => { + CompositorMsg::GetClientWindowRect(webview_id, response_sender) => { let client_window_rect = self .webviews .get(webview_id) @@ -891,7 +866,7 @@ impl IOCompositor { warn!("Sending response to get client window failed ({error:?})."); } }, - CrossProcessCompositorMessage::GetScreenSize(webview_id, response_sender) => { + CompositorMsg::GetScreenSize(webview_id, response_sender) => { let screen_size = self .webviews .get(webview_id) @@ -901,7 +876,7 @@ impl IOCompositor { warn!("Sending response to get screen size failed ({error:?})."); } }, - CrossProcessCompositorMessage::GetAvailableScreenSize(webview_id, response_sender) => { + CompositorMsg::GetAvailableScreenSize(webview_id, response_sender) => { let available_screen_size = self .webviews .get(webview_id) @@ -935,16 +910,14 @@ impl IOCompositor { } let _ = sender.send(()); }, - CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GenerateImageKey( - sender, - )) => { + CompositorMsg::GenerateImageKey(sender) => { let _ = sender.send(self.global.borrow().webrender_api.generate_image_key()); }, - CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GenerateFontKeys( + CompositorMsg::GenerateFontKeys( number_of_font_keys, number_of_font_instance_keys, result_sender, - )) => { + ) => { let font_keys = (0..number_of_font_keys) .map(|_| self.global.borrow().webrender_api.generate_font_key()) .collect(); @@ -958,26 +931,17 @@ impl IOCompositor { .collect(); let _ = result_sender.send((font_keys, font_instance_keys)); }, - CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GetClientWindowRect( - _, - response_sender, - )) => { + CompositorMsg::GetClientWindowRect(_, response_sender) => { if let Err(error) = response_sender.send(Default::default()) { warn!("Sending response to get client window failed ({error:?})."); } }, - CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GetScreenSize( - _, - response_sender, - )) => { + CompositorMsg::GetScreenSize(_, response_sender) => { if let Err(error) = response_sender.send(Default::default()) { warn!("Sending response to get client window failed ({error:?})."); } }, - CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GetAvailableScreenSize( - _, - response_sender, - )) => { + CompositorMsg::GetAvailableScreenSize(_, response_sender) => { if let Err(error) = response_sender.send(Default::default()) { warn!("Sending response to get client window failed ({error:?})."); } @@ -1600,12 +1564,7 @@ impl IOCompositor { // Check for new messages coming from the other threads in the system. let mut compositor_messages = vec![]; let mut found_recomposite_msg = false; - while let Some(msg) = self - .global - .borrow_mut() - .compositor_receiver - .try_recv_compositor_msg() - { + while let Ok(msg) = self.global.borrow_mut().compositor_receiver.try_recv() { match msg { CompositorMsg::NewWebRenderFrameReady(..) if found_recomposite_msg => { // Only take one of duplicate NewWebRendeFrameReady messages, but do subtract diff --git a/components/compositing/lib.rs b/components/compositing/lib.rs index 2e09fd0b1ab..8631b63076a 100644 --- a/components/compositing/lib.rs +++ b/components/compositing/lib.rs @@ -8,9 +8,9 @@ use std::cell::Cell; use std::rc::Rc; use compositing_traits::rendering_context::RenderingContext; -use compositing_traits::{CompositorProxy, CompositorReceiver}; +use compositing_traits::{CompositorMsg, CompositorProxy}; use constellation_traits::EmbedderToConstellationMessage; -use crossbeam_channel::Sender; +use crossbeam_channel::{Receiver, Sender}; use embedder_traits::ShutdownState; use profile_traits::{mem, time}; use webrender::RenderApi; @@ -32,7 +32,7 @@ pub struct InitialCompositorState { /// A channel to the compositor. pub sender: CompositorProxy, /// A port on which messages inbound to the compositor can be received. - pub receiver: CompositorReceiver, + pub receiver: Receiver, /// A channel to the constellation. pub constellation_chan: Sender, /// A channel to the time profiler thread. diff --git a/components/compositing/tracing.rs b/components/compositing/tracing.rs index 907c931aebe..0373d0f1d1a 100644 --- a/components/compositing/tracing.rs +++ b/components/compositing/tracing.rs @@ -42,7 +42,21 @@ mod from_constellation { Self::LoadComplete(..) => target!("LoadComplete"), Self::WebDriverMouseButtonEvent(..) => target!("WebDriverMouseButtonEvent"), Self::WebDriverMouseMoveEvent(..) => target!("WebDriverMouseMoveEvent"), - Self::CrossProcess(_) => target!("CrossProcess"), + Self::SendInitialTransaction(..) => target!("SendInitialTransaction"), + 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"), } } } diff --git a/components/script/dom/screen.rs b/components/script/dom/screen.rs index 4d76b20d626..061b2da3eca 100644 --- a/components/script/dom/screen.rs +++ b/components/script/dom/screen.rs @@ -2,7 +2,7 @@ * 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/. */ -use compositing_traits::CrossProcessCompositorMessage; +use compositing_traits::CompositorMsg; use dom_struct::dom_struct; use euclid::Size2D; use profile_traits::ipc; @@ -41,7 +41,7 @@ impl Screen { self.window .compositor_api() .sender() - .send(CrossProcessCompositorMessage::GetScreenSize( + .send(CompositorMsg::GetScreenSize( self.window.webview_id(), sender, )) @@ -57,7 +57,7 @@ impl Screen { self.window .compositor_api() .sender() - .send(CrossProcessCompositorMessage::GetAvailableScreenSize( + .send(CompositorMsg::GetAvailableScreenSize( self.window.webview_id(), sender, )) diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 90e498b9dba..ce3b5ff6e8c 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -1884,10 +1884,7 @@ impl Window { let (sender, receiver) = ProfiledIpc::channel::(timer_profile_chan).unwrap(); let _ = self.compositor_api.sender().send( - compositing_traits::CrossProcessCompositorMessage::GetClientWindowRect( - self.webview_id(), - sender, - ), + compositing_traits::CompositorMsg::GetClientWindowRect(self.webview_id(), sender), ); let rect = receiver.recv().unwrap_or_default(); ( diff --git a/components/servo/lib.rs b/components/servo/lib.rs index 68008775c42..87b95330021 100644 --- a/components/servo/lib.rs +++ b/components/servo/lib.rs @@ -48,8 +48,8 @@ pub use compositing_traits::rendering_context::{ OffscreenRenderingContext, RenderingContext, SoftwareRenderingContext, WindowRenderingContext, }; use compositing_traits::{ - CompositorMsg, CompositorProxy, CompositorReceiver, CrossProcessCompositorApi, - WebrenderExternalImageHandlers, WebrenderExternalImageRegistry, WebrenderImageHandlerType, + CompositorMsg, CompositorProxy, CrossProcessCompositorApi, WebrenderExternalImageHandlers, + WebrenderExternalImageRegistry, WebrenderImageHandlerType, }; #[cfg(all( not(target_os = "windows"), @@ -992,7 +992,7 @@ fn create_embedder_channel( fn create_compositor_channel( event_loop_waker: Box, -) -> (CompositorProxy, CompositorReceiver) { +) -> (CompositorProxy, Receiver) { let (sender, receiver) = unbounded(); let (compositor_ipc_sender, compositor_ipc_receiver) = @@ -1009,13 +1009,11 @@ fn create_compositor_channel( 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_clone.send(message.expect("Could not convert Compositor message")); }), ); - (compositor_proxy, CompositorReceiver { receiver }) + (compositor_proxy, receiver) } #[allow(clippy::too_many_arguments)] diff --git a/components/shared/compositing/lib.rs b/components/shared/compositing/lib.rs index acc14e3de29..3a5e188c5d6 100644 --- a/components/shared/compositing/lib.rs +++ b/components/shared/compositing/lib.rs @@ -7,7 +7,7 @@ use std::fmt::{Debug, Error, Formatter}; use base::id::{PipelineId, WebViewId}; -use crossbeam_channel::{Receiver, Sender}; +use crossbeam_channel::Sender; use embedder_traits::{ AnimationState, EventLoopWaker, MouseButton, MouseButtonAction, TouchEventResult, }; @@ -22,7 +22,6 @@ use webrender_api::DocumentId; pub mod display_list; pub mod rendering_context; -use core::fmt; use std::collections::HashMap; use std::sync::{Arc, Mutex}; @@ -60,22 +59,8 @@ impl CompositorProxy { } } -/// The port that the compositor receives messages on. -pub struct CompositorReceiver { - pub receiver: Receiver, -} - -impl CompositorReceiver { - pub fn try_recv_compositor_msg(&mut self) -> Option { - 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. -#[derive(IntoStaticStr)] +#[derive(Deserialize, IntoStaticStr, Serialize)] pub enum CompositorMsg { /// Alerts the compositor that the given pipeline has changed whether it is running animations. ChangeRunningAnimationsState(WebViewId, PipelineId, AnimationState), @@ -112,32 +97,6 @@ pub enum CompositorMsg { /// WebDriver mouse move event 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, -} - -/// 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. SendInitialTransaction(WebRenderPipelineId), /// Perform a scroll operation. @@ -197,31 +156,29 @@ pub enum CrossProcessCompositorMessage { GetAvailableScreenSize(WebViewId, IpcSender), } -impl fmt::Debug for CrossProcessCompositorMessage { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - 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"), - } +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 struct SendableFrameTree { + pub pipeline: CompositionPipeline, + pub children: Vec, +} + +/// 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. #[derive(Clone, Deserialize, Serialize)] -pub struct CrossProcessCompositorApi(pub IpcSender); +pub struct CrossProcessCompositorApi(pub IpcSender); impl CrossProcessCompositorApi { /// Create a new [`CrossProcessCompositorApi`] struct that does not have a listener on the other @@ -232,18 +189,13 @@ impl CrossProcessCompositorApi { } /// Get the sender for this proxy. - pub fn sender(&self) -> &IpcSender { + pub fn sender(&self) -> &IpcSender { &self.0 } /// Inform WebRender of the existence of this pipeline. pub fn send_initial_transaction(&self, pipeline: WebRenderPipelineId) { - if let Err(e) = self - .0 - .send(CrossProcessCompositorMessage::SendInitialTransaction( - pipeline, - )) - { + if let Err(e) = self.0.send(CompositorMsg::SendInitialTransaction(pipeline)) { warn!("Error sending initial transaction: {}", e); } } @@ -256,7 +208,7 @@ impl CrossProcessCompositorApi { point: LayoutPoint, scroll_id: ExternalScrollId, ) { - if let Err(e) = self.0.send(CrossProcessCompositorMessage::SendScrollNode( + if let Err(e) = self.0.send(CompositorMsg::SendScrollNode( webview_id, pipeline_id, point, @@ -275,7 +227,7 @@ impl CrossProcessCompositorApi { ) { let (display_list_data, display_list_descriptor) = list.into_data(); let (display_list_sender, display_list_receiver) = ipc::bytes_channel().unwrap(); - if let Err(e) = self.0.send(CrossProcessCompositorMessage::SendDisplayList { + if let Err(e) = self.0.send(CompositorMsg::SendDisplayList { webview_id, display_list_descriptor, display_list_receiver, @@ -310,9 +262,7 @@ impl CrossProcessCompositorApi { ) -> Vec { let (sender, receiver) = ipc::channel().unwrap(); self.0 - .send(CrossProcessCompositorMessage::HitTest( - pipeline, point, flags, sender, - )) + .send(CompositorMsg::HitTest(pipeline, point, flags, sender)) .expect("error sending hit test"); receiver.recv().expect("error receiving hit test result") } @@ -320,9 +270,7 @@ impl CrossProcessCompositorApi { /// Create a new image key. Blocks until the key is available. pub fn generate_image_key(&self) -> Option { let (sender, receiver) = ipc::channel().unwrap(); - self.0 - .send(CrossProcessCompositorMessage::GenerateImageKey(sender)) - .ok()?; + self.0.send(CompositorMsg::GenerateImageKey(sender)).ok()?; receiver.recv().ok() } @@ -332,19 +280,14 @@ impl CrossProcessCompositorApi { descriptor: ImageDescriptor, data: SerializableImageData, ) { - if let Err(e) = self.0.send(CrossProcessCompositorMessage::AddImage( - key, descriptor, data, - )) { + if let Err(e) = self.0.send(CompositorMsg::AddImage(key, descriptor, data)) { warn!("Error sending image update: {}", e); } } /// Perform an image resource update operation. pub fn update_images(&self, updates: Vec) { - if let Err(e) = self - .0 - .send(CrossProcessCompositorMessage::UpdateImages(updates)) - { + if let Err(e) = self.0.send(CompositorMsg::UpdateImages(updates)) { warn!("error sending image updates: {}", e); } } @@ -357,10 +300,7 @@ impl CrossProcessCompositorApi { if keys.is_empty() && instance_keys.is_empty() { return; } - let _ = self.0.send(CrossProcessCompositorMessage::RemoveFonts( - keys, - instance_keys, - )); + let _ = self.0.send(CompositorMsg::RemoveFonts(keys, instance_keys)); } pub fn add_font_instance( @@ -370,7 +310,7 @@ impl CrossProcessCompositorApi { size: f32, flags: FontInstanceFlags, ) { - let _x = self.0.send(CrossProcessCompositorMessage::AddFontInstance( + let _x = self.0.send(CompositorMsg::AddFontInstance( font_instance_key, font_key, size, @@ -379,15 +319,11 @@ impl CrossProcessCompositorApi { } pub fn add_font(&self, font_key: FontKey, data: Arc, index: u32) { - let _ = self.0.send(CrossProcessCompositorMessage::AddFont( - font_key, data, index, - )); + let _ = self.0.send(CompositorMsg::AddFont(font_key, data, index)); } pub fn add_system_font(&self, font_key: FontKey, handle: NativeFontHandle) { - let _ = self.0.send(CrossProcessCompositorMessage::AddSystemFont( - font_key, handle, - )); + let _ = self.0.send(CompositorMsg::AddSystemFont(font_key, handle)); } pub fn fetch_font_keys( @@ -396,7 +332,7 @@ impl CrossProcessCompositorApi { number_of_font_instance_keys: usize, ) -> (Vec, Vec) { let (sender, receiver) = ipc_channel::ipc::channel().expect("Could not create IPC channel"); - let _ = self.0.send(CrossProcessCompositorMessage::GenerateFontKeys( + let _ = self.0.send(CompositorMsg::GenerateFontKeys( number_of_font_keys, number_of_font_instance_keys, sender,