I'm currently writing a program that involves graphics. After some thought I decided to write directly to the frame buffer on /dev/fb0 and the code is working great but the writing speed is slow. It takes 0.161s to write a blank screen (0.213s is the program with the fb0 writing and 0.052s is the program without writing to fb) which means 6fps without any 3d rendering. Is there a way to write faster to a file like the /dev/fb0 on C? I'm currently using fb = freopen("/dev/fb0","w",stdout);
to open the file and the regular printf
to write to the file, also the display is 320x240.
Thanks :)
1 Answer
You can map the framebuffer device into memory using mmap()
and blit to and from it with memcpy()
or pointers. Unless you are running X windows, in which case you need to go through an API such as X11, OpenGL or SDL.
-
betteros.org/tut/graphics1.php#dumb is a tutorial with more details. One caveat: it just takes a guess about what buffer size is “more than enough space and assume[s] that it will always be enough.” Don’t do that with external interfaces that are meant to support every piece of graphics hardware that will ever be created.– DavislorCommented May 29, 2017 at 6:13
printf
- write into a memory buffer and blit it usingfwrite
. 3. Ensure you are using optimizations (-O2
or-O3
). In any case, you should show your actual code, not just try to describe what you're doing. Maybe you're doing something insane like usingprintf
to write one character at a time.fwrite
instead of printing one character at a time withprintf
my program went from 0.213s total to 0.034s. Thanks a lot, do you want to post it as an answer so I can accept it as the answer?