Linux驱动修炼之道-framebuffer(下)

努力成为linux kernel hacker的人李万鹏原创作品,为梦而战。转载请标明出处

http://blog.csdn.net/woshixingaaa/archive/2011/05/29/6452690.aspx

用户写屏程序:

#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> #include <linux/fb.h> #include <sys/mman.h> #include <sys/ioctl.h> int main() { int fbfd = 0; struct fb_var_screeninfo vinfo; struct fb_fix_screeninfo finfo; long int screensize = 0; char *fbp = 0; int x = 0, y = 0; long int location = 0; // Open the file for reading and writing fbfd = open("/dev/fb0", O_RDWR); if (fbfd == -1) { perror("Error: cannot open framebuffer device"); exit(1); } printf("The framebuffer device was opened successfully./n"); // Get fixed screen information if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) { perror("Error reading fixed information"); exit(2); } // Get variable screen information if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) { perror("Error reading variable information"); exit(3); } printf("%dx%d, %dbpp/n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel); // Figure out the size of the screen in bytes screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; // Map the device to memory fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0); if ((int)fbp == -1) { perror("Error: failed to map framebuffer device to memory"); exit(4); } printf("The framebuffer device was mapped to memory successfully./n"); x = 100; y = 100; // Where we are going to put the pixel // Figure out where in memory to put the pixel for (y = 100; y < 300; y++) for (x = 100; x < 300; x++) { location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length; if (vinfo.bits_per_pixel == 32) { *(fbp + location) = 100; // Some blue *(fbp + location + 1) = 15+(x-100)/2; // A little green *(fbp + location + 2) = 200-(y-100)/5; // A lot of red *(fbp + location + 3) = 0; // No transparency } else { //assume 16bpp int b = 10; int g = (x-100)/6; // A little green int r = 31-(y-100)/16; // A lot of red unsigned short int t = r<<11 | g << 5 | b; *((unsigned short int*)(fbp + location)) = t; } } munmap(fbp, screensize); close(fbfd); return 0; }

这个是网上流行的用户测试程序,总结一下用户程序写屏步骤:
1)打开设备节点
2)获得fb_info的固定参数与可变参数结构体
3)计算帧缓冲区大小
4)调用mmap将缓冲区映射到进程的地址空间
5)写屏
这里注意fb_var_screeninfo中的可见分辨率xres,yres是屏的实际大小,即320,240。而虚拟分辨率xres_virtual,yres_virtual是虚拟窗口的大小。但是在s3c2440中被设为相等的。看源码:
s3c24xxfb_probe函数中:

fbinfo->var.xres = display->xres; fbinfo->var.yres = display->yres; fbinfo->var.bits_per_pixel = display->bpp;

s3c2410fb_check_var函数中:

var->xres_virtual = display->xres; var->yres_virtual = display->yres;

所以xres=xres_virtual,yres=yres_virtual。
而实际的xres_virtual=xres + 2 * x_offset,yres_virtual = yres + 2 * y_offset。可以看s3c2440的datasheet,有一个图,

Linux驱动修炼之道-framebuffer(下)

看那个for循环,如果是在PC机上当然就是32BPP,R,G,B分别一个字节,另外一个字节空着或者干其他的事情,比如这里就用来表示透明度。如果在开发板上运行就是16BPP,在开发板上运行要改一下x,y的范围。在开发板上运行清屏命令:

dd if=/dev/zero of=/dev/fb0 bs=320 count=240

然后运行效果如下:

Linux驱动修炼之道-framebuffer(下)