servo/components/compositing/headless.rs
Patrick Walton 9b4cc41695 compositing: Stop compositing unnecessarily after each animation frame.
Instead, schedule a delayed composite after each frame of an animation.

The previous code would cause jank, because the following sequence
frequently occurred:

1. The page uses `requestAnimationFrame()` to request a frame.

2. The compositor receives the message, schedules a composite,
dispatches the rAF message to the script thread, composites, and goes to
sleep waiting for vblank (frame 1).

3. The script makes a change and sends it through the pipeline.
Eventually it gets painted and is sent to the compositor, but the
compositor is sleeping.

4. The compositor wakes up, sees the new painted content, page flips,
and goes to sleep (frame 2). Repeat from step 1.

The problem is that we have two composition frames, not just one. This
halves Web apps' framerate!

This commit fixes the problem by scheduling the composite in step 2 to
12 ms in the future. We already have this delayed-composition
functionality in the form of the scrolling timer, which I repurposed and
renamed to the "delayed composition timer" for this task. This change
gives the page 12 ms to prepare the frame, which seems to usually be
enough, especially with WebRender.

Note that simply removing the scheduled composite after rAF is not the
correct solution. If this is done, then pages that call rAF and don't
modify the page won't receive future rAFs, since the compositor will be
sleeping and won't be notified of vblank.

Fixes a bunch of jank in browser.html. The remaining jank seems to be a
problem with browser.html itself.
2016-02-24 14:06:46 -08:00

146 lines
5.2 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use CompositorMsg as ConstellationMsg;
use compositor_thread::{CompositorEventListener, CompositorReceiver};
use compositor_thread::{InitialCompositorState, Msg};
use euclid::scale_factor::ScaleFactor;
use euclid::{Point2D, Size2D};
use msg::constellation_msg::WindowSizeData;
use profile_traits::mem;
use profile_traits::time;
use script_traits::AnimationState;
use std::sync::mpsc::Sender;
use util::opts;
use windowing::WindowEvent;
/// Starts the compositor, which listens for messages on the specified port.
///
/// This is the null compositor which doesn't draw anything to the screen.
/// It's intended for headless testing.
pub struct NullCompositor {
/// The port on which we receive messages.
pub port: Box<CompositorReceiver>,
/// A channel to the constellation.
constellation_chan: Sender<ConstellationMsg>,
/// A channel to the time profiler.
time_profiler_chan: time::ProfilerChan,
/// A channel to the memory profiler.
mem_profiler_chan: mem::ProfilerChan,
}
impl NullCompositor {
fn new(state: InitialCompositorState) -> NullCompositor {
NullCompositor {
port: state.receiver,
constellation_chan: state.constellation_chan,
time_profiler_chan: state.time_profiler_chan,
mem_profiler_chan: state.mem_profiler_chan,
}
}
pub fn create(state: InitialCompositorState) -> NullCompositor {
let compositor = NullCompositor::new(state);
// Tell the constellation about the initial fake size.
{
compositor.constellation_chan.send(ConstellationMsg::ResizedWindow(WindowSizeData {
initial_viewport: Size2D::typed(800_f32, 600_f32),
visible_viewport: Size2D::typed(800_f32, 600_f32),
device_pixel_ratio:
ScaleFactor::new(opts::get().device_pixels_per_px.unwrap_or(1.0)),
})).unwrap();
}
compositor
}
}
impl CompositorEventListener for NullCompositor {
fn handle_events(&mut self, _: Vec<WindowEvent>) -> bool {
match self.port.recv_compositor_msg() {
Msg::Exit(chan) => {
debug!("shutting down the constellation");
self.constellation_chan.send(ConstellationMsg::Exit).unwrap();
chan.send(()).unwrap();
}
Msg::ShutdownComplete => {
debug!("constellation completed shutdown");
// Drain compositor port, sometimes messages contain channels that are blocking
// another thread from finishing (i.e. SetIds)
while self.port.try_recv_compositor_msg().is_some() {}
self.time_profiler_chan.send(time::ProfilerMsg::Exit);
self.mem_profiler_chan.send(mem::ProfilerMsg::Exit);
return false
}
Msg::GetNativeDisplay(chan) => {
chan.send(None).unwrap();
}
Msg::SetFrameTree(_, response_chan, _) => {
response_chan.send(()).unwrap();
}
Msg::GetClientWindow(send) => {
let rect = (Size2D::zero(), Point2D::zero());
send.send(rect).unwrap();
}
Msg::ChangeRunningAnimationsState(pipeline_id, animation_state) => {
match animation_state {
AnimationState::AnimationsPresent |
AnimationState::NoAnimationsPresent |
AnimationState::NoAnimationCallbacksPresent => {}
AnimationState::AnimationCallbacksPresent => {
let msg = ConstellationMsg::TickAnimation(pipeline_id);
self.constellation_chan.send(msg).unwrap()
}
}
}
// Explicitly list ignored messages so that when we add a new one,
// we'll notice and think about whether it needs a response, like
// SetFrameTree.
Msg::InitializeLayersForPipeline(..) |
Msg::AssignPaintedBuffers(..) |
Msg::ScrollFragmentPoint(..) |
Msg::Status(..) |
Msg::LoadStart(..) |
Msg::LoadComplete(..) |
Msg::DelayedCompositionTimeout(..) |
Msg::Recomposite(..) |
Msg::ChangePageTitle(..) |
Msg::ChangePageUrl(..) |
Msg::KeyEvent(..) |
Msg::TouchEventProcessed(..) |
Msg::SetCursor(..) |
Msg::ViewportConstrained(..) => {}
Msg::CreatePng(..) |
Msg::PaintThreadExited(..) |
Msg::MoveTo(..) |
Msg::ResizeTo(..) |
Msg::IsReadyToSaveImageReply(..) => {}
Msg::NewFavicon(..) => {}
Msg::HeadParsed => {}
Msg::ReturnUnusedNativeSurfaces(..) => {}
Msg::CollectMemoryReports(..) => {}
Msg::PipelineExited(..) => {}
}
true
}
fn repaint_synchronously(&mut self) {}
fn pinch_zoom_level(&self) -> f32 {
1.0
}
fn title_for_main_frame(&self) {}
}