I initialize my linux framebuffer like this:
#include "framebuffer_init.h"
#include <sys/ioctl.h>
#include <stdint.h> // uint32_t, uint64_t
#include <stddef.h> // size_t
#include <stdio.h> // perror()
#include <unistd.h> // close()
#include <sys/ioctl.h> // ioctl()
#include <sys/mman.h> // mmap(), munmap(), PROT_*, MAP_*
#include <linux/fb.h> // fb_var_screeninfo, fb_fix_screeninfo, FBIOGET_*
#include <fcntl.h> // open(), O_RDWR (if opening framebuffer here)
int framebuffer_init(
int fb,
uint32_t *width,
uint32_t *height,
uint32_t *pitch,
uint32_t *bpp,
uint64_t *size,
void **map
){
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0 ||
ioctl(fb, FBIOGET_FSCREENINFO, &finfo) < 0) {
perror("framebuffer ioctl");
return 0;
}
*width = vinfo.xres;
*height = vinfo.yres;
*bpp = vinfo.bits_per_pixel;
*pitch = finfo.line_length; // bytes per row (was never set)
// Total mapped size: use the virtual height in case of double buffering
*size = (uint64_t)finfo.line_length * vinfo.yres_virtual;
*map = mmap(NULL,*size,PROT_READ | PROT_WRITE,MAP_SHARED,fb,0);
if (*map == MAP_FAILED) {
perror("framebuffer mmap");
*map = NULL;
return 0;
}
return 1;
}
void framebuffer_cleanup(
int *fb_fd,
void **map,
uint64_t size
) {
// Implementation ommited for reduced post verbocity
}
Then I my main:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <cairo.h>
#include "framebuffer_init.h"
#include "cairo_render.h"
int main(void)
{
int fb = open("/dev/fb0", O_RDWR | O_CLOEXEC);
if (fb < 0) {
perror("open framebuffer");
return 1;
}
uint32_t width = 0, height = 0, pitch = 0, bpp = 0;
uint64_t size = 0;
void *map = NULL;
if (!framebuffer_init(fb, &width, &height, &pitch, &bpp, &size, &map)) {
fprintf(stderr, "Framebuffer initialization failed\n");
framebuffer_cleanup(&fb, &map, size);
return 1;
}
cairo_format_t fmt = (bpp == 32) ? CAIRO_FORMAT_RGB24
: CAIRO_FORMAT_RGB16_565;
// Back buffer in normal RAM, with exactly the same layout as the framebuffer
size_t frame_bytes = (size_t)pitch * height;
uint8_t *backbuf = calloc(1, frame_bytes);
if (!backbuf) {
fprintf(stderr, "Back buffer allocation failed\n");
framebuffer_cleanup(&fb, &map, size);
return 1;
}
cairo_surface_t *back = cairo_image_surface_create_for_data(backbuf, fmt, width, height, pitch);
if (cairo_surface_status(back) != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "Back buffer surface creation failed: %s\n",
cairo_status_to_string(cairo_surface_status(back)));
cairo_surface_destroy(back);
free(backbuf);
framebuffer_cleanup(&fb, &map, size);
return 1;
}
cairo_t *cr = cairo_create(back);
const struct timespec frame_delay = { 0, 16 * 1000 * 1000 }; // ~60 FPS
while (true) {
draw_with_cairo(cr);
cairo_surface_flush(back);
// Wait for vblank; if unsupported, fall back to a fixed delay
uint32_t dummy = 0;
if (ioctl(fb, FBIO_WAITFORVSYNC, &dummy) < 0) {
nanosleep(&frame_delay, NULL);
}
// Single copy of the whole frame
memcpy(map, backbuf, frame_bytes);
}
// Cleanup (reached if you add an exit condition to the loop)
cairo_destroy(cr);
cairo_surface_destroy(back);
free(backbuf);
framebuffer_cleanup(&fb, &map, size);
return 0;
}
I initialise the backbuff like this:
size_t frame_bytes = (size_t)pitch * height;
uint8_t *backbuf = calloc(1, frame_bytes);
Whereas I use this function for drawing using cairo:
void draw_with_cairo (cairo_t *cr) {
// Background
cairo_set_source_rgb(cr, 0.05, 0.08, 0.16);
cairo_paint(cr);
// Rectangle
cairo_set_source_rgb(cr, 0.1, 0.6, 0.9);
cairo_rectangle(cr, 100, 100, 400, 200);
cairo_fill(cr);
// Circle
cairo_set_source_rgb(cr, 1.0, 0.3, 0.2);
cairo_arc(cr, 600, 300, 100, 0, 2 * 3.14159265);
cairo_fill(cr);
// Text
cairo_select_font_face(cr, "Sans",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 48);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_move_to(cr, 100, 450);
cairo_show_text(cr, "Cairo + DRM/KMS");
}
I feel like that this calculation:
size_t frame_bytes = (size_t)pitch * height;
Is unnecessery since backbuff should have the same size as mmaped framebuff. The reasin why is because I memcpy the backbuff into actual buffer:
memcpy(map, backbuf, frame_bytes);
Upon drawing loop. Cairo initialization and drawing happen through these functions:
#include "cairo_render.h"
cairo_t* init_cairo(void *map, uint32_t width,uint32_t height,uint32_t pitch, cairo_surface_t **surface ){
*surface =cairo_image_surface_create_for_data(map,CAIRO_FORMAT_ARGB32,width,height,pitch);
return cairo_create(*surface);
}
void draw_with_cairo (cairo_t *cr) {
// Background
cairo_set_source_rgb(cr, 0.05, 0.08, 0.16);
cairo_paint(cr);
// Rectangle
cairo_set_source_rgb(cr, 0.1, 0.6, 0.9);
cairo_rectangle(cr, 100, 100, 400, 200);
cairo_fill(cr);
// Circle
cairo_set_source_rgb(cr, 1.0, 0.3, 0.2);
cairo_arc(cr, 600, 300, 100, 0, 2 * 3.14159265);
cairo_fill(cr);
// Text
cairo_select_font_face(cr, "Sans",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 48);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_move_to(cr, 100, 450);
cairo_show_text(cr, "Cairo + DRM/KMS");
}