Thursday, July 24, 2008

How to easily mix QWidgets and OpenGL

Around a month ago there was a post at Trolltech Labs Blogs about embedding widgets in QGLWidget. I had been wanting to do something like that for KGLLib, so I downloaded the demo and took a look. It worked fine, but the code didn't exactly fit my purposes.
The demo did all the OpenGL rendering in the QGraphicsScene subclass. Usually it is done in QGLWidget subclass and I wanted a solution which would be as transparent as possible. Thus the rendering had to stay in GLWidget.
The second problem was the events. The event handling could have been done in QGraphicsView or QGraphicsScene subclass. But if I already have a GLWidget using event handlers, I'd prefer them to continue working as before. So some kind of smart event forwarding was needed to send the events either to the gl widget or to the graphicsview, depending on whether any of the QGV widgets had focus.

I have now solved both problems and the result is a class called WidgetProxy. It takes a GLWidget as an argument and creates a QGraphicsView object, using the GL widget as viewport. The OpenGL rendering is done in your GLWidget as usual and QWidgets can be drawn onto the OpenGL scene. It also tries to do the right thing concerning events: mouse events are sent to whatever is behind the cursor (QGV widget if there is one behind the cursor, otherwise to the GL widget). Keyboard events are sent to whatever has the focus.

It's really easy to use: if you have a toplevel QGLWidget and want to add widgets into it, all you have to do is first create the WidgetProxy object:
WidgetProxy* proxy = new WidgetProxy(myGLWidget);
and then add some widgets:
QWidget* embeddedWindow = proxy->createWindow("Window title"); embeddedWindow->layout()->addWidget(new QLabel("This is an embedded widget!", embeddedWindow));
embeddedWindow->move(10, 10); proxy->addWidget(embeddedWindow);
If the GLWidget is not toplevel but in a layout then the only difference is that you have to add the proxy, not the GLWidget, into the layout.

You can even show() or hide() the GLWidget and it works as before since the WidgetProxy intercepts those events, showing or hiding the QGraphicsView instead.
The aim is to make it possible to have widgets in OpenGL with pretty much a single line of code - often the user of the GL widget doesn't even need to know about it.

The code is now part of my KGLLib, along with an example ("widgets"). KGLLib's code can be found in KDE's SVN, in trunk/playground/libs/kgllib/.

Wednesday, January 2, 2008

New year, KWin and other stuff

First let me wish you all happy new year :-)
2008 is certainly going to be an interesting year for KDE with the next major version finally being released and the focus shifting from refactoring to polishing and using the new APIs. I can't wait to see what people will come up with by the time KDE 4.1 or even 4.2 gets out.

KWin's compositing has seen many improvements in the last months, but we're still not completely sure if we want to enable it by default (even only for the selected cards and drivers). If we enable compositing and it unexpectedly fails for someone then there's the chance of him/her not getting a usable desktop at all and we're not sure if we want to risk with that...
Should we disable it for everyone by default (it's not difficult to enable anyway) and wait until we implement some safeguards for 4.1? Or should we enable it because it works for most people for whom it's enabled by default and end-users are less likely to adopt 4.0 anyway? What do you think?

I've also got a few more interesting things coming up, but I'll keep those for the next time ;-)

Saturday, September 22, 2007

KWin improvements

I first wanted to blog about KWin changes after returning from aKademy but then I never got to doing it. After that I've thought something like "once I'll complete this feature, I'll blog" a few times but then another interesting thing came up...
But finally, here's a summary of cool stuff that has happened in recent months:

One of the most interesting things is compositing settings autodetection. KWin now tries to detect your video driver and it's version and if it's good enough then compositing will be enabled by default. ATM it'll be enabled for NVidia drivers >= 96.39 and for Intel >= 20061017. If you know which settings work for another card/driver then please report it to kwin mailing list or post a comment here.

When you enable compositing yourself or change some of its settings, you'll get a confirmation dialog which will revert back to previous, hopefully working settings if you won't respond in 10 seconds (it's similar to the one you get when changing resolution. In fact there's plan to use exactly the same dialog). ATM it's a bit unreliable in that KWin sometimes crashes when reloading it's settings but hopefully it'll be fixed soon.

Speaking of KWin crashes, we're planning to make KWin restart itself in such cases (just like Plasma or KRunner) and eventually turn off compositing so that you'd always have a usable window manager, even when compositing has some problems.

Then there's the not-so-new-anymore compositing config module where you can turn on compositing and effects. Since a few days ago it also includes the entire list of effects so there's no separate "Window effects" config module for that anymore.

One of the biggest usability complaints - effect shortcuts aren't listed anywhere - is also starting to get solved. I added shortcuts to PresentWindows config module. I'm still not sure if all effects should have a config module just to show the shortcuts or if it would be better to just list the shortcut in effect's description (e.g. "Shows all desktops side by side when Ctrl+F8 is pressed"... or just "Shows all desktops side by side (Ctrl+F8)"). We've still got a little time to figure it out.

There are a few new effects as well. Invert and Sharpen effects were added during aKademy and Looking Glass - a fancier-looking magnifier - a little after it.

Old effects have received some polishing. PresentWindows and BoxSwitch both show window captions and icons. Shadows are nice and round (and unfortunately a bit slow). Some animations are faster, making KWin feel snappier.

I'm not posting any videos this time but if all goes well, you should see one in beta3's announcement.

Sunday, September 2, 2007

Iconcache benchmarking results

In the last three days I've been fixing some last bugs in KPixmapCache, benchmarking it and analyzing the results. Here they are.

It's going to be a long post, so the short story is: the cache reduces KDE startup time by about 1 second (~ 9%), depending on how many and which applications are autostarted. Startup time of individual applications is somewhat reduced as well, but the exact numbers depend on the application (more specifically, on how many icons they use).

I benchmarked KDE startup on two computers: my desktop and laptop.
Desktop is custom built with AMD Athlon 2800+, 1Gb memory and SATA hard drive.
Laptop is Dell Inspiron 6400, with Intel Core Duo T2350 with 2Gb of memory.
Operating system on both computers is Kubuntu Feisty, with all updates.
KDE was pretty much 4.0 beta2, I updated SVN at 29/08 about 1400 UTC.
I used a bit customized Bootchart and kpc-stats patch for benchmarking.

For numerical data I used two things: first, CPU time which is amount of time that system was not idle, gotten from /proc/uptime.
Second, startup time reported by kpc-stats which is time elapsed from when the first KPixmapCache object is created (by kded4 at the very beginning of the KDE session) until when the last icon is found or inserted to the cache (at which point all applications should be started). This can be thought of as a perceived startup time, it shows how long it takes until KDE is ready for use.
I tested with both default KDE startup (referred to as bare KDE) which starts only Plasma, Kicker, KWin, KTip (and background programs) as well as "full KDE" which also starts Konqueror, KBounce, KMPlot and Dolphin and is meant to simulate full KDE session where many programs are started and/or restored.
Every case was ran 3 or 4 times and average time was used. Disk caches were dropped before each run.

The numbers:
CPU time: Bare KDE on desktop computer: 9985 vs 8933 ms, win is 1052 ms or 11,8%. On laptop 6717 vs 6088 ms, win is 630 ms / 10,3%.
Full KDE: desktop - 20636 vs 19525 ms, win is 1111 ms / 5,7%. On laptop - 9722 vs 8968 ms, win: 755 ms / 8,4%.
Startup time: Bare KDE on desktop - 13810 vs 12572 ms, win is 1237 ms / 9,8%. On laptop - 9889 vs 9075 ms, win: 814 ms / 9,0%.
Full KDE: desktop - 24905 vs 23690 ms, win: 1215 ms / 5,1%. Laptop - 13138 vs 11965 ms, win: 1174 ms / 9,8%.

Some bootchart images:

Bare KDE startup on desktop, without (left) and with (right) iconcache.


Full KDE startup on laptop, without and with iconcache.

You can see from the numbers that the icon cache gives almost 10% improvement. Another interesting observation is that improvement in full KDE session isn't much bigger than in bare KDE. On desktop the improvement is pretty much the same, only in laptop is it a bit bigger with full session. Possible reason is that laptop's CPU is faster and thus I/O becomes bigger bottleneck, especially as it's HDD is also slower (5400 vs 7200 rpm).
Most of the 1-second improvement probably comes from the fact that icon theme initialization, which scans all icon dirs, can be skipped when icon cache is present because needed info is already stored in the iconcache header.
You can visually see the improvement when flipping between bootchart image pairs. On with-iconcache images, the later part of the graph has shifted about 1-1.5 seconds forward. You can also see there that the 1-second improvement comes from kded process which is where the expensive icontheme initialization takes place.

I didn't benchmark the applications again since I have already done so earlier (and the numbers shouldn't be really different) and because it was more painful to do than benchmarking full KDE.
Only exception was Dolphin which was the first app I tried to benchmark (after that one I didn't bother with the rest) and which showed a 400ms (11.4 vs 11.0 sec) improvement in cpu time and 1.5 sec (12.6 vs 11.1 sec) improvement in startup time. This fits with my earlier benchmarks quite well. Also note that the earlier benchmarks were all done on the laptop while this one was done on desktop, that's why the absolute times are so different.

Last but not least, keep in mind that absolute performance improvements aren't the only advantage the icon cache brings. It also makes some things possible which would have been way too slow without it. Also there's the generic pixmap cache which is already being used in quite a few games to speed up rendering of game elements and background from SVGs.

And finally, since this is quite likely my last GSoC-related post, I'd like to thank my mentors and the whole KDE community for helping and supporting me as well as Google for organizing the SoC and supporting open-source development.
Keep up the great work everyone! :-)

Tuesday, August 28, 2007

KDE OpenGL library?

Lately I've gotten the feeling that OpenGL is really going mainstream, including in KDE. We already have quite a few apps that are using it and it looks like more are coming.
However ATM many of those apps are reinventing the wheel by writing same generic code such as a texture class over and over again.

So I got the idea of creating some kind of KDE OpenGL library. It would have Qt-style API that KDE developers would be familiar with and would provide some common building blocks that would ease development of OpenGL apps. Right now I'm thinking texture and shader classes, maybe a camera class, some common functions, vector library (Eigen should be great for that) and perhaps an extension library (GLEW could be used). I don't want too many things though, it should still stay lightweight instead of becoming yet another fully featured 3d engine.
And it should be extensible. If your app needs e.g. cubemap textures, you should be able to write a CubemapTexture class that inherits from Texture class and provides necessary functionality. Such extensions could even be put into a common place so that other apps could copy them into their own source tree and use them without bloating the library.
The library probably needn't depend on KDE itself, just on Qt. This might make it interesting for pure Qt apps as well.

What do you think of that idea? Would you use such a library? Or know someone/something that might? Add your comments here.

Wednesday, August 22, 2007

Iconcache is Done :-)

The coding phase of the Summer of Code is now over (for two days already).
Within the last days I managed to add mmap-implementation which instead of opening/reading/closing cache files all the time maps them into shared memory and then just uses that memory. It seems to increase performance (of reading icons from the cache) by about 30%. More than I anticipated.

In the end I managed to get in all the features and ideas that I had, so the work on the icon/pixmap cache can be considered complete now. There might of course be some smaller performance tweaking and bugfixing and apidocs additions and so on, but I don't expect to do any bigger changes.
It also means that if you see a bug concerning icon loading or pixmap cache (I hope you won't ;-) then report it. Either contact me directly or add a comment here or whatever.

I still haven't gotten to doing the final benchmarking... maybe at the end of this week or in the next one...

Friday, August 17, 2007

Iconcache status

Summer of Code is ending (the official deadline for writing code is 20th August) and so my work on the icon cache is also coming to an end.

Yesterday I committed the last crucial piece of code. The cache now periodically checks for icon theme updates, so as soon as you install some new icons, they should be used and the obsolete cache be discarded.
This means that the cache is now fd.o icontheme spec-compliant and all the necessary features should be implemented :-)

Some smaller things remain to be done, such as further performance improvements and final benchmarking. I hope to complete the code part in the coming days and most of benchmarking will probably be done after the 20th.

Speaking of benchmarks, I did some a few days ago. As you have probably heard already, perhaps the most extreme case is kfind which starts up in about 4 secs without the cache and in a bit less than 1 sec (of which just 300ms is icon loading) with the cache. That's with hot caches (i.e. operating system has cached some of the data already). With cold caches it's 12secs vs 2.5secs.
Dolphin uses quite a few icons as well. With cold caches it takes 3.9 secs with iconcache and 5.2 secs without it to start. With hot caches it's 1.2 vs 1.5 secs.

I also noticed about one second improvement (12 vs 13 secs) in full KDE session startup time (with Dolphin, Konqueror, Konsole and KBounce autostarted). I have to admit it's a bit less than I had hoped for, but KDE's startup time will probably be improved prior to 4.0 anyway. Anyway I will make further benchmarks to find out more accurate numbers.

The times are measured from when KPixmapCache contructor is called (which should be at most 100ms after the program is started) until the last find() call (after which the application should be ready to be used).