Qt Graphics and Performance – An Overview

Published Wednesday December 16th, 2009 | by

For this blog series that I’m doing, I figure its nice to start with an overview of the whole painter, pixmaps, widgets, graphicsview, backingstore idea.

At the centre of all Qt graphics is the QPainter class. It can render to surfaces, through the QPaintDevice class. Examples of paint devices are QImage’s, QPixmaps and QWidgets. The way it works is that for a given QPaintDevice implementation we return a custom paint engine which supports rendering to that surface. This is all part of our documentation so perhaps not too interesting. Lets look at this in more detail.

QWidgets and QWindowSurface

Even though QWidget is a QPaintDevice subclass, one will never render directly into a QWidget’s surface. Instead, during the paintEvent, the painting is redirected to an offscreen surface which is represented by the internal class QWindowSurface. This was traditionally implemented using the QPainter::setRedirected(), but has since been replaced by an internal mechanism between QPainter and QWidget which is slightly more optimal.

Some times we refer to this surface as “the backingstore”, but it really is just a 2D surface. If you ever looked through the Qt source code and found a class QWidgetBackingStore, this class is responsible for figuring out which parts of the window surface needs to be updated prior to showing it to screen, so its really a repaint manager. When the concept of backingstore was introduced in Qt 4.1, the two classes were the same, but the introduction of more varying ways to get content to screen made us split it in two.

In the old days widgets were rendered “on screen”. Though the option to paint on screen is still available, it is not recommended to use it. I believe the only system that remotely supports it is X11, but it is more or less untested and thus often cause artifacts in the more complex styles. Setting the flag Qt::WA_PaintOnScreen means that the repaint manager inside Qt ignores that widget when repainting the windowsurface and instead sends a special paintEvent to that widget only. Prior to Qt 4.5 there was a significant speed gain to be had when 10-100 widgets updated at max fps, but in Qt 4.5 the repaint manager was optimized to handle this better so, on screen painting is usually worse than buffered.

Back to the window surface. All widgets are composited into the window surface top to bottom and the top-level widget will fill the surface with its background or with transparent if the Qt::WA_TranslucentBackground attribute is set. All other widgets are considered transparent. A label only draws a bit of text, but doesn’t touch anything else. What that means for the repaint manager, is that every widget that overlaps with the label, but stacks behind it, needs to be drawn before it. If the application knows that a certain widget is opaque and will draw every single pixel for every paint event, then one should set the Qt::WA_OpaquePaintEvent, which causes the repaint manager to exclude the widgets region when painting the widgets behind it.

Since all widgets are repainted into the same surface, we need to make sure that widgets don’t accidentally paint outside their own boundaries and into other widgets. Since there is no guarantee that widgets will paint inside their bounds, this could potentially lead to painting artifacts, so we set up a clip behind QPainter‘s back called the “system clip”. For most widgets the system clip is a rectangle and looking at the performance section of the QPainter docs, we see that that is not so bad. Rectangular clips, when pixel aligned, are fast. A masked widget, on the other hand, is a performance disaster. It is slower to set up and slower to render. The system clip is the same clip that is passed to the paint event, except that the clip in the paint event has been translated to be relative to the top-left of the widget, rather than to the top-left of the surface. Do NOT set the paint event’s region as a clip on the painter. It is already set up, and we don’t detect that it is the exact same region and just process it fully again. The purpose of the region/rect in the paint event is so that widgets can decide to not draw certain parts. This is primarily useful when you have big scenes in the widgets, such as a map application, graphics view or similar.

In addition to the system clip which is set up prior to calling paintEvent, the painter also needs to be in a clean state, which means setting up brushes, pens, fonts and others. Its not a huge amount, but if you have many widgets it adds up. So, though widgets are no longer native window handles (aka Alien), there is still a price tag involved in repainting them. Be aware of that when you design your application. For instance, implementing a photo gallery using QLabel‘s with pixmaps in a QScrollArea doesn’t scale. You would have to set up clipping and all the other states per label, even though the label only draws a pixmap. A single “view” widget would scale much better, because the widget can then implement a tight loop that draws pixmaps in the right places.
This whole backingstore and window surface logic only hold for Mac OS X when raster or opengl graphics systems are used. Personally I would strongly recommend to use raster, it implements the full feature set, it is often faster, has the same performance profile as Qt on Windows and painting bugs are prioritized higher for raster than for the CoreGraphics backend. In qt/main we plan to switch the default for Mac OS X to raster, we just have to iron out some window system integration issues.

Graphics systems

The concept of a graphics system was introduced in Qt 4.5. The idea is to be able to select at startup time, on an application level, what kind of graphics stack you should be using. The graphics system is responsible for creating the pixmap backends and the window surface. We currently have graphics systems for raster, OpenGL 1.x, OpenGL/ES 2.0, OpenVG and X11. You can select graphics systems either by starting the application with the command line option -graphicssystem raster|opengl|opengl1|x11|native, where “native” means to use the system default. Another option is to provide the exact same option to configure which will set that option for all applications using Qt. Finally there is the function QApplication::setGraphicsSystem which hardcodes the graphics system for a given application.

In later blogs, we plan to go into each of the paint engines in more detail, but for now, lets just look at the highlights.

Raster

The raster graphics system is the reference implementation of QPainter. It implements all the features we specify and does it all in software. When a new port is started, such as with S60, we usually start with getting raster running. It is currently the default on Windows, Embedded, S60 and will also be on Mac OS X.

Just a though. What do you think of raster on X11? If you ignore for a second that you currently get a local process local font cache. It performs quite nice on X11 and I’ve seen many people switch it at runtime. If we consider remote displays, this seems daunting, but it still may not be too bad. The way it works in the X11 paint engine today is that any gradient and pixmap transform is anyway done in software and uploaded as an image on a per painter-command level. Why not just do it all client side and upload only the parts that needs updating. We can watch HD videos (for some definition of HD, anyway) on youtube, certainly we can afford to upload a few pixels. This is bound to generate comments on XRender and server-side gradients and transforms, but these have been tried numerous times and the performance is simply not good enough.

The window system integreation is handcoded for each platform to make the most out of it. For windows the windowsurface is a QImage which shares bits with a DIBSECTION, which results in pretty good blitting speed. On X11 we use MIT Shared Memory Images. We used to use Shared Memory Pixmaps, but this is removed from Xorg, but we got this awesome patch from the community, so we’re back up and running. On Mac OS X, we’re experimenting with using GL texture streaming for getting the backbuffer to screen and we’re seeing some promising numbers with that, so I hope that will make into Qt for 4.7 too.

Because it is just an array of bytes, most native API’s have the ability to render into the same buffer we do. This makes integration with native theming quite straightforward, which is one of the reasons why this is attractive as a default desktop graphics system, despite not being hardware accelerated.

OpenGL

We have two OpenGL based graphics systems in Qt. One for OpenGL 1.x, which is primarily implemented using the fixed functionality pipeline in combination with a few ARB fragment programs. It was written for desktops back in the Qt 4.0 days (2004-2005) and has grown quite a bit since. You can enable it by writing -graphicssystem opengl1 on the command line. It is currently in life-support mode, which means that we will fix critical things like crashes, but otherwise leave it be. It is not a focus for performance from our side, though it does perform quite nicely for many scenarios.

Our primary focus is the OpenGL/ES 2.0 graphics system, which is written to run on modern graphics hardware. It does not use a fixed functionality pipeline, only vertex shaders and fragment shaders. Since Qt 4.6, this is the default paint engine used for QGLWidget. Only when the required feature set is not available will we fall back to using the 1.x engine instead. When we refer to our OpenGL paint engine, its the 2.0 engine we’re talking about.

We’ve wanted to have GL as a default graphics system on all our desktop systems for a while, but there are two major problems with it. Aliased drawing is a pain, it is close to impossible to guarantee that a line goes where you want it for certain drivers. Integration with native theming is a pain. It is rarely possible to pass a GL context to a theming function and tell it draw itself, hence we need to use temporary pixmaps for style elements. On Mac OS X, there is a function to get a CGContext from a GL context, but we’ve so far not managed to get any sensible results out of it. On the other hand, much of the UI content doesn’t depend on these features, which makes GL optimal for typical scene rendering, such as the viewport of a QGraphicsView or a photo gallery view. So as far as how the default setup in Qt will look in the future, we’re considering that the best default setup for desktop may be a combination of raster for the natively themed widgets and GL for one or two high-performance widgets. Nothing is decided on this topic though, we’re just looking at alternatives.

Another problem with using GL by default is font sharing. With raster we could theoretically share pre-rendered glyphs between processes in a cross platform manner using shared memory, with GL this becomes a bit more difficult. On X11, there is an extension to bind textures as XPixmaps which can be shared across processes, but this will usually force the textures into a less optimal format which makes them somewhat slower to draw, so it is still not optimal. On Windows, Mac OS X, S60 or QWS, we would need driver-level support for sharing texture ids, which we currently don’t have.

OpenVG

I am actually quite blank in this area. I’ve not been involved with writing it nor getting it up and running. It sits on top of EGL which makes it quite similar to the OpenGL graphics systems. We expect that OpenVG will be used in a number of mid-range embedded devices.

The cool thing about OpenVG is that it matches the QPainter API quite nicely. It supports paths, pens, brushes, gradients and composition modes, so in theory, the vectorial APIs should run optimally.

Rhys, which wrote the OpenVG paint engine, plans to do a post on the OpenVG paint engines internals in full in the near future.

Images and Pixmaps

The difference between these two is mostly covered in the documentation, but I would like to highlight a few things none the less.

Our documentation says: “QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen.”

Raster

When using the raster graphics system, pixmaps are implemented as a QImage, with a potentially significant difference. When converting a QImage to a QPixmap, we do a few things.

The image is converted to a pixel format that is fast to render to the backbuffer, meaning ARGB32_Premultiplied, RGB32, ARGB8565_Premultiplied or RGB16. When images are loaded from disk using the PNG plugin or when they are generated in software by the application, the format is often ARGB32 (non-premultiplied) as this is an easy format to work on, pixel-wise. I’ve measured ARGB32_Premultiplied onto RGB32 to be about 2-4x faster than drawing an ARGB32 non-premultiplied depending on the usecase.

Secondly, we check the pixel data for transparent pixels and convert it to an opaque format if none are found. This means that if a “.png” file is loaded as ARGB32 from disk, but only contains opaque pixels, it will be rendered as an RGB32, which is also about 2-4x faster.

OpenGL

When using the OpenGL graphics system the actual implementation of the QPixmap varies a bit from setup to setup. The most ideal option gets enabled when your GL implementation supports Frame Buffer Objects (FBOs) in combination with the GL_EXT_framebuffer_blit extension. In this case, the pixmap is represented as a OpenGL texture id, and whenever a QPainter is opened on the pixmap we grab an FBO from an internal pool and use the FBO to render into the texture.

Without these extensions available, which is typically the case for OpenGL/ES 2.0 devices, the implementation is a QImage (in optimal layout, same as raster) which is backed by a texture id. When you open a QPainter on the pixmap, you render into the QImage and when the pixmap is drawn to the screen, the texture id is used. Internally there is a syncing process between the two representations, so there will be a one-time hit of re-uploading the texture after drawing into it.

In general

If you intend to draw the same QImage twice, always convert it to a QPixmap.

There are some usecases where QPixmap is potentially worse though. We have these functions, QPixmap::scaled(), QPixmap::tranformed() and friends, which historically are there because we wanted QImage and QPixmap to have similar API’s. We have support for reimplementing this functionality on a per pixmap-backend basis, but currently no engine does this, so for the GL case, or X11 for that matter, calling QPixmap::transformed() implies a conversion from QPixmap into QImage, a software conversion, and the a conversion back to the original format.

By default a QPixmap is treated as opaque. When doing QPixmap::fill(Qt::transparent), it will be made into a pixmap with alpha channel which is slower to draw. If the pixmap is going to end up as opaque, initialize it with QPixmap::fill(Qt::white). You can even skip the initialization step all together if when you know that all pixels will be written as opaque when the pixmap is painted into.

Before moving onto something else, I’ll just give a small warning on the functions setAlphaChannel and setMask and the innocently looking alphaChannel() and mask(). These functions are part of the Qt 3 legacy that we didn’t quite manage to clean up when moving to Qt 4. In the past the alpha channel of a pixmap, or its mask, was stored separately from the pixmap data. Depending on which platform you were on, the actual implementation was a bit different. For instance on X11, you had one 1-bit pixmap mask + an 8-bit alpha channel + a 24-bit color buffer. On Windows you had a 1-bit mask + a packed 32-bit ARGB pixel buffer. In Qt 4 we merged all this into one API, so that QPixmap is to be considered a packed datastructure of ARGB pixels. What we did not remove the functions implementing the old API however. In fact, we even added the alpha channel accessors, so we made it worse. The API was to some extent convenient, but all those four functions imply touching all the data and either merging the source with the pixmap or extracting a new pixmap from the current pixmap content. Bottom line. Just don’t call them. With composition modes, you can manipulate the alpha channel of the pixmaps using QPainter. This also has the benefit that it will potentially be SSE optimized for raster or done in hardware on OpenGL, so it has potential for being quite a bit faster. There is also the QGraphicsOpacityEffect which allows you to set a mask widgets and graphics items, but as of today, it is not as fast as we would like it to be.

QGraphicsView

I’ll do at least one separate post on graphicsview alone, so I’ll just comment quickly on the difference between using QGraphicsView with items vs QWidget‘s. QGraphicsView with its scene populated with items is in many ways very similar to the widgets and their repaint handling. With the addition of layouts and QGraphicsWidgets the line is even more blurry. So which solution should you pick? More and more often, we’re seeing that people choose to create their UI’s in graphics view rather than creating them using traditional widgets.

Compared to widgets, items in a graphics view are very cheap. If we consider the photo gallery again, then using a separate item for each of the items in the view may (I say may) be reasonable. A widget is repainted through its paintEvent. A QGraphicsItem is repainted through its paint function. The good thing with the items function is that there is no QPainter::begin as the painter is already properly set up for rendering. Another good thing is that the painter has less guaranteed state than the in the widget case. There may be a transformation and some clip, but no guarantees about fonts, pens or brushes. This makes the setup a bit cheaper.

Another huge improvement over widgets is that items are not clipped by default. They have a bounding rectangle and there is a contract between the subclass implementer and the scene that the item does not paint outside. If we compare this to the system clip we need to set for widgets, then again there is less work to be done for the items. If the item violates this there will be rendering artifacts, but for graphicsview this has proven an acceptable compromise.

Most UI elements are rather simple. A button, for instance, can be composed of a background image and a short text. In QPainter terms that is one call to drawPixmap and one call to drawText. The less time spent between painter calls the better the performance. The less state changes between painter calls, the better the performance. Looking back at how much happens between these calls for a button, you quickly realize that the traditional widgets are quite heavy. If widgets are going to survive the test of time, then they need to behave more like QGraphicsItem‘s.

Some final words

I’ve been rambling on for a while, but hopefully there was some useful information in here. You may have noticed that I do not mention printing, PDF or SVG generation, nor do I focus on X11 or CoreGraphics paint engines in great detail. This is because, as outlined in the painter performance docs, we focus our performance efforts in on only a few backends which we consider critical for Qt.

Did you like this? Share it:

Posted in Graphics Dojo, OpenGL, Painting, Performance

21 comments to Qt Graphics and Performance – An Overview

boulabiar says:

I was waiting for such articles from very long time.

Maybe comparing performance with other toolkits (cairo) is a good idea.
Graph bars are very much appreciated.

Philippe says:

Thanks for this must-read article and your continuing efforts at improving the performances.
I already spent a good amount of time finding experimentally what you describe here (Mac/Windows). But I will review my code again :-)

miniak says:

Is there any chance of implementing a Direct2D graphics system. Or at least using DirectWrite for font rendering?

gunnar says:

miniak: There are no plans currently to implement a Direct2D graphics system, nor using DirectWrite for font rendering. We’re trying to focus those few developers we have onto graphics stacks that work cross platform so all platforms receive the same love. This is why we rather aim for Khronos standards like OpenGL and OpenVG than focus on Windows-only API’s.

amol says:

We did some performance measurements and optimizations in Qt 4.5. Overall, the QT performance is not so bad comparing to GDI (i’m talking about raster engine). On modern video cards and cpu it shows almost the same numbers. PC with on-board video (that has no dedicated memory) is 1.5-2 times worse for QT. Optimizations we’ve made are:
1. Caching glyphs to avoid rebuilding text on every render. This was a huge win – appx 2 times, so performance become the same as GDI (without anti-aliasing).
2. Unroll two loops in QRasterEngine that gives about 30% speedup for certain operations like text rendering and drawing vertical lines.

As to Direct2D. It’s not yet hardware supported and for the moment QT is 2-2.5 faster (we tested on quite recent Nvidia with the latest drivers). However i’d realy like to see graphics engine based on Direct3D (according to docs Direct2D is just a wrapper on Direct3D functions). The reason is that raster engine gives you no hardware acceleration, and opengl is not suitable for all cases. Because you just can’t use more than 2-3 opengl context in the same process (don’t know the reason, seems it’s optimized only for a certain limited amount of contexts per process). Currently, when 4 monitors becomes common, even highly optimized raster engine may not be enough and hardware support is needed.

gunnar says:

amol: We’re working on an API where we cache the textlayout which speeds up by a big factor, 2-4x is what we’ve seen. These two loops that you unrolled. Do you perhaps have a patch that we could apply? As for Direct3D, the answer is the same as for Direct2D. We had experimental support for Direct3D as a rendering backend up to Qt 4.6, but we removed it because it was not being worked on, and does not give us any cross paltform benefit. As for context limitations, if you’re doing Qt’s GUI will ever only live in one thread. There is the occational background scene rendering, but I really cannot see a use-case where QPainter needs to render into GL in multitudes of threads. Its all serialized to the same GPU anyway, so its probably even worse the more parallel your code is.

Great Post.

Question: Are there any plans on the Qt roadmap to port everything
to Graphics View? Or possibly merging with QWidget into one single framework?

Scott says:

A question about not using setAlphaChannel(). I’ve written some code that created a QImage from raw pixel data and wanted to set a single color (black) to be transparent. I used createMaskFromColor() to get a masking image, then setAlphaChannel() to make those pixels transparent. If this is really inefficient, what is a better way? Keep in mind, this is on a newly created QImage (hasn’t ever been drawn). I could blank out the pixels myself (either through QImage or by reformatting the data given to QImage), but I’d prefer to reuse code in Qt, if possible.

Or is it just QPixmap::setAlphaChannel() that shouldn’t be used?

amol says:

gunnar: No yet, I’ll do that in a week. As to the opengl – by context i mean opengl window. So when i create 2 opengl windows – it works perfect, 3 windows – it starts to eat cpu at about 30%, 4 windows – cpu 100%. I don’t know how to explain this, i’ve checked this on several PCs with different graphic cards and result was the same, the only difference is the number of windows when it starts to eat CPU – from 3 to 8.

Kirill says:

About Qt::WA_TranslucentBackground. Is it working with QGraphicsView? It seems to menot,as all my attempts to add TranslucentBackground to QGraphicsView were failed. :( Or is there any sample how to make this?

Dimitris Menounos says:

Excellent, and much needed, article!

A question about raster on X11. Doesn’t the “process local font cache” issue also appear and on the other raster platforms (say windows)?

From my personal experience with KDE 4.2, applications launched with -graphicssystem raster “felt” somewhat faster. For example scrolling with dolphin on some folders with houndrends of files. OpenGL mostly crashed however! Anyway, I would vote for having raster the default on every platform for the sake of uniformity.

sj says:

Thanks for this very interesting article! It would be interesting to get some deeper knowledge about the DirectFB backend as well!

Dragan says:

Thanks for all the details, helps a lot.
Does QWS work with directFB back end?

gunnar says:

Kirill: Transparent backgrounds work with QGraphicsView for me on Windows. Apply the following patch:

diff --git a/examples/animation/animatedtiles/main.cpp b/examples/animation/animatedtiles/main.cpp
index 74164d3..f2779b8 100644
--- a/examples/animation/animatedtiles/main.cpp
+++ b/examples/animation/animatedtiles/main.cpp
@@ -41,6 +41,7 @@

 #include 
 #include 
+#include 

 class Pixmap : public QObject, public QGraphicsPixmapItem
 {
@@ -120,7 +121,15 @@ private:
 class View : public QGraphicsView
 {
 public:
-    View(QGraphicsScene *scene) : QGraphicsView(scene) { }
+    View(QGraphicsScene *scene) : QGraphicsView(scene)
+    {
+//        setViewport(new QGLWidget);
+        QPalette p = palette();
+        p.setBrush(backgroundRole(), Qt::NoBrush);
+        setWindowFlags(Qt::FramelessWindowHint);
+        setAttribute(Qt::WA_TranslucentBackground);
+        setPalette(p);
+    }

 protected:
     void resizeEvent(QResizeEvent *event)
@@ -208,7 +217,12 @@ int main(int argc, char **argv)
     View *view = new View(&scene);
     view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Animated Tiles"));
     view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
-    view->setBackgroundBrush(bgPix);
+
+    QLinearGradient lg(0, 0, 1000, 1000);
+    lg.setColorAt(0, Qt::transparent);
+    lg.setColorAt(1, Qt::red);
+
+    view->setBackgroundBrush(lg);
     view->setCacheMode(QGraphicsView::CacheBackground);
     view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
     view->show();

to the animated tiles demo and you’ll get a transparent gradient background. There is still the scroll area frame, which looks a bit silly, but some style trickery will fix that. If you enable the GL Viewport, commented out in the patch, then the background is black. Translucent QGLWidget’s are a bit dodgy, at the moment, unfortunately.

gunnar says:

Dimitris: Regarding the font caching. You are right, the font caching is a problem on any platform if we decide to use the raster graphics system by default. On Windows and Mac OS X, there aren’t that many applications running which are written in Qt. Under KDE, there are maybe 50 Qt apps running at any given time, so the impact on the overall system is quite a bit worse.

amol: Looking forward to your patch ;)

The Rest: Thanks for positive feedback!

Kirill says:

@Gunnar: Great thanks to you. Your post and information are very usefull!

I will try you code with Translucent QGraphicsView on Windows soon.
Sorry, that I forgot to mention – all my attermpts to make transparent background for QGraphicsView – was on Mac and (as I understand now, thanks to your posts) – using -native- graphicsengine. Unfortunatelly, currently I don’t have ability to check your example on Mac. But I hope, I will do this in some nearest future.

But again, big thanks for your posts, now many things become more clear and this will significantly help to my cross-platform project with outstanding UI :)!

Sunil says:

Awesome article !!

Thanks a lot for explaining painting in depth. Appreciate the effort you have put.

Chris Burke says:

Gunnar: Great article! I’m a little confused by the description of how QPixmaps are loaded when using the raster engine. It seems like QPixmaps are in premultiplied format if you convert from a QImage but are NOT in premultiplied format when you load a png from disk. Is that correct? So to get a premultiplied pixmap loaded from disk, should I load the png as a QImage then convert it to a QPixmap?

gunnar says:

Chris: a QImage loaded from a .png file is usually in ARGB32 non-premultiplied. If you convert it to a QPixmap it will become premultiplied. The problem I wanted to highlight was that drawing images loaded from disk is potentially sub-optimal

Hans says:

Hello Gunnar-

> What do you think of raster on X11? If we consider
> remote displays, this seems daunting, but it still
> may not be too bad.

Half of the time I’m using a remote desktop. Across town over an SSH tunnel. The link has large bandwidth but also largish latency. I’m using nomachine NX for the display connection. My KDE desktop renders just fine using the native X11 engine, but is unusable with raster. The same desktop on a local display works about equally well with raster and X11.

To me it looks like switching to raster would lose you the remote desktop advantage of X Windows, while not gain much if any performance.

Cheers

Hans

Commenting closed.