Off-main-thread compositing on Linux

We recently enabled off-main-thread compositing on Linux. As I write this post it is enabled on Firefox nightly and Firefox developer edition and if nothing bad comes up it will ride the trains and get enabled in beta and in the stable release of Firefox 40.

Web browsers render web content in separate intermediate surfaces (we call them layers) in order to be able to efficiently animate, scroll or zoom some elements independently of the others. Compositing is the action of taking these layers and flattening them in one surface that can be brought to the screen.

Gecko used to render and composite layers from the same thread (the main thread), which is also responsible for a lot of expensive operations such as running JavaScript and computing the layout of the page. This meant that if something was taking too long on the main thread, it would also block/delay everything else.

But some operations can be performed independently of one another. For instance, a video decoder should be able to send video frames to the screen without being blocked by a script taking too long. To some extent, scrolling and zooming should also be performed without being blocked by a script that contains an infinite loop. Here comes off-main-thread compositing (let’s save a few key strokes and call it OMTC from now on).

With OMTC, Gecko does the compositing work on a dedicated thread. This thread is never waiting for the main thread, so we can make sure it runs smoothly. This lets us, for example, have video decoding threads talk directly to the compositor which improves video playback dramatically. This also opens the door to a lot of optimizations that we have been building on other platforms, but could not on Linux because they were relying on OMTC’s architecture. Chief among them, asynchronous panning and zooming (APZ), letting us perform scrolling and zooming on the compositor thread at 60 frames per seconds even when the main thread can’t keep this frame rate up (teaser: APZ is a real game changer). There is also asynchronous animations which (no surprise there) lets animations be performed by the compositor, again with 60 frames per second no matter how busy the main thread is. Currently, async animations are enabled on Linux but APZ is still in the work.

OMTC has been around for a long while. In fact we have shipped it on mobile platforms for years, but it took a long while before we managed to enable it by default on Mac, then Windows and finally on Linux recently. APZ is already enabled on mobile platforms and is getting close to ship on Mac and Windows, Linux should come soon after.

This is very exciting for the gfx team, because we have been working on getting OMTC enabled for a long time now. This is the base for a lot of important optimizations and will let us finally simplify a lot of code since we now use the same compositing architecture on all of our platforms. While this change is not obvious to user, it is a – hidden but huge – step towards great things to come, so there will be a lot to be excited about moving forward (although don’t expect everything to happen tomorrow or the day after, these things take time!).

OMTC on Linux, in its current state is still pretty new and comes with some improvements and regressions we are confident that we will be able to catch up with all of the regressions eventually but, as usual, don’t hesitate to file bugs at bugzilla.mozilla.org.

Another thing to be excited about is the work to move from Gtk 2 to Gtk 3. This is a long-standing project and I will blog about it later, when we get closer to enabling it by default. I am very pleased to see progress in this area and I’ll use this post as an opportunity to thank Martin Stránský, Lee Salzman, and the many others who are helping making this happen.

14 thoughts on “Off-main-thread compositing on Linux

    1. OMTC is somewhat independent of which compositing backend we use for compositing. Currently we use the basic (xrender) backend by default rather than the OpenGL backend on Linux. OMTC incidentally makes it easier for to enable the OpenGL backend because in order to simplify the code we removed the non-OMTC OpenGL logic from the code.
      Using a different Drawing backend than cairo (note: drawing is different from compositing) is also independent from OMTC, but being able to switch to another drawing backend without taking performance hits depends on the switch to Gtk 3.
      To enable OpenGL layers on Linux when OMTC is enabled (currently in nightly and dev edition), set the preference “layers.acceleration.force-enabled” to true in about:config. But that’s at your own risk because we don’t currently test this configuration. Don’t hesitate to report bugs if you run into them with OpenGL compositing enabled, though.

      1. Thanks for the detailed answer and for precising the difference between drawing and compositing.

        Since I have an Intel HD Graphics (Ivy Bridge), I don’t think I can try hardware acceleration (Intel driver is blacklisted as far as I know ?)

      2. OpenGL compositing works on my Ivy Bridge Linux laptop. We don’t usually blocklist all of the drivers of a certain manufacturer, but rather do it based on combinations of driver and hardware versions as we run into issues. If it turns out that your driver version is blocklisted, I encourage you to update it to a more recent version.

    1. Yes. More precisely, the way gtk2 renders native-looking widgets involves xlib pixmaps, while gtk3 does things a bit differently (better) and doesn’t require this dependency to X. So to get rid of our dependency to X (and move to wayland), we need to switch to gtk3 (or stop being able to draw buttons that respect your desktop’s theme)

  1. Thank you very much, Nicolas and all the Mozilla Community, for the great work! I hope this comes to the Stable version the future. :)

  2. Hi Everyone! I was just using the latest Nightly build and I have 2 questions that I would like to be answered by @NicalSilva , please:

    1- I have the preference “layers.offmainthreadcomposition.testing.enabled” and set to true. Will that help to improve the browser in anyway (like, this will enable more unstable features and send it to the Mozilla team?)

    2- There’s something else that I can do to help to improve the OMTC and related technologies?

    Thanks.

    1. 1- No, this pref has no effect anymore. We needed it a little while ago for some reason related to automated testing with different configurations on multiple branches.

      2- If you want to play with experimental/work-in-progress stuff, you can try (at your own risks) to enable:
      * “layers.acceleration.force-enabled”
      * “layers.async-pan-zoom.enabled”
      On Linux, the former will try to use opengl for compositing (instead of xrender), the latter will attempt to separate scrolling and painting so that scrolling is not affected by scripts that take too long. The effects of these two prefs will vary depending on your hardware/drivers so we don’t know very well yet how much of an improvement/regression they represent. If you find issues don’t hesitate to file bugs with the specifics of your configuration by copy-pasting the graphics section of about:support in the bug and saying which prefs you changed.

    1. Gtk3 is tracked by bug 627699. I don’t have an ETA but the GTK3 port has been progressing rapidly, and Fedora 22 already ships with a GTK3 build of Firefox (although this is a bold move as there are still some known issues).

    1. Yep, there’s still stuff to figure out before APZ works well on Windows. It is pretty high in our priority list, though.

Leave a comment