Graphics in Google Chrome

Thursday, October 23, 2008

Google Chrome uses a library called Skia, which is also the graphics engine behind Google's Android mobile OS. The two projects share code that implements WebKit's porting API in terms of Skia. Google Chrome also uses Skia to render parts of the user interface such as the toolbar and tab strip. I'm going to talk about some of the history that led us to choose Skia, as well as how our graphics layer works. 

WebKit is designed to work on multiple operating systems. It abstracts platform-specific functions into the "port," which an embedder application such as Google Chrome implements specifically for their system. This relatively clean abstraction has helped WebKit to be adopted on a wide variety of devices and systems. One of the parts of the port we had to consider when developing Google Chrome was the graphics layer, which is responsible for rendering text, images, and other graphics to the screen.

Which graphics library?

One question that people often ask is, why not use OpenGL or DirectX for accelerated rendering? First, on Windows, we use a sandbox that prevents us from displaying windows from our renderer processes. The image data must be transferred to the main browser process before it can be drawn to the screen, which limits the possible approaches we can take. If the images needs to be read off the video card only to be copied back to the video card in another process, it is usually not worthwhile using accelerated rendering in the first place.

Second, drawing graphics is actually a very small percentage of the time we spend rendering a page. Most of the time is spent in WebKit computing where things will be placed, what styles to apply to them, and using system routines to draw text. Accelerated 3D graphics would not give us enough overall improvement in speed to balance out the extra work and compatibility problems that we would encounter.

If we aren't going to be using OpenGL or DirectX, what about other graphics libraries? We considered a number of options when we first started work on our Windows port of WebKit:
  • Windows GDI: GDI is the basic, low-level graphics API in Microsoft Windows. It is used to draw buttons, window controls, and dialog boxes for every Windows application, so we know that it's tested and works well. However, it has relatively basic capabilities. Although most web pages can be drawn using only these basic primitives, parts of <canvas> or SVG would need to be implemented separately, either using a different graphics library, or our own custom code.
  • GDI+: GDI+ is a more advanced graphics API provided on newer versions of Windows. Its API is cleaner and it supports most 2D graphics operations you could think to use. However, we had concerns about GDI+ using device independent metrics, which means that text and letter spacing might look different in Google Chrome than in other Windows applications (which measure and draw text tailored to the screen device). Additionally, at the time we were making the decision, Microsoft was recommending developers use newer graphics APIs in Windows, so we weren't sure how much longer GDI+ would be supported and maintained.
  • Cairo: Cairo is an open-source 2D graphics library. It is successfully used in Firefox 3, and the Windows port of WebKit at that time already had a partially complete graphics implementation for WebKit. Cairo is also cross-platform, a key advantage over GDI and GDI+ when building a cross-platform browser.
We ended up choosing Skia over these options because it is cross-platform (meaning our work wouldn't have to be duplicated when porting to other systems), because there was already a high-quality WebKit port using it created for Android's browser, and because we had in-house expertise. The latter point is critical because we expected to (and did) need additional features added to the graphics library as well as some bugs fixed.

So far, we've been very happy with our choice. Skia has proved to be effective at handling all the graphics operations we've needed, has been fast enough despite being software-only, and we've gotten great support from the Skia team. Thanks!

System-specific features

Android has the advantage of controlling the entire operating system graphics layer. Skia's font layer implements all text rendering for the Android system, so all text looks consistent. However, we wanted to match the host OS's look and feel. This means using native text rendering routines so that, for example, we can get ClearType on Windows.

To solve this problem, we create a wrapper around Skia's SkDevice (an object representing a low-level drawing surface) which we call PlatformDevice. The object is both a bitmap in main memory that Skia can draw into, and a "Device Independent Bitmap" that the Windows GDI layer can draw into. Lines, images, and patters are all drawn by Skia into this bitmap, while text is drawn directly by Windows. As part of our porting efforts, we are currently working on creating similar abstractions for OS X and Linux.

19 comments:

Sander said...

Would it still be possible to put hardware acceleration into Chrome in the future? I'm building a web app using heavy interactive 2D graphics (with Canvas and/or SVG), assuming that in a few years any desktop browser will be able to handle that. Mozilla and Opera are already experimenting with 3D graphics in the Canvas and I think Cairo can be rendered using the graphics card.

On Linux, application developers seem to be making use of the graphics card more and more (Compiz, Clutter, KDE4, ...). It would be nice if web applications got that privilege too.

(Not sure whether this is really related to Chromium's choice for a 2D drawing system, but I just wondered. :-)

Brett Wilson said...

By "ActiveX" I of course meant "DirectX". Whoops.

sander: Anything's possible. Depending on the sandbox for each platform, it may or may not be easier than on Windows.

Another option would be to proxy all painting calls to the main browser process (basically serializing the drawing stream) where they could be drawn directly.

dflock said...

You mentioned Cairo, but didn't say why you decided not to use it - I'd be interested to hear the reasons. Like you say: it's cross platform, well tested and mostly implemented for Webkit, so why not?

I would also be very interested to hear your thoughts on Clutter (http://clutter-project.org/) - this has loads of features that I would love were I writing a browser: it does media playback, uses Pango for text, can run pretty much anywhere (Win/Lin/OSX & mobiles), is 3D accelerated (I know you said this wasn't required, but if it's free...) and integrates with webkit, amongst many other things.

James Maurice said...

I am glad that you have raised the important issue of text appearance, and am curious as to what control (if any) users will be afforded over such features in future releases. Personally, I have never been terribly impressed with ClearType's font rendering, while the smoothing applied to text in the Windows port of Safari had the potential to induce headaches.

Under Windows XP, Chinese/Japanese characters look far better when using the rendering method implemented by GDI++. Unfortunately, the nature of the patch means that it does not work correctly with Chrome's multi-process architecture (the --singleprocess switch offers a work-around for the time being...).

Cheers,
James
x

EscortVIP said...

i'will tri chrome
acompanhantes

Brad K said...

quote: "Skia has proved to be effective at handling all the graphics operations we've needed"

Not quite. From my view, the main result of that decision is that rendering in Chrome doesn't look as good as it does in Safari: rounded corners (CSS border-radius) do not look round or smooth, text-shadows don't show, and—thanks to ClearType—the shapes of the letterforms are seriously degraded (which you seem to consider an advantage, but results in all fonts looking alike, and bold text is almost indistinguishable from non-bold text, except at large sizes).

Darky said...

> "One question that people often ask is, why not use OpenGL or ActiveX for accelerated rendering?"

I supposed you by "ActiveX" you mean "DirectX"?

Justin Beck said...

Thank you so much for using ClearType Rendering! The more I use Chrome the more I love it. Safari Speed in layout and rendering, yet still beautiful Microsoft font rendering. I am looking forward to seeing the innovation pour out of the common usage of Mobile and Web technology. aka Android and Chrome.

Eugene said...

I notice that on certain CSS3 things (namely border-radius) the rendering is very pixelly compared to firefox. Is this due to the limitations of Skia?

Steven Roussey said...

So is this why text-shadow is not supported when it is in Safari??

Brett Wilson said...

Eugene: The aliasing on rounded corners is a limitation of the hacks we do to allow Windows to draw text into our buffers and not mess up our alpha channel. Skia is quite happy to do antialiasing. I managed to get a fix, but it broke a bunch of other things in the process, so it remains chunky for now.

Steven: text shadow is coming. We use Windows to render text, and it doesn't support shadows, so it requires some extra work on our part.

play_max said...

hey


why i googl chrome connot display the friendster layouts?????


can you fix it!!!

feydopen said...

Typo: Last paragraph, second to last sentence: patters -> patterns.

Ryan WS said...

how about supporting texture mapping in Chrome, using just texture mapping (RGBA color, Additive blend, Multiply blend, RGB guroud), so we can make a unique rendering feel, well if there a texture mapping function i can try to make pysta (code.google.com/p/pysta) work in javascript in Chrome :)

Just a hope from me :)

Jessie Lawrence said...

so, umm...why is chrome for windows only?!?

"...if you had a Picasso painting, would you put it in the bathroom?"

JayaPrakash said...

Does Google Chrome work in VxWorks? Skia graphics library available for VxWorks?

Roy said...

Hi Bret,
I know how to draw with Skia to an image.
But I don't know how to draw to the screen..

Could you please explain that, or possible even reply with a short code snippet?

Thanks!

Emacs said...


Android has the advantage of controlling the entire operating system graphics layer. Skia's font layer implements all text rendering for the Android system, so all text looks consistent. However, we wanted to match the host OS's look and feel. This means using native text rendering routines so that, for example, we can get ClearType on Windows.

To solve this problem, we create a wrapper around Skia's SkDevice (an object representing a low-level drawing surface) which we call PlatformDevice. The object is both a bitmap in main memory that Skia can draw into, and a "Device Independent Bitmap" that the Windows GDI layer can draw into. Lines, images, and patters are all drawn by Skia into this bitmap, while text is drawn directly by Windows. As part of our porting efforts, we are currently working on creating similar abstractions for OS X and Linux.


It's not a truth. You've made your "abstraction", but it looks like shit:
How it looks in firefox
How it looks in chromium

Emacs said...

The system rendering is the same as in firefox (autohinter, slight hinting)