From b6cf6c50e4f95c6ffdb548a1303274db021b0751 Mon Sep 17 00:00:00 2001 From: Guro Bokum Date: Mon, 20 Apr 2015 22:01:56 +0700 Subject: [PATCH] Start using on_refresh_driver_tick #5681 Final --- components/compositing/compositor.rs | 39 +++++--- components/compositing/compositor_task.rs | 4 +- components/compositing/constellation.rs | 9 +- components/devtools/actors/framerate.rs | 95 ++++++++++++++----- components/devtools/actors/timeline.rs | 25 +++-- components/devtools/lib.rs | 30 ++++-- components/devtools_traits/lib.rs | 2 + components/layout/animation.rs | 17 +++- components/msg/constellation_msg.rs | 10 +- components/script/devtools.rs | 8 +- components/script/dom/bindings/trace.rs | 7 ++ components/script/dom/document.rs | 75 ++++++++++++++- components/script/dom/webidls/Window.webidl | 7 ++ components/script/dom/window.rs | 25 ++++- components/script/script_task.rs | 11 +++ components/script_traits/lib.rs | 4 +- tests/html/test_request_animation_frame.html | 5 + tests/wpt/include.ini | 2 + .../callback-invoked.html.ini | 4 + 19 files changed, 313 insertions(+), 66 deletions(-) create mode 100644 tests/html/test_request_animation_frame.html create mode 100644 tests/wpt/metadata/animation-timing/callback-invoked.html.ini diff --git a/components/compositing/compositor.rs b/components/compositing/compositor.rs index e09b0685d412..221dcf23f7f2 100644 --- a/components/compositing/compositor.rs +++ b/components/compositing/compositor.rs @@ -27,6 +27,7 @@ use layers::rendergl; use layers::scene::Scene; use msg::compositor_msg::{Epoch, LayerId}; use msg::compositor_msg::{ReadyState, PaintState, ScrollPolicy}; +use msg::constellation_msg::AnimationState; use msg::constellation_msg::Msg as ConstellationMsg; use msg::constellation_msg::{ConstellationChan, NavigationDirection}; use msg::constellation_msg::{Key, KeyModifiers, KeyState, LoadData}; @@ -166,8 +167,11 @@ struct PipelineDetails { /// The status of this pipeline's PaintTask. paint_state: PaintState, - /// Whether animations are running. + /// Whether animations are running animations_running: bool, + + /// Whether there are animation callbacks + animation_callbacks_running: bool, } impl PipelineDetails { @@ -177,6 +181,7 @@ impl PipelineDetails { ready_state: ReadyState::Blank, paint_state: PaintState::Painting, animations_running: false, + animation_callbacks_running: false, } } } @@ -278,9 +283,9 @@ impl IOCompositor { self.change_paint_state(pipeline_id, paint_state); } - (Msg::ChangeRunningAnimationsState(pipeline_id, running_animations), + (Msg::ChangeRunningAnimationsState(pipeline_id, animation_state), ShutdownState::NotShuttingDown) => { - self.change_running_animations_state(pipeline_id, running_animations); + self.change_running_animations_state(pipeline_id, animation_state); } (Msg::ChangePageTitle(pipeline_id, title), ShutdownState::NotShuttingDown) => { @@ -422,11 +427,22 @@ impl IOCompositor { /// recomposite if necessary. fn change_running_animations_state(&mut self, pipeline_id: PipelineId, - animations_running: bool) { - self.get_or_create_pipeline_details(pipeline_id).animations_running = animations_running; - - if animations_running { - self.composite_if_necessary(CompositingReason::Animation); + animation_state: AnimationState) { + match animation_state { + AnimationState::AnimationsPresent => { + self.get_or_create_pipeline_details(pipeline_id).animations_running = true; + self.composite_if_necessary(CompositingReason::Animation); + } + AnimationState::AnimationCallbacksPresent => { + self.get_or_create_pipeline_details(pipeline_id).animation_callbacks_running = true; + self.composite_if_necessary(CompositingReason::Animation); + } + AnimationState::NoAnimationsPresent => { + self.get_or_create_pipeline_details(pipeline_id).animations_running = false; + } + AnimationState::NoAnimationCallbacksPresent => { + self.get_or_create_pipeline_details(pipeline_id).animation_callbacks_running = false; + } } } @@ -921,10 +937,11 @@ impl IOCompositor { /// If there are any animations running, dispatches appropriate messages to the constellation. fn process_animations(&mut self) { for (pipeline_id, pipeline_details) in self.pipeline_details.iter() { - if !pipeline_details.animations_running { - continue + if pipeline_details.animations_running || + pipeline_details.animation_callbacks_running { + + self.constellation_chan.0.send(ConstellationMsg::TickAnimation(*pipeline_id)).unwrap(); } - self.constellation_chan.0.send(ConstellationMsg::TickAnimation(*pipeline_id)).unwrap(); } } diff --git a/components/compositing/compositor_task.rs b/components/compositing/compositor_task.rs index acafbbc8f9b7..0f2b12497038 100644 --- a/components/compositing/compositor_task.rs +++ b/components/compositing/compositor_task.rs @@ -19,7 +19,7 @@ use layers::platform::surface::{NativeCompositingGraphicsContext, NativeGraphics use layers::layers::LayerBufferSet; use msg::compositor_msg::{Epoch, LayerId, LayerMetadata, ReadyState}; use msg::compositor_msg::{PaintListener, PaintState, ScriptListener, ScrollPolicy}; -use msg::constellation_msg::{ConstellationChan, PipelineId}; +use msg::constellation_msg::{AnimationState, ConstellationChan, PipelineId}; use msg::constellation_msg::{Key, KeyState, KeyModifiers}; use profile_traits::mem; use profile_traits::time; @@ -202,7 +202,7 @@ pub enum Msg { /// Alerts the compositor that the current page has changed its URL. ChangePageUrl(PipelineId, Url), /// Alerts the compositor that the given pipeline has changed whether it is running animations. - ChangeRunningAnimationsState(PipelineId, bool), + ChangeRunningAnimationsState(PipelineId, AnimationState), /// Alerts the compositor that a `PaintMsg` has been discarded. PaintMsgDiscarded, /// Replaces the current frame tree, typically called during main frame navigation. diff --git a/components/compositing/constellation.rs b/components/compositing/constellation.rs index a9271bc0cfd5..5b69a6781173 100644 --- a/components/compositing/constellation.rs +++ b/components/compositing/constellation.rs @@ -14,6 +14,7 @@ use gfx::font_cache_task::FontCacheTask; use layout_traits::{LayoutControlMsg, LayoutTaskFactory}; use libc; use msg::compositor_msg::LayerId; +use msg::constellation_msg::AnimationState; use msg::constellation_msg::Msg as ConstellationMsg; use msg::constellation_msg::{FrameId, PipelineExitType, PipelineId}; use msg::constellation_msg::{IFrameSandboxState, MozBrowserEvent, NavigationDirection}; @@ -344,8 +345,8 @@ impl Constellation { sandbox); } ConstellationMsg::SetCursor(cursor) => self.handle_set_cursor_msg(cursor), - ConstellationMsg::ChangeRunningAnimationsState(pipeline_id, animations_running) => { - self.handle_change_running_animations_state(pipeline_id, animations_running) + ConstellationMsg::ChangeRunningAnimationsState(pipeline_id, animation_state) => { + self.handle_change_running_animations_state(pipeline_id, animation_state) } ConstellationMsg::TickAnimation(pipeline_id) => { self.handle_tick_animation(pipeline_id) @@ -560,9 +561,9 @@ impl Constellation { fn handle_change_running_animations_state(&mut self, pipeline_id: PipelineId, - animations_running: bool) { + animation_state: AnimationState) { self.compositor_proxy.send(CompositorMsg::ChangeRunningAnimationsState(pipeline_id, - animations_running)) + animation_state)) } fn handle_tick_animation(&mut self, pipeline_id: PipelineId) { diff --git a/components/devtools/actors/framerate.rs b/components/devtools/actors/framerate.rs index 0cb824863427..3e02971a1edd 100644 --- a/components/devtools/actors/framerate.rs +++ b/components/devtools/actors/framerate.rs @@ -5,15 +5,23 @@ use rustc_serialize::json; use std::mem; use std::net::TcpStream; +use std::sync::{Arc, Mutex}; +use std::sync::mpsc::Sender; use time::precise_time_ns; +use msg::constellation_msg::PipelineId; use actor::{Actor, ActorRegistry}; +use actors::timeline::HighResolutionStamp; +use devtools_traits::{DevtoolsControlMsg, DevtoolScriptControlMsg}; pub struct FramerateActor { name: String, + pipeline: PipelineId, + script_sender: Sender, + devtools_sender: Sender, start_time: Option, - is_recording: bool, - ticks: Vec, + is_recording: Arc>, + ticks: Arc>>, } impl Actor for FramerateActor { @@ -33,13 +41,19 @@ impl Actor for FramerateActor { impl FramerateActor { /// return name of actor - pub fn create(registry: &ActorRegistry) -> String { + pub fn create(registry: &ActorRegistry, + pipeline_id: PipelineId, + script_sender: Sender, + devtools_sender: Sender) -> String { let actor_name = registry.new_name("framerate"); let mut actor = FramerateActor { name: actor_name.clone(), + pipeline: pipeline_id, + script_sender: script_sender, + devtools_sender: devtools_sender, start_time: None, - is_recording: false, - ticks: Vec::new(), + is_recording: Arc::new(Mutex::new(false)), + ticks: Arc::new(Mutex::new(Vec::new())), }; actor.start_recording(); @@ -47,36 +61,71 @@ impl FramerateActor { actor_name } - // callback on request animation frame - #[allow(dead_code)] - pub fn on_refresh_driver_tick(&mut self) { - if !self.is_recording { - return; - } - // TODO: Need implement requesting animation frame - // http://hg.mozilla.org/mozilla-central/file/0a46652bd992/dom/base/nsGlobalWindow.cpp#l5314 - - let start_time = self.start_time.as_ref().unwrap(); - self.ticks.push(*start_time - precise_time_ns()); + pub fn add_tick(&self, tick: f64) { + let mut lock = self.ticks.lock(); + let mut ticks = lock.as_mut().unwrap(); + ticks.push(HighResolutionStamp::wrap(tick)); } - pub fn take_pending_ticks(&mut self) -> Vec { - mem::replace(&mut self.ticks, Vec::new()) + pub fn take_pending_ticks(&self) -> Vec { + let mut lock = self.ticks.lock(); + let mut ticks = lock.as_mut().unwrap(); + mem::replace(ticks, Vec::new()) } fn start_recording(&mut self) { - self.is_recording = true; + let mut lock = self.is_recording.lock(); + if **lock.as_ref().unwrap() { + return; + } + self.start_time = Some(precise_time_ns()); + let is_recording = lock.as_mut(); + **is_recording.unwrap() = true; + + fn get_closure(is_recording: Arc>, + name: String, + pipeline: PipelineId, + script_sender: Sender, + devtools_sender: Sender) + -> Box { + + let closure = move |now: f64| { + let msg = DevtoolsControlMsg::FramerateTick(name.clone(), now); + devtools_sender.send(msg).unwrap(); + + if !*is_recording.lock().unwrap() { + return; + } + + let closure = get_closure(is_recording.clone(), + name.clone(), + pipeline.clone(), + script_sender.clone(), + devtools_sender.clone()); + let msg = DevtoolScriptControlMsg::RequestAnimationFrame(pipeline, closure); + script_sender.send(msg).unwrap(); + }; + Box::new(closure) + }; - // TODO(#5681): Need implement requesting animation frame - // http://hg.mozilla.org/mozilla-central/file/0a46652bd992/dom/base/nsGlobalWindow.cpp#l5314 + let closure = get_closure(self.is_recording.clone(), + self.name(), + self.pipeline.clone(), + self.script_sender.clone(), + self.devtools_sender.clone()); + let msg = DevtoolScriptControlMsg::RequestAnimationFrame(self.pipeline, closure); + self.script_sender.send(msg).unwrap(); } fn stop_recording(&mut self) { - if !self.is_recording { + let mut lock = self.is_recording.lock(); + if !**lock.as_ref().unwrap() { return; } - self.is_recording = false; + + let is_recording = lock.as_mut(); + **is_recording.unwrap() = false; self.start_time = None; } diff --git a/components/devtools/actors/timeline.rs b/components/devtools/actors/timeline.rs index 12c361b279d8..dc29b8a80b74 100644 --- a/components/devtools/actors/timeline.rs +++ b/components/devtools/actors/timeline.rs @@ -16,7 +16,7 @@ use time::PreciseTime; use actor::{Actor, ActorRegistry}; use actors::memory::{MemoryActor, TimelineMemoryReply}; use actors::framerate::FramerateActor; -use devtools_traits::DevtoolScriptControlMsg; +use devtools_traits::{DevtoolsControlMsg, DevtoolScriptControlMsg}; use devtools_traits::DevtoolScriptControlMsg::{SetTimelineMarkers, DropTimelineMarkers}; use devtools_traits::{TimelineMarker, TracingMetadata, TimelineMarkerType}; use protocol::JsonPacketStream; @@ -25,6 +25,7 @@ use util::task; pub struct TimelineActor { name: String, script_sender: Sender, + devtools_sender: Sender, marker_types: Vec, pipeline: PipelineId, is_recording: Arc>, @@ -93,21 +94,25 @@ struct FramerateEmitterReply { __type__: String, from: String, delta: HighResolutionStamp, - timestamps: Vec, + timestamps: Vec, } /// HighResolutionStamp is struct that contains duration in milliseconds /// with accuracy to microsecond that shows how much time has passed since /// actor registry inited /// analog https://w3c.github.io/hr-time/#sec-DOMHighResTimeStamp -struct HighResolutionStamp(f64); +pub struct HighResolutionStamp(f64); impl HighResolutionStamp { - fn new(start_stamp: PreciseTime, time: PreciseTime) -> HighResolutionStamp { + pub fn new(start_stamp: PreciseTime, time: PreciseTime) -> HighResolutionStamp { let duration = start_stamp.to(time).num_microseconds() .expect("Too big duration in microseconds"); HighResolutionStamp(duration as f64 / 1000 as f64) } + + pub fn wrap(time: f64) -> HighResolutionStamp { + HighResolutionStamp(time) + } } impl Encodable for HighResolutionStamp { @@ -121,7 +126,8 @@ static DEFAULT_TIMELINE_DATA_PULL_TIMEOUT: u32 = 200; //ms impl TimelineActor { pub fn new(name: String, pipeline: PipelineId, - script_sender: Sender) -> TimelineActor { + script_sender: Sender, + devtools_sender: Sender) -> TimelineActor { let marker_types = vec!(TimelineMarkerType::Reflow, TimelineMarkerType::DOMEvent); @@ -131,6 +137,7 @@ impl TimelineActor { pipeline: pipeline, marker_types: marker_types, script_sender: script_sender, + devtools_sender: devtools_sender, is_recording: Arc::new(Mutex::new(false)), stream: RefCell::new(None), @@ -248,7 +255,11 @@ impl Actor for TimelineActor { // init framerate actor if let Some(with_ticks) = msg.get("withTicks") { if let Some(true) = with_ticks.as_boolean() { - *self.framerate_actor.borrow_mut() = Some(FramerateActor::create(registry)); + let framerate_actor = Some(FramerateActor::create(registry, + self.pipeline.clone(), + self.script_sender.clone(), + self.devtools_sender.clone())); + *self.framerate_actor.borrow_mut() = framerate_actor; } } @@ -352,7 +363,7 @@ impl Emitter { if let Some(ref actor_name) = self.framerate_actor { let mut lock = self.registry.lock(); let registry = lock.as_mut().unwrap(); - let mut framerate_actor = registry.find_mut::(actor_name); + let framerate_actor = registry.find_mut::(actor_name); let framerateReply = FramerateEmitterReply { __type__: "framerate".to_string(), from: framerate_actor.name(), diff --git a/components/devtools/lib.rs b/components/devtools/lib.rs index 348c4fdeb12a..1d6ca68fc91d 100644 --- a/components/devtools/lib.rs +++ b/components/devtools/lib.rs @@ -28,11 +28,12 @@ extern crate util; use actor::{Actor, ActorRegistry}; use actors::console::ConsoleActor; -use actors::worker::WorkerActor; +use actors::framerate::FramerateActor; use actors::inspector::InspectorActor; use actors::root::RootActor; use actors::tab::TabActor; use actors::timeline::TimelineActor; +use actors::worker::WorkerActor; use protocol::JsonPacketStream; use devtools_traits::{ConsoleMessage, DevtoolsControlMsg}; @@ -149,12 +150,19 @@ fn run_server(sender: Sender, } } + fn handle_framerate_tick(actors: Arc>, actor_name: String, tick: f64) { + let actors = actors.lock().unwrap(); + let framerate_actor = actors.find::(&actor_name); + framerate_actor.add_tick(tick); + } + // We need separate actor representations for each script global that exists; // clients can theoretically connect to multiple globals simultaneously. // TODO: move this into the root or tab modules? fn handle_new_global(actors: Arc>, ids: (PipelineId, Option), - scriptSender: Sender, + script_sender: Sender, + devtools_sender: Sender, actor_pipelines: &mut HashMap, actor_workers: &mut HashMap<(PipelineId, WorkerId), String>, page_info: DevtoolsPageInfo) { @@ -166,7 +174,7 @@ fn run_server(sender: Sender, let (tab, console, inspector, timeline) = { let console = ConsoleActor { name: actors.new_name("console"), - script_chan: scriptSender.clone(), + script_chan: script_sender.clone(), pipeline: pipeline, streams: RefCell::new(Vec::new()), }; @@ -175,13 +183,14 @@ fn run_server(sender: Sender, walker: RefCell::new(None), pageStyle: RefCell::new(None), highlighter: RefCell::new(None), - script_chan: scriptSender.clone(), + script_chan: script_sender.clone(), pipeline: pipeline, }; let timeline = TimelineActor::new(actors.new_name("timeline"), pipeline, - scriptSender); + script_sender, + devtools_sender); let DevtoolsPageInfo { title, url } = page_info; let tab = TabActor { @@ -252,11 +261,12 @@ fn run_server(sender: Sender, return console_actor_name; } + let sender_clone = sender.clone(); spawn_named("DevtoolsClientAcceptor".to_owned(), move || { // accept connections and process them, spawning a new task for each one for stream in listener.incoming() { // connection succeeded - sender.send(DevtoolsControlMsg::AddClient(stream.unwrap())).unwrap(); + sender_clone.send(DevtoolsControlMsg::AddClient(stream.unwrap())).unwrap(); } }); @@ -269,13 +279,15 @@ fn run_server(sender: Sender, handle_client(actors, stream.try_clone().unwrap()) }) } - Ok(DevtoolsControlMsg::ServerExitMsg) | Err(RecvError) => break, - Ok(DevtoolsControlMsg::NewGlobal(ids, scriptSender, pageinfo)) => - handle_new_global(actors.clone(), ids, scriptSender, &mut actor_pipelines, + Ok(DevtoolsControlMsg::FramerateTick(actor_name, tick)) => + handle_framerate_tick(actors.clone(), actor_name, tick), + Ok(DevtoolsControlMsg::NewGlobal(ids, script_sender, pageinfo)) => + handle_new_global(actors.clone(), ids, script_sender, sender.clone(), &mut actor_pipelines, &mut actor_workers, pageinfo), Ok(DevtoolsControlMsg::SendConsoleMessage(id, console_message)) => handle_console_message(actors.clone(), id, console_message, &actor_pipelines), + Ok(DevtoolsControlMsg::ServerExitMsg) | Err(RecvError) => break, } } diff --git a/components/devtools_traits/lib.rs b/components/devtools_traits/lib.rs index 9be18bf5e192..c6e379edca28 100644 --- a/components/devtools_traits/lib.rs +++ b/components/devtools_traits/lib.rs @@ -39,6 +39,7 @@ pub struct DevtoolsPageInfo { /// according to changes in the browser. pub enum DevtoolsControlMsg { AddClient(TcpStream), + FramerateTick(String, f64), NewGlobal((PipelineId, Option), Sender, DevtoolsPageInfo), SendConsoleMessage(PipelineId, ConsoleMessage), ServerExitMsg @@ -115,6 +116,7 @@ pub enum DevtoolScriptControlMsg { WantsLiveNotifications(PipelineId, bool), SetTimelineMarkers(PipelineId, Vec, Sender), DropTimelineMarkers(PipelineId, Vec), + RequestAnimationFrame(PipelineId, Box), } #[derive(RustcEncodable)] diff --git a/components/layout/animation.rs b/components/layout/animation.rs index 4bf5dd7ab6e4..a2a3e1c5459b 100644 --- a/components/layout/animation.rs +++ b/components/layout/animation.rs @@ -10,8 +10,9 @@ use incremental::{self, RestyleDamage}; use clock_ticks; use gfx::display_list::OpaqueNode; use layout_task::{LayoutTask, LayoutTaskData}; -use msg::constellation_msg::{Msg, PipelineId}; +use msg::constellation_msg::{AnimationState, Msg, PipelineId}; use script::layout_interface::Animation; +use script_traits::{ConstellationControlMsg, ScriptControlChan}; use std::mem; use std::sync::mpsc::Sender; use style::animation::{GetMod, PropertyAnimation}; @@ -51,11 +52,18 @@ pub fn process_new_animations(rw_data: &mut LayoutTaskData, pipeline_id: Pipelin rw_data.running_animations.push(animation) } - let animations_are_running = !rw_data.running_animations.is_empty(); + let animation_state; + if rw_data.running_animations.is_empty() { + animation_state = AnimationState::NoAnimationsPresent; + } else { + animation_state = AnimationState::AnimationsPresent; + } + rw_data.constellation_chan .0 - .send(Msg::ChangeRunningAnimationsState(pipeline_id, animations_are_running)) + .send(Msg::ChangeRunningAnimationsState(pipeline_id, animation_state)) .unwrap(); + } /// Recalculates style for an animation. This does *not* run with the DOM lock held. @@ -100,5 +108,8 @@ pub fn tick_all_animations(layout_task: &LayoutTask, rw_data: &mut LayoutTaskDat rw_data.running_animations.push(running_animation) } } + + let ScriptControlChan(ref chan) = layout_task.script_chan; + chan.send(ConstellationControlMsg::TickAllAnimations(layout_task.id)).unwrap(); } diff --git a/components/msg/constellation_msg.rs b/components/msg/constellation_msg.rs index 40ddb0161ddc..98743cdc6506 100644 --- a/components/msg/constellation_msg.rs +++ b/components/msg/constellation_msg.rs @@ -223,7 +223,7 @@ pub enum Msg { /// Dispatch a mozbrowser event to a given iframe. Only available in experimental mode. MozBrowserEvent(PipelineId, SubpageId, MozBrowserEvent), /// Indicates whether this pipeline is currently running animations. - ChangeRunningAnimationsState(PipelineId, bool), + ChangeRunningAnimationsState(PipelineId, AnimationState), /// Requests that the constellation instruct layout to begin a new tick of the animation. TickAnimation(PipelineId), // Request that the constellation send the current root pipeline id over a provided channel @@ -236,6 +236,14 @@ pub enum Msg { WebDriverCommand(PipelineId, WebDriverScriptCommand) } +#[derive(Clone, Eq, PartialEq)] +pub enum AnimationState { + AnimationsPresent, + AnimationCallbacksPresent, + NoAnimationsPresent, + NoAnimationCallbacksPresent, +} + // https://developer.mozilla.org/en-US/docs/Web/API/Using_the_Browser_API#Events pub enum MozBrowserEvent { /// Sent when the scroll position within a browser