See:
http://blog.ednchina.com/exbob/254847/message.aspx
Frame buffer device for Linux
frame buffer (framebuffer) is an interface that Linux provides for display devices, a device that abstracts the video memory, allowing the upper application to read and write directly to the display buffer in graphics mode. This operation is abstract and unified. Users do not have to care about the physical memory of the location, page-changing mechanism and so on specific details. These are done by the framebuffer device driver. Frame buffer driver is widely used in Linux desktop system, Xwindow server uses frame buffer to draw window. In particular, the frame buffer can display the Chinese character lattice, which becomes the only feasible scheme for the localization of Linux.
frame buffer device corresponding to the device file for/dev/fb*, if the system has more than one display card, Linux can also support a number of frame buffer devices, up to the number of, respectively,/dev/fb0 to/dev/fb31, and/DEV/FB is the current default frame buffer device , usually pointing to/dev/fb0. Of course, it is enough to support a display device in an embedded system. The frame buffer device is a standard character device, the main device number is 29, and the secondary device number is 0 to 31. correspond to/dev/fb0-/dev/fb31 respectively.
With /DEV/FB, there are several main operations of the application:
1. Read/write (read/write)/DEV/FB: equivalent to a read/write screen buffer. For example, the CP/DEV/FB0 tmp command allows the contents of the current screen to be copied to a file, while the command CP tmp >/DEV/FB0 Displays the graphics file tmp on the screen.
2. Mapping (MAP) operations: Because Linux works in protected mode, each application has its own virtual address space, and the physical buffer address is not directly accessible in the application. To do this, Linux provides a mmap function in the file_operations structure of file operations, which maps the contents of a file to user space. For frame buffering devices, the mapping operation allows the physical address of the screen buffer to be mapped to a virtual address in user space, after which the user can then draw on the screen by reading and writing the virtual address to the screen buffer.
3. I/O control: For frame buffer devices, the IOCTL operation of the device files can read/Set the display device and screen parameters, such as resolution, display color number, screen size and so on. The IOCTL operation is done by the underlying driver.
in your application, the general steps for manipulating/DEV/FB are as follows:
1. Open the/DEV/FB device file.
2. Use the Ioctrl operation to get the parameters of the current display screen, such as screen resolution, the number of bits per pixel. Calculate screen buffering based on screen parameters
the size of the zone.
3. Maps the screen buffer to user space (mmap).
4. After mapping, you can read and write directly to the screen buffer for drawing and picture display.
Typical program sections are as follows:
------------------------
#include <linux/fb.h>
int main ()
{
int fbfd = 0;
struct Fb_var_screeninfo vinfo;
struct Fb_fix_screeninfo finfo;
long int screensize = 0;
/* Open Device File *
/FBFD = open ("/dev/fb0", O_RDWR);
/* Get screen-related parameters * *
IOCTL (FBFD, Fbioget_fscreeninfo, &finfo);
IOCTL (FBFD, Fbioget_vscreeninfo, &vinfo);
/* Calculate screen buffer size * *
screensize = vinfo.xres * vinfo.yres * VINFO.BITS_PER_PIXEL/8;
/* Map screen buffer to user address space
/* fbp= (char*) mmap (0,screensize,prot_read| prot_write,map_shared, FBFD, 0);
/* Below the FBP pointer can read/write buffer ...
/* Release buffer, shut down the device * *
munmap (FBP, screensize);
Close (FBFD);
}
-----------------------
IOCTL Operation
IOCTL (FBFD, Fbioget_fscreeninfo, &finfo)
gets the information about the FB_VAR_SCREENINFO structure, defined in Linux/include/linux/fb.h.
IOCTL (FBFD, Fbioget_vscreeninfo, &vinfo)
gets information about the FB_FIX_SCREENINFON structure. Defined in Linux/include/linux/fb.h.
FBFD is the device file number.
-----------------------
mmap function
function Description:
the MMAP function is a system call under Unix/linux
mmap maps a file or other object into memory. Files are mapped to multiple pages, and if the size of the file is not the sum of all the pages, the space that the last page is not used will be zeroed. Munmap performs the reverse operation and deletes the object mappings for a specific address area.
file-based mapping, the st_atime of the mapped file may be updated at any time during the execution of the mmap and Munmap. If the St_atime field is not updated in the case described earlier, the value of the field is updated when the first page of the mapping area is first indexed. File mappings established with the Prot_write and map_shared flags, whose st_ctime and St_mtime are updated after they are written to the mapping area, but before Msync () is invoked through the Ms_sync and Ms_async two flags.
Usage:
#include <sys/mman.h>
void *mmap (void *start, size_t length, int prot, int flags,
int fd, off_t offset);
int Munmap (void *start, size_t length);
Parameters:
start: The starting address of the mapping area.
Length: The lengths of the mapping area.
Prot: The desired memory protection flag cannot conflict with the open mode of the file. is one of the following values that can be reasonably grouped together by or operation
prot_exec//page content can be executed
prot_read//page content can be read
prot_write//pages can be written
prot_none//Page not accessible
Flags: Specifies the type of mapping object, whether the mapping options and the mapping page can be shared. Its value can be a combination of one or more of the following bits
Map_fixed//Using the specified mapping start address, if the memory area specified by the start and Len parameters overlaps the existing mapping space, the overlapping portions are discarded. If the specified starting address is not available, the operation will fail. And the starting address must fall on the page boundary.
map_shared//shared mapping space with all other processes that map this object. Writing to a shared area is equivalent to outputting to a file. The file will not actually be updated until Msync () or Munmap () is invoked.
Map_private//Creates a private mapping of a write-time copy. Memory area writes do not affect the original file. This flag is mutually exclusive with the above flags and only one of them can be used.
Map_denywrite//This flag is ignored.
map_executable//ditto
Map_noreserve//Do not reserve swap space for this mapping. When the swap space is retained, the modification of the mapping area may be guaranteed. When the swap space is not preserved and there is not enough memory, the modification of the mapping area can cause a section of the offending signal.
map_locked//Lock the mapped area of the page to prevent the page from being swapped out of memory.
Map_growsdown//For stack, tells kernel VM system, map area can be extended downward.
map_anonymous//anonymous mappings, the mapping area is not associated with any files.
Map_anon//map_anonymous's nickname, no longer used.
map_file//Compatibility flag, ignored.
map_32bit//The mapping area is ignored when it is specified in the low 2gb,map_fixed of the process address space. The current logo is only supported on the X86-64 platform.
map_populate//Prepares the page table for file mapping by prefetching. Subsequent access to the map area will not be blocked by the page violation.
Map_nonblock//is only meaningful when used with map_populate. Does not perform prefetching, only creating page table entries for pages that already exist in memory.
FD: A valid file descriptor. If the map_anonymous is set, the value should be-1 for compatibility issues.
offset: The starting point of the content of the mapped object.
Return Description:
upon successful execution, mmap () returns a pointer to the mapped area, Munmap () returns 0.
on Failure, mmap () returns map_failed[with a value of (void *)-1], andMunmap returns-1. Errno is set to one of the following values
eacces: Access error
eagain: File is locked, or too much memory is locked
EBADF:FD is not a valid file descriptive word
einval: One or more arguments are invalid
Enfile: The system has reached the limit on open files
Enodev: The file system where the specified file is located does not support memory mapping
Enomem: Not enough memory, or the process has exceeded the maximum number of memory mappings
eperm: Insufficient power, operation not allowed
Etxtbsy: The written way to open the file and specify the MAP_DENYWRITE flag
SIGSEGV: Trying to write to a read-only area
Sigbus: Trying to access memory areas that are not part of the process
------------------------