Search code examples
linuxgraphicsframebuffer

Linux framebuffer graphics and VSync


I'm making a game in C that works on the linux framebuffer. So far I have a red 100x100 square that moves in tandem with the mouse, just like a pointer. I have implemented double buffering and it works correctly (the square doesn't flicker). The problem is, there is quite a lot of tearing happening horizontally. What I mean by that is, when the square moves on the x axis, it's like it is divided horizontally, and one part of it advances slithgly more than the other. This "rip" slowly propagates from top to bottom on the square if i keep moving it.

I believe it happens because the hardware reads the framebuffer during the moment when I copy the double buffer into the main buffer.

I have tried to combat the problem with the FBIO_WAITFORVSYNC ioctl, but no success.

Any ideas how I could do VSync on the framebuffer? Please add detail to your explanations as this is the first time I am programming such stuff on a Linux based OS so I might not understand otherwise.

Here is my code: http://pastebin.com/KJ4iaVEL


Solution

  • This isn't the correct way to do double-buffering. You're right to do all the painting on a back-buffer, but then you do a memcpy to transfer the data to the front. A screen refresh could easily happen during the copy.

    To do this properly, you should only have to switch a pointer to the data; not copy the data itself. With the Linux framebuffer device, this is done by having a "virtual" screen that is twice as large as the physical screen, and using an offset variable to set whether you're showing the top or bottom half. You can query the size and set offset using the FBIOGET_VSCREENINFO, FBIOPUT_VSCREENINFO, and FBIOPAN_DISPLAY ioctl calls.

    This page briefly gives some details about this: http://www.ummon.eu/Linux/API/Devices/framebuffer.html

    All the relevant data structures are in the linux/fb.h header file.