How to set a video mode through libdrm (Part 2)

This second tutorial on #libdrm / #kms madness is concerned with . Page flipping is the slender brother of . Both require two buffers with the dimensions of the screen to reduce flickering. Using double buffering, there is a fixed (aka backbuffer) and a fixed . Once everything is rendered into the double buffer its contents get flushed, for example with #memcpy , into the #framebuffer. Using page flipping the role of frame and double buffer change every flush, meaning once rendering to the double buffer is complete, it becomes the framebuffer, and the current framebuffer becomes the double buffer. This has the advantage of not having to copy a large chunk of memory, yet it can be only used when the whole screens gets rendered every frame, as each buffer contains the second to last state after each flip.

So, first we need a second framebuffer. Four steps need to be done here: create, map, memory map and add:

for convenience of usage also the user buffers should be swapped with something like:

frame.swap(back);

Using this as-is will probably result in really bad flickering. Since we have no idea when a flip is complete, we just keep on rendering and flipping, no matter if there is a flip in progress. To determine if a flip is complete we ask the device in charge:

This completes the toolbox for page flipping with libdrm / kms. Yet there is one string attached. Having a memory mapped double buffer requires you to make sure nothing gets rendered beyond its bounds, the executable will segfault. On the upside, it is a free memory check.