Contents

Plasma Wallpaper Tutorial 3 - Double Buffer

Introduction

So far we have ignored the exposedRect argument of our paint function. Whenever the paint function was called we redrew the entire wallpaper even when only a small region needed repainting. As you can imagine this is not very efficient so let's improve our wallpaper by using a so-called 'double buffer' technique.

Adding the Buffer

The way this double buffer technique works is by rendering our wallpaper to a pixmap in memory, the buffer, instead of rendering it directly to the screen. Whenever a region on the screen needs repainting we can just copy the corresponding region from the buffer to the screen.

First we'll need to add the buffer to our private variables and write a new function for rendering the buffer. Add the following lines to our tutorial.h:

If the buffer has the wrong size or if the buffer doesn't exist yet (size = 0px*0px) we create a new QPixmap with the same size as our wallpaper. We then associate a QPainter object with this buffer and draw with the QPainter as before.

Using the Buffer

We will now need to change the paint function to make use of our buffer.

If the buffer has the wrong size or if the buffer doesn't exist yet (size = 0px*0px) we call our render function to fill the buffer with the correct content. When our buffer is up-to-date we copy the exposedRect region from the buffer to the painter.

We'll also need to update the buffer, by calling our render function, whenever the content of the wallpaper changes. Add the following lines to the init and settingsModified functions in our tutorial.cpp: