Introduction to framebuffer

Source: Internet
Author: User

Framebuffer is a driver interface that appears in the 2.2.xx kernel. Linux works in protection mode, so user-mode processes cannot use the interrupt call provided in the graphics card BIOS as DOS to directly write the screen, linux abstracts the framebuffer device for the user-state process to directly write the screen. The framebuffer mechanism imitates the function of the video card, abstracts the hardware structure of the video card, and can directly perform operations on the video memory through the read and write operations of framebuffer. Users can regard framebuffer as an image showing the memory. After ing it to the process address space, they can directly perform read/write operations, and write operations can immediately respond to the screen. Such operations are abstract and unified. Users do not have to worry about the location of physical display, page feed mechanism, and other details. These are all driven by the framebuffer device.

Framebuffer itself does not have any ability to operate data, so it is better than a pool that temporarily stores water. the CPU puts the result after calculation into this pool, and the pool then streams the result to the display. data is not processed in the middle. the application can also directly read and write the contents of this pool. in this mechanism, although framebuffer requires the support of the real graphics driver, all display tasks are completed by the CPU, so the CPU burden is very heavy.

The frame buffering driver is widely used. In Linux desktop systems, the X Window server uses frame buffering to draw windows. In particular, frame buffering allows the display of Chinese character lattice, making it the only feasible solution for Chinese Linux.

In the developer's opinion, framebuffer is essentially a display cache. Writing a special data block to the display cache means outputting content to the screen. So framebuffer is a whiteboard. For example, for a 16-bit framebuffer, the two bytes in framebuffer represent a point on the screen, from top to bottom, from left to right, the linear relationship between screen position and memory address is sequential.

The frame cache can be anywhere in the system memory. The Video Controller refreshes the screen by accessing the frame cache. Frame cache is also called frame buffer or refresh buffer. The frame here refers to the entire screen range.

The frame cache has an address in the memory. By constantly writing data to the frame buffer, the display controller automatically retrieves data from the frame buffer and displays the data. All images share the same frame cache.

When the CPU specifies that the display controller works, the display controller obtains data and commands from the CPU control to the specified place. The current data is generally retrieved from the display memory. If the Display memory does not exist, it can be retrieved from the memory, and the memory can not be stored, but from the hard disk. Of course, it is not the memory, but to save the memory, it can be stored in the hard disk, then, use the command control to display the Controller for retrieval. The content stored in the frame buffer in the frame cache is one frame at a time, and the video card will constantly refresh the frame buffer. If this frame is not captured, it will be discarded, that is, it is real-time. Each frame, whether Stored in the memory or in the video memory, is an explicit message, which is assumed to be a resolution of 800x600, 800x600 pixels and color values are saved.

How to enable framebuffer

Check whether the kernel supports framebuffer and check whether the/proc/FB file exists. If the file exists, it is supported. Otherwise, it is not supported. Check whether the framebuffer device is activated. If the/dev/fbx file exists, the device is activated. Otherwise, the device is not activated.

When the system starts, you can transmit the VGA = mode-number parameter to the kernel to activate the framebuffer device, such as VGA = 0x314, the 800*600 * 16bpp mode will be enabled. To enable the framebuffer device by default in Linux, you need to set/etc/grub. conf is changed to the following format:

# Grub. conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# Notice: you do not have a/boot partition. This means that
# All kernel and initrd paths are relative to/, eg.
# Root (hd0, 0)
# Kernel/boot/vmlinuz-version Ro root =/dev/sda1
# Initrd/boot/initrd-version.img
# Boot =/dev/SDA
Default = 0
Timeout = 10
Splashimage = (hd0, 0)/boot/GRUB/splash.xpm.gz
Title Red Hat Linux (2.4.18-14)
Root (hd0, 0)
Kernel/boot/vmlinuz-2.4.18-14 Ro root = label =/HDC = ide-scsi vga = 0x314
Initrd/boot/initrd-2.4.18-14.img

0x314 indicates 800*600 * 16bpp. For other values, see the following table:
Color 640x400 640x480 800x600 1024x768 1280x1024 1600x1200
4 bits ? ? Zero X 302 ? ? ?
8 bits Zero X 300 Zero X 301 Zero X 303 Zero X 305 Zero X 307 0x31c
15 bits ? Zero X 310 Zero X 313 Zero X 316 Zero X 319 0x31d
16 bits ? Zero X 311 Zero X 314 Zero X 317 0x31a 0x31e
24 bits ? Zero X 312 Zero x 315 Zero X 318 0x31b 0x31f
32 bits ? ? ? ? ? ?

After the framebuffer device is enabled, a penguin is displayed in the upper-left corner of the screen when the system is restarted.

How to program framebuffer operations

The device file corresponding to the frame buffer device is/dev/FB *. If the system has multiple display cards, Linux supports multiple frame buffer devices, up to 32, they are/dev/fb0 to/dev/fb31 respectively, while/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 primary device number is 29, and the secondary device number ranges from 0 to 31, corresponding to/dev/fb0 to/dev/fb31 respectively. Through/dev/FB, application operations mainly include:

  1. Read/write/dev/FB file: equivalent to the read/write screen buffer. Use the seek interface to locate the read/write location and read and write specific data through the read/write interface;
  2. Map operation: since Linux is working in protection mode, each application has its own virtual address space, and the physical buffer address cannot be directly accessed in the application. Therefore, Linux provides the MMAP function in the file_operations structure for file operations, which can map the file content to the user space. For frame buffer devices, you can map the physical address of the screen buffer to a virtual address in the user space, then, the user can access the screen buffer by reading and writing the virtual address, and draw a picture on the screen. In fact, all applications that use frame buffering devices use ing operations to display images. Since all the ing operations are completed by the kernel, we will see below that there is not much work left by the frame buffer driver for developers.
  3. I/O Control: For frame buffering devices, IOCTL operations on device files can be performed to read/set the parameters of the display device and screen, such as resolution, number of colors, and screen size. IOCTL operations are performed by the underlying driver.

In applications, the framebuffer device is usually used by ing the framebuffer device to the process address space. For example, the following program opens the/dev/fb0 device and performs address ing through the MMAP system call, then use memset to clear the screen (Here we assume the display mode is 1024x7688-bit color mode, linear memory mode ):

Int FB;
Unsigned char * fb_mem;
Fb = open ("/dev/fb0", o_rdwr );
Fb_mem = MMAP (null, 1024*768, prot_read | prot_write, map_shared, FB, 0 );
Memset (fb_mem, 0, 1024*768 );

The framebuffer device also provides several IOCTL commands to obtain some fixed information about the display device (for example
Display memory size), variable information related to the display mode (such as resolution, pixel structure, byte width per scanned line), and
Color Palette information in pseudo-color mode.

The framebuffer device can also obtain the types of accelerated display cards supported by the current kernel (obtained through fixed information ),
This type is usually related to a specific display chip. For example, the latest kernel (2.4.9) includes,
Acceleration of popular display chips, such as nvidia and 3dfx. After obtaining the acceleration chip type, the application can
Memory I/O (memio) ing to the address space of the process. These memio are generally used to control the registers of the display card.
These register operations allow the application to control the acceleration function of a specific video card.

The PCI device can map its own control registers to the physical memory space. Then, access to these control registers becomes
Physical memory access. Therefore, these registers are also called "memio ". Once mapped to the physical memory, a common Linux Process can
Map these memory I/O to the process address space through MMAP, so that you can directly access these registers.

Of course, because different display chips have different acceleration capabilities and their use and definitions for memio are different, you need
Write different types of acceleration chips to implement different acceleration functions. For example, most chips provide hardware acceleration for Rectangle filling,
But different chip implementations are different. In this case, you need to write different functions for filling the rectangle for different chip types.

Framebuffer is only a device that provides Display memory and display chip registers ing from physical memory to process address space. So,
For applications, If You Want To program graphics on framebuffer, you also need to do a lot of work by yourself.
Tool cat

/Dev/FB is a file, so we can read and write it.

CAT/dev/fb0> screensnap.txt/* read current sreen to a file */
Cat screensnap.txt>/dev/fb0/* Paste the content of screensnap.txt to the screen */


Dd

Run the following command to clear the screen:
Dd If =/dev/Zero of =/dev/FB
If the display mode is 1024x768-8, run the following command to clear the screen:
Dd If =/dev/Zero of =/dev/fb0 BS = 1024 COUNT = 768
Run the following command to save the content in FB and write it back to the screen:
Dd If =/dev/FB of = fbfile
Dd If = fbfile of =/dev/FB


Fbset

Fbset is a tool for viewing and setting framebuffer. For more information, see the manual.



What is framebuffer?Favorites

Framebuffer is a driver interface that appears in the 2.2.xx kernel. This interface will display
The backup is abstracted as the frame buffer. Users can regard it as an image for displaying memory and map it to the process location.
After the address space, you can directly perform read/write operations, and write operations can immediately respond to the screen. This driver
The device files of the program are generally/dev/fb0,/dev/Fb1, and so on. For example, assume that the current display mode is
To clear the screen, run the following command:

$ Dd If =/dev/Zero of =/dev/fb0 BS = 1024 COUNT = 768
In applications, You can map the framebuffer device to the process address space,
For example, the following program opens the/dev/fb0 device and uses the MMAP system to call the address ing.
Then use memset to clear the screen (Here we assume the display mode is 1024x7688-bit color mode, linear memory
Mode): int FB;
Unsigned char * fb_mem; Fb = open ("/dev/fb0", o_rdwr );
Fb_mem = MMAP (null, 1024*768, prot_read | prot_write, map_shared, FB, 0); memset (fb_mem, 0, 1024*768 ); the framebuffer device also provides several IOCTL commands to obtain the display device.
Some fixed information (such as the Display memory size), variable information related to the display mode (such as resolution
Rate, pixel structure, byte width of each scanned line), and color palette information in pseudo-color mode.
Through the framebuffer device, you can also obtain the type of the acceleration display card supported by the current kernel (through
Fixed information). This type is usually related to a specific display chip. For example, the latest kernel
(2.4.9), including acceleration of popular display chips such as S3, matrox, NVIDIA, and 3dfx
Yes. After obtaining the acceleration chip type, the application can change the memory I/O of the PCI device.
(Memio) maps to the address space of the process. These memio are generally used to control the registers of the display card,
By operating these registers, the application can control the acceleration function of a specific video card. PCI devices can map their own control registers to the physical memory space.
To the physical memory. Therefore, these registers are also called "memio ". Once
After being mapped to the physical memory, common processes in Linux can map these memory I/O
Process address space, so that you can directly access these registers. Of course, because different display chips have different acceleration capabilities, the use and definition of memio are also different.
At this time, you need to write different types of acceleration chips to implement different acceleration functions. For example, most
The number chip provides hardware acceleration for Rectangle filling, but different chip implementation methods are different,
You need to write different functions for filling the rectangle for different chip types. Speaking of this, the reader may have realized that framebuffer is only a display memory and display chip.
Registers are mapped from physical memory to devices in the process address space. Therefore, for applications, if
To program graphics on framebuffer, you still need to do a lot of work. For example
Speaking, framebuffer is like a canvas. You still need to know what the paint looks like and how to draw it.
Hands-on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.