New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compositing: Stop compositing unnecessarily after each animation frame. #9663
Merged
+146
−115
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
Loading status checks…
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.
- Loading branch information
commit 9b4cc416954ac550788f3cd82f0016d74d4995ea
| @@ -0,0 +1,94 @@ | ||
| /* 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/. */ | ||
|
|
||
| //! A timer thread that composites near the end of the frame. | ||
| //! | ||
| //! This is useful when we need to composite next frame but we want to opportunistically give the | ||
| //! painting thread time to paint if it can. | ||
|
|
||
| use compositor_thread::{CompositorProxy, Msg}; | ||
| use std::sync::mpsc::{Receiver, Sender, channel}; | ||
| use std::thread::{self, Builder}; | ||
| use time; | ||
| use util::time::duration_from_nanoseconds; | ||
|
|
||
| /// The amount of time in nanoseconds that we give to the painting thread to paint. When this | ||
| /// expires, we give up and composite anyway. | ||
| static TIMEOUT: u64 = 12_000_000; | ||
|
|
||
| pub struct DelayedCompositionTimerProxy { | ||
| sender: Sender<ToDelayedCompositionTimerMsg>, | ||
| } | ||
|
|
||
| pub struct DelayedCompositionTimer { | ||
| compositor_proxy: Box<CompositorProxy>, | ||
| receiver: Receiver<ToDelayedCompositionTimerMsg>, | ||
| } | ||
|
|
||
| enum ToDelayedCompositionTimerMsg { | ||
| Exit, | ||
| ScheduleComposite(u64), | ||
| } | ||
|
|
||
| impl DelayedCompositionTimerProxy { | ||
| pub fn new(compositor_proxy: Box<CompositorProxy + Send>) -> DelayedCompositionTimerProxy { | ||
| let (to_timer_sender, to_timer_receiver) = channel(); | ||
| Builder::new().spawn(move || { | ||
| let mut timer = DelayedCompositionTimer { | ||
| compositor_proxy: compositor_proxy, | ||
| receiver: to_timer_receiver, | ||
| }; | ||
| timer.run(); | ||
| }).unwrap(); | ||
| DelayedCompositionTimerProxy { | ||
| sender: to_timer_sender, | ||
| } | ||
| } | ||
|
|
||
| pub fn schedule_composite(&mut self, timestamp: u64) { | ||
| self.sender.send(ToDelayedCompositionTimerMsg::ScheduleComposite(timestamp)).unwrap() | ||
| } | ||
|
|
||
| pub fn shutdown(&mut self) { | ||
| self.sender.send(ToDelayedCompositionTimerMsg::Exit).unwrap() | ||
| } | ||
| } | ||
|
|
||
| impl DelayedCompositionTimer { | ||
| pub fn run(&mut self) { | ||
| 'outer: loop { | ||
| let mut timestamp; | ||
| loop { | ||
| match self.receiver.recv() { | ||
| Ok(ToDelayedCompositionTimerMsg::ScheduleComposite(this_timestamp)) => { | ||
| timestamp = this_timestamp; | ||
| break | ||
| } | ||
| Ok(ToDelayedCompositionTimerMsg::Exit) => break 'outer, | ||
| _ => break 'outer, | ||
| } | ||
| } | ||
|
|
||
| // Drain all messages from the queue. | ||
| loop { | ||
| match self.receiver.try_recv() { | ||
| Ok(ToDelayedCompositionTimerMsg::ScheduleComposite(this_timestamp)) => { | ||
| timestamp = this_timestamp; | ||
| break | ||
| } | ||
| _ => break, | ||
|
||
| } | ||
| } | ||
|
|
||
| let target = timestamp + TIMEOUT; | ||
| let now = time::precise_time_ns(); | ||
| if target > now { | ||
| let delta_ns = target - now; | ||
| thread::sleep(duration_from_nanoseconds(delta_ns)); | ||
| } | ||
| self.compositor_proxy.send(Msg::DelayedCompositionTimeout(timestamp)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Should this have a case for exit? e.g.