Playing with linux framebuffer
The Linux framebuffer is an abstraction layer, hardware independant, to display graphical elements in Linux console. Framebuffer is often used in embedded devices. Thanks to DirectFB , hadware acceleration can be used.
How to program framebuffer ?
Programming the Linux framebuffer is quite simple:
- ioctl commands and structs are located in /usr/include/linux/fb.h
- use open(2) to open /dev/fb0
- use various ioctl(2) calls to get or set properties
- mmap(2) framebuffer device to memory
- write to the mmaped memory to display your graphics
struct fb_var_screeninfo; /* Get variable screen informations with FBIOGET_VSCREENINFO */ struct fb_fix_screeninfo; /* Get fixed screen informations with FBIOGET_FSCREENINFO */
A simple program
Return codes are not checked for simplicity.
#include #include #include
First, open the framebuffer device,
int fbfd; fbfd = open("/dev/fb0", O_RDWR);
Get various screen informations with ioctl ,
static struct fb_fix_screeninfo finfo; static struct fb_var_screeninfo vinfo; ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo); ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo); fprintf(stdout, "Fixed screen informations\n" "-------------------------\n" "Id string: %s\n" "FB start memory: %p\n", finfo.id, (void *)finfo.smem_start); fprintf(stdout, "Variable screen informations\n" "----------------------------\n" "xres: %d\n" "yres: %d\n" "xres_virtual: %d\n" "yres_virtual: %d\n" "bits_per_pixel: %d\n", vinfo.xres, vinfo.yres, vinfo.xres_virtual, vinfo.yres_virtual, vinfo.bits_per_pixel);
screen_size = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; fbp = mmap(NULL, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
Yon can now draw a green rectangle in console mode
int x = 20; int y = 20; /* Upper left corner */ int w = 40; int h = 60; /* Width and height */ for (int j = 0; j h; ++j) < for (int i = 0; i w; ++i) < unsigned char *p; p = fbp + ((y + j) * vinfo.xres + (x + i)) * vinfo.bits_per_pixel/8; /* We consider a 32 bits per pixels */ p[0] = 0; /* blue */ p[1] = 255; /* green */ p[2] = 0; /* red */ p[3] = 0; /* alpha */ > >
Do not forget to release allocated resources,
Links
Frame Buffer Library¶
The frame buffer drivers depend heavily on four data structures. These structures are declared in include/linux/fb.h. They are fb_info, fb_var_screeninfo, fb_fix_screeninfo and fb_monospecs. The last three can be made available to and from userland.
fb_info defines the current state of a particular video card. Inside fb_info, there exists a fb_ops structure which is a collection of needed functions to make fbdev and fbcon work. fb_info is only visible to the kernel.
fb_var_screeninfo is used to describe the features of a video card that are user defined. With fb_var_screeninfo, things such as depth and the resolution may be defined.
The next structure is fb_fix_screeninfo. This defines the properties of a card that are created when a mode is set and can’t be changed otherwise. A good example of this is the start of the frame buffer memory. This «locks» the address of the frame buffer memory, so that it cannot be changed or moved.
The last structure is fb_monospecs. In the old API, there was little importance for fb_monospecs. This allowed for forbidden things such as setting a mode of 800×600 on a fix frequency monitor. With the new API, fb_monospecs prevents such things, and if used correctly, can prevent a monitor from being cooked. fb_monospecs will not be useful until kernels 2.5.x.
Frame Buffer Memory¶
registers a frame buffer device
frame buffer info structure
Registers a frame buffer device fb_info.
Returns negative errno on error, or zero for success.
releases a frame buffer device
frame buffer info structure
Unregisters a frame buffer device fb_info.
Returns negative errno on error, or zero for success.
This function will also notify the framebuffer console to release the driver.
This is meant to be called within a driver’s module_exit() function. If this is called outside module_exit() , ensure that the driver implements fb_open() and fb_release() to check that no processes are using the device.
low level driver signals suspend
0 = resuming, !=0 = suspending
This is meant to be used by low level drivers to signal suspend/resume to the core & clients. It must be called with the console semaphore held
Frame Buffer Colormap¶
frame buffer colormap structure
Deallocates a colormap that was previously allocated with fb_alloc_cmap().
const struct fb_cmap *from
frame buffer colormap structure
frame buffer colormap structure
Copy contents of colormap from from to to.
frame buffer colormap structure
frame buffer info structure
Sets the colormap cmap for a screen of device info.
Returns negative errno on error, or zero on success.
size of palette for a depth
Gets the default colormap for a specific screen depth. len is the size of the palette for a particular screen depth.
Returns pointer to a frame buffer colormap structure.
invert all defaults colormaps
Description
Invert all default colormaps.
Frame Buffer Video Mode Database¶
int fb_try_mode ( struct fb_var_screeninfo * var , struct fb_info * info , const struct fb_videomode * mode , unsigned int bpp ) ¶
struct fb_var_screeninfo *var
frame buffer user defined part of display
frame buffer info structure
const struct fb_videomode *mode
frame buffer video mode structure
color depth in bits per pixel
Tries a video mode to test it’s validity for device info.
removed videomode entry from modelist
const struct fb_videomode *mode
struct list_head of modelist
Will remove all matching mode entries
int fb_find_mode ( struct fb_var_screeninfo * var , struct fb_info * info , const char * mode_option , const struct fb_videomode * db , unsigned int dbsize , const struct fb_videomode * default_mode , unsigned int default_bpp ) ¶
struct fb_var_screeninfo *var
frame buffer user defined part of display
frame buffer info structure
string video mode to find
const struct fb_videomode *db
size of db
const struct fb_videomode *default_mode
default video mode to fall back to
default color depth in bits per pixel
Description
Finds a suitable video mode, starting with the specified mode in mode_option with fallback to default_mode. If default_mode fails, all modes in the video mode database will be tried.
Valid mode specifiers for mode_option:
with , , and decimal numbers and a string.
If ‘M’ is present after yres (and before refresh/bpp if present), the function will compute the timings using VESA(tm) Coordinated Video Timings (CVT). If ‘R’ is present after ‘M’, will compute with reduced blanking (for flatpanels). If ‘i’ or ‘p’ are present, compute interlaced or progressive mode. If ‘m’ is present, add margins equal to 1.8% of xres rounded down to 8 pixels, and 1.8% of yres. The char ‘i’, ‘p’ and ‘m’ must be after ‘M’ and ‘R’. Example:
1024x768MR-8@60m - Reduced blank with margins at 60Hz.
Returns zero for failure, 1 if using specified mode_option, 2 if using specified mode_option with an ignored refresh rate, 3 if default mode is used, 4 if fall back to any valid mode.
The passed struct var is _not_ cleared! This allows you to supply values for e.g. the grayscale and accel_flags fields.
convert fb_var_screeninfo to fb_videomode
pointer to struct fb_videomode
const struct fb_var_screeninfo *var
pointer to struct fb_var_screeninfo
convert fb_videomode to fb_var_screeninfo
struct fb_var_screeninfo *var
pointer to struct fb_var_screeninfo
const struct fb_videomode *mode
pointer to struct fb_videomode
const struct fb_videomode *mode1
const struct fb_videomode *mode2
const struct fb_videomode * fb_find_best_mode ( const struct fb_var_screeninfo * var , struct list_head * head ) ¶
find best matching videomode
const struct fb_var_screeninfo *var
pointer to struct fb_var_screeninfo
pointer to struct list_head of modelist
struct fb_videomode, NULL if none found
Description
IMPORTANT: This function assumes that all modelist entries in info->modelist are valid.
Finds best matching videomode which has an equal or greater dimension than var->xres and var->yres. If more than 1 videomode is found, will return the videomode with the highest refresh rate
const struct fb_videomode * fb_find_nearest_mode ( const struct fb_videomode * mode , struct list_head * head ) ¶
const struct fb_videomode *mode
pointer to struct fb_videomode
Description
Finds best matching videomode, smaller or greater in dimension. If more than 1 videomode is found, will return the videomode with the closest refresh rate.
const struct fb_videomode * fb_match_mode ( const struct fb_var_screeninfo * var , struct list_head * head ) ¶
find a videomode which exactly matches the timings in var
const struct fb_var_screeninfo *var
pointer to struct fb_var_screeninfo
pointer to struct list_head of modelist
struct fb_videomode, NULL if none found
adds videomode entry to modelist
const struct fb_videomode *mode
struct list_head of modelist
Will only add unmatched mode entries
struct list_head of modelist
void fb_videomode_to_modelist ( const struct fb_videomode * modedb , int num , struct list_head * head ) ¶
convert mode array to mode list
const struct fb_videomode *modedb
array of struct fb_videomode
number of entries in array
struct list_head of modelist
Frame Buffer Macintosh Video Mode Database¶
converts vmode/cmode pair to var structure
struct fb_var_screeninfo *var
frame buffer video mode structure
Converts a MacOS vmode/cmode pair to a frame buffer video mode structure.
Returns negative errno on error, or zero for success.
Convert monitor sense to vmode
Macintosh monitor sense number
Converts a Macintosh monitor sense number to a MacOS vmode number.
Returns MacOS vmode video mode number.
int mac_find_mode ( struct fb_var_screeninfo * var , struct fb_info * info , const char * mode_option , unsigned int default_bpp ) ¶
struct fb_var_screeninfo *var
frame buffer user defined part of display
frame buffer info structure
video mode name (see mac_modedb[])
default color depth in bits per pixel
Finds a suitable video mode. Tries to set mode specified by mode_option. If the name of the wanted mode begins with ‘mac’, the Mac video mode database will be used, otherwise it will fall back to the standard video mode database.
Function marked as __init and can only be used during
Returns error code from fb_find_mode (see fb_find_mode function).
Frame Buffer Fonts¶
Refer to the file lib/fonts/fonts.c for more information.