Introduce MainThreadScriptMsg::RegisterPaintWorklet

This avoids the need for a generic task to send messages to the layout thread
through the main script thread.
This commit is contained in:
Anthony Ramine 2017-09-17 09:57:12 +02:00
parent 95dc54d216
commit f58207b851
4 changed files with 67 additions and 39 deletions

View file

@ -46,7 +46,6 @@ use js::rust::Runtime;
use msg::constellation_msg::PipelineId; use msg::constellation_msg::PipelineId;
use net_traits::image::base::PixelFormat; use net_traits::image::base::PixelFormat;
use net_traits::image_cache::ImageCache; use net_traits::image_cache::ImageCache;
use script_layout_interface::message::Msg;
use script_traits::DrawAPaintImageResult; use script_traits::DrawAPaintImageResult;
use script_traits::Painter; use script_traits::Painter;
use servo_atoms::Atom; use servo_atoms::Atom;
@ -443,8 +442,7 @@ impl PaintWorkletGlobalScopeMethods for PaintWorkletGlobalScope {
// Inform layout that there is a registered paint worklet. // Inform layout that there is a registered paint worklet.
// TODO: layout will end up getting this message multiple times. // TODO: layout will end up getting this message multiple times.
let painter = self.painter(name.clone()); let painter = self.painter(name.clone());
let msg = Msg::RegisterPaint(name, properties, painter); self.worklet_global.register_paint_worklet(name, properties, painter);
self.worklet_global.send_to_layout(msg);
Ok(()) Ok(())
} }

View file

@ -22,12 +22,10 @@ use net_traits::ResourceThreads;
use net_traits::image_cache::ImageCache; use net_traits::image_cache::ImageCache;
use profile_traits::mem; use profile_traits::mem;
use profile_traits::time; use profile_traits::time;
use script_layout_interface::message::Msg; use script_thread::MainThreadScriptMsg;
use script_runtime::ScriptThreadEventCategory; use script_traits::{Painter, ScriptMsg};
use script_thread::{MainThreadScriptMsg, ScriptThread, Task}; use script_traits::{ScriptToConstellationChan, TimerSchedulerMsg};
use script_traits::ScriptMsg; use servo_atoms::Atom;
use script_traits::ScriptToConstellationChan;
use script_traits::TimerSchedulerMsg;
use servo_url::ImmutableOrigin; use servo_url::ImmutableOrigin;
use servo_url::MutableOrigin; use servo_url::MutableOrigin;
use servo_url::ServoUrl; use servo_url::ServoUrl;
@ -93,31 +91,23 @@ impl WorkletGlobalScope {
self.globalscope.evaluate_js_on_global_with_result(&*script, rval.handle_mut()) self.globalscope.evaluate_js_on_global_with_result(&*script, rval.handle_mut())
} }
/// Run a task in the main script thread. /// Register a paint worklet to the script thread.
pub fn run_in_script_thread<T>(&self, task: T) pub fn register_paint_worklet(
where &self,
T: 'static + Send + Task, name: Atom,
{ properties: Vec<Atom>,
painter: Box<Painter>,
) {
self.to_script_thread_sender self.to_script_thread_sender
.send(MainThreadScriptMsg::MainThreadTask( .send(MainThreadScriptMsg::RegisterPaintWorklet {
ScriptThreadEventCategory::WorkletEvent, pipeline_id: self.globalscope.pipeline_id(),
box task, name,
)) properties,
painter,
})
.expect("Worklet thread outlived script thread."); .expect("Worklet thread outlived script thread.");
} }
/// Send a message to layout.
pub fn send_to_layout(&self, msg: Msg) {
struct SendToLayoutTask(PipelineId, Msg);
impl Task for SendToLayoutTask {
fn run_with_script_thread(self: Box<Self>, script_thread: &ScriptThread) {
script_thread.send_to_layout(self.0, self.1);
}
}
let pipeline_id = self.globalscope.pipeline_id();
self.run_in_script_thread(SendToLayoutTask(pipeline_id, msg));
}
/// The base URL of this global. /// The base URL of this global.
pub fn base_url(&self) -> ServoUrl { pub fn base_url(&self) -> ServoUrl {
self.base_url.clone() self.base_url.clone()

View file

@ -87,16 +87,19 @@ use profile_traits::time::{self, ProfilerCategory, profile};
use script_layout_interface::message::{self, Msg, NewLayoutThreadInfo, ReflowQueryType}; use script_layout_interface::message::{self, Msg, NewLayoutThreadInfo, ReflowQueryType};
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory}; use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory};
use script_runtime::{ScriptPort, StackRootTLS, get_reports, new_rt_and_cx}; use script_runtime::{ScriptPort, StackRootTLS, get_reports, new_rt_and_cx};
use script_traits::{CompositorEvent, ConstellationControlMsg, PaintMetricType}; use script_traits::{CompositorEvent, ConstellationControlMsg};
use script_traits::{DocumentActivity, DiscardBrowsingContext, EventResult}; use script_traits::{DiscardBrowsingContext, DocumentActivity, EventResult};
use script_traits::{InitialScriptState, JsEvalResult, LayoutMsg, LoadData, MouseButton, MouseEventType}; use script_traits::{InitialScriptState, JsEvalResult, LayoutMsg, LoadData};
use script_traits::{MozBrowserEvent, NewLayoutInfo, ScriptToConstellationChan, ScriptMsg, UpdatePipelineIdReason}; use script_traits::{MouseButton, MouseEventType, MozBrowserEvent, NewLayoutInfo};
use script_traits::{ScriptThreadFactory, TimerEvent, TimerSchedulerMsg, TimerSource}; use script_traits::{PaintMetricType, Painter, ScriptMsg, ScriptThreadFactory};
use script_traits::{TouchEventType, TouchId, UntrustedNodeAddress, WindowSizeData, WindowSizeType}; use script_traits::{ScriptToConstellationChan, TimerEvent, TimerSchedulerMsg};
use script_traits::{TimerSource, TouchEventType, TouchId, UntrustedNodeAddress};
use script_traits::{UpdatePipelineIdReason, WindowSizeData, WindowSizeType};
use script_traits::CompositorEvent::{KeyEvent, MouseButtonEvent, MouseMoveEvent, ResizeEvent}; use script_traits::CompositorEvent::{KeyEvent, MouseButtonEvent, MouseMoveEvent, ResizeEvent};
use script_traits::CompositorEvent::{TouchEvent, TouchpadPressureEvent}; use script_traits::CompositorEvent::{TouchEvent, TouchpadPressureEvent};
use script_traits::webdriver_msg::WebDriverScriptCommand; use script_traits::webdriver_msg::WebDriverScriptCommand;
use serviceworkerjob::{Job, JobQueue, AsyncJobHandler}; use serviceworkerjob::{Job, JobQueue, AsyncJobHandler};
use servo_atoms::Atom;
use servo_config::opts; use servo_config::opts;
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl}; use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use std::cell::Cell; use std::cell::Cell;
@ -294,6 +297,13 @@ pub enum MainThreadScriptMsg {
/// Notifies the script thread that a new worklet has been loaded, and thus the page should be /// Notifies the script thread that a new worklet has been loaded, and thus the page should be
/// reflowed. /// reflowed.
WorkletLoaded(PipelineId), WorkletLoaded(PipelineId),
/// Notifies the script thread that a new paint worklet has been registered.
RegisterPaintWorklet {
pipeline_id: PipelineId,
name: Atom,
properties: Vec<Atom>,
painter: Box<Painter>
},
/// Runs a Task in the main thread. /// Runs a Task in the main thread.
MainThreadTask(ScriptThreadEventCategory, Box<Task + Send>), MainThreadTask(ScriptThreadEventCategory, Box<Task + Send>),
} }
@ -780,13 +790,21 @@ impl ScriptThread {
}) })
} }
pub fn send_to_layout(&self, pipeline_id: PipelineId, msg: Msg) { fn handle_register_paint_worklet(
&self,
pipeline_id: PipelineId,
name: Atom,
properties: Vec<Atom>,
painter: Box<Painter>,
) {
let window = self.documents.borrow().find_window(pipeline_id); let window = self.documents.borrow().find_window(pipeline_id);
let window = match window { let window = match window {
Some(window) => window, Some(window) => window,
None => return warn!("Message sent to layout after pipeline {} closed.", pipeline_id), None => return warn!("Paint worklet registered after pipeline {} closed.", pipeline_id),
}; };
let _ = window.layout_chan().send(msg); let _ = window.layout_chan().send(
Msg::RegisterPaint(name, properties, painter),
);
} }
pub fn push_new_element_queue() { pub fn push_new_element_queue() {
@ -1176,6 +1194,9 @@ impl ScriptThread {
match *inner_msg { match *inner_msg {
MainThreadScriptMsg::Common(CommonScriptMsg::Task(category, _)) | MainThreadScriptMsg::Common(CommonScriptMsg::Task(category, _)) |
MainThreadScriptMsg::MainThreadTask(category, _) => category, MainThreadScriptMsg::MainThreadTask(category, _) => category,
MainThreadScriptMsg::RegisterPaintWorklet { .. } => {
ScriptThreadEventCategory::WorkletEvent
},
_ => ScriptThreadEventCategory::ScriptEvent, _ => ScriptThreadEventCategory::ScriptEvent,
} }
}, },
@ -1314,6 +1335,19 @@ impl ScriptThread {
MainThreadScriptMsg::WorkletLoaded(pipeline_id) => { MainThreadScriptMsg::WorkletLoaded(pipeline_id) => {
self.handle_worklet_loaded(pipeline_id) self.handle_worklet_loaded(pipeline_id)
}, },
MainThreadScriptMsg::RegisterPaintWorklet {
pipeline_id,
name,
properties,
painter,
} => {
self.handle_register_paint_worklet(
pipeline_id,
name,
properties,
painter,
)
},
MainThreadScriptMsg::MainThreadTask(_, task) => { MainThreadScriptMsg::MainThreadTask(_, task) => {
task.run_with_script_thread(self) task.run_with_script_thread(self)
}, },

View file

@ -860,6 +860,12 @@ pub trait Painter: SpeculativePainter {
-> DrawAPaintImageResult; -> DrawAPaintImageResult;
} }
impl fmt::Debug for Painter {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple("Painter").field(&format_args!("..")).finish()
}
}
/// The result of executing paint code: the image together with any image URLs that need to be loaded. /// The result of executing paint code: the image together with any image URLs that need to be loaded.
/// TODO: this should return a WR display list. https://github.com/servo/servo/issues/17497 /// TODO: this should return a WR display list. https://github.com/servo/servo/issues/17497
#[derive(Clone, Debug, Deserialize, HeapSizeOf, Serialize)] #[derive(Clone, Debug, Deserialize, HeapSizeOf, Serialize)]