WebKitGTK and WPEWebKit Switching to Skia for 2D Graphics Rendering

In recent years we have had an ongoing effort to improve graphics performance of the WebKit GTK and WPE ports. As a result of this we shipped features like threaded rendering, the DMA-BUF renderer, or proper vertical retrace synchronization (VSync). While these improvements have helped keep WebKit competitive, and even perform better than other engines in some scenarios, it has been clear for a while that we were reaching the limits of what can be achieved with a CPU based 2D renderer.

There was an attempt at making Cairo support GPU rendering, which did not work particularly well due to the library being designed around stateful operation based upon the PostScript model—resulting in a convenient and familiar API, great output quality, but hard to retarget and with some particularly slow corner cases. Meanwhile, other web engines have moved more work to the GPU, including 2D rendering, where many operations are considerably faster.

We checked all the available 2D rendering libraries we could find, but none of them met all our requirements, so we decided to try writing our own library. At the beginning it worked really well, with impressive results in performance even compared to other GPU based alternatives. However, it proved challenging to find the right balance between performance and rendering quality, so we decided to try other alternatives before continuing with its development. Our next option had always been Skia. The main reason why we didn’t choose Skia from the beginning was that it didn’t provide a public library with API stability that distros can package and we can use like most of our dependencies. It still wasn’t what we wanted, but now we have more experience in WebKit maintaining third party dependencies inside the source tree like ANGLE and libwebrtc, so it was no longer a blocker either.

In December 2023 we made the decision of giving Skia a try internally and see if it would be worth the effort of maintaining the project as a third party module inside WebKit. In just one month we had implemented enough features to be able to run all MotionMark tests. The results in the desktop were quite impressive, getting double the score of MotionMark global result. We still had to do more tests in embedded devices which are the actual target of WPE, but it was clear that, at least in the desktop, with this very initial implementation that was not even optimized (we kept our current architecture that is optimized for CPU rendering) we got much better results. We decided that Skia was the option, so we continued working on it and doing more tests in embedded devices. In the boards that we tried we also got better results than CPU rendering, but the difference was not so big, which means that with less powerful GPUs and with our current architecture designed for CPU rendering we were not that far from CPU rendering. That’s the reason why we managed to keep WPE competitive in embeeded devices, but Skia will not only bring performance improvements, it will also simplify the code and will allow us to implement new features . So, we had enough data already to make the final decision of going with Skia.

In February 2024 we reached a point in which our Skia internal branch was in an “upstreamable” state, so there was no reason to continue working privately. We met with several teams from Google, Sony, Apple and Red Hat to discuss with them about our intention to switch from Cairo to Skia, upstreaming what we had as soon as possible. We got really positive feedback from all of them, so we sent an email to the WebKit developers mailing list to make it public. And again we only got positive feedback, so we started to prepare the patches to import Skia into WebKit, add the CMake integration and the initial Skia implementation for the WPE port that already landed in main.

We will continue working on the Skia implementation in upstream WebKit, and we also have plans to change our architecture to better support the GPU rendering case in a more efficient way. We don’t have a deadline, it will be ready when we have implemented everything currently supported by Cairo, we don’t plan to switch with regressions. We are focused on the WPE port for now, but at some point we will start working on GTK too and other ports using cairo will eventually start getting Skia support as well.

Cairo 1.10

Yes, it’s true, Cairo 1.10 has been finally released. Chris already summarized two years of development in 60 lines, so I’m going to talk about how this release affects to Evince/Poppler.

Blend Modes

Cairo 1.10 has some new operators inspired by PDF blend modes, that allowed us to easily implement PDF blend modes in the poppler cairo backend

PDF Blend Modes
PDF Blend Modes

Here is an example of a PDF document combining the use of Multiply blend mode with gradients

Scooby gradient rendered with Cairo 1.8
Scooby gradient rendered by poppler with Cairo 1.8
Scooby gradient rendered by poppler with Cairo 1.10
Scooby gradient rendered by poppler with Cairo 1.10
Scooby gradient rendered by xpdf
Scooby gradient rendered by xpdf

Blend modes are also important for annotations, since highlight annotations are usually implemented by using the Multiply blend mode

Performance

There were some PDF documents where the performance of the poppler cairo backend was really bad. It seems that, with such particular documents, poppler was clipping too much. Of course, Chris fixed it, see the screenshot:

Poppler with cairo 1.8 and 1.10
Poppler with cairo 1.8 and 1.10

Output file size when printing

Thanks to cairo_surface_set_mime_data now we can attach the original uncompressed image to the ps/pdf surface that will be used when creating the resulting ps/pdf file. It drastically reduces the size of the ps/pdf output files created when printing documents that contain images

Fit to contents/Trim margins

This hasn’t been implemented in Evince yet, but using the new recording surface we can get the page bounding box and use it to implement a new fit to contents zoom mode.

Thanks!

Thank you very much to everybody involved in this release