FrameBuffer操作入门

来源:互联网 发布:香港记者跑的快 知乎 编辑:程序博客网 时间:2024/05/05 00:26

所有的这些操作,都是在控制台界面下,root登录。

一,先变一个魔法

         $ cat /dev/fb0 > sreensnap      /*获取一屏的数据*/  

         $ clear                                         /*清楚屏幕的输出*/

         $ cat sreensnap > /dev/fb0     /*将刚才的屏幕数据显示*/


二,操作/dev/fb0

        1)查看/dev/fb0 的信息

[html] view plain copy
  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3. #include <fcntl.h>  
  4. #include <linux/fb.h>  
  5. #include <sys/mman.h>  
  6. #include <stdlib.h>  
  7.   
  8. int main ()   
  9. {  
  10.      int fp=0;  
  11.      struct fb_var_screeninfo vinfo;  
  12.      struct fb_fix_screeninfo finfo;  
  13.      fp = open ("/dev/fb0",O_RDWR);  
  14.   
  15.      if (fp < 0){  
  16.       printf("Error : Can not open framebuffer device/n");  
  17.       exit(1);  
  18.      }  
  19.   
  20.      if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo)){  
  21.       printf("Error reading fixed information/n");  
  22.       exit(2);  
  23.      }  
  24.        
  25.      if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo)){  
  26.       printf("Error reading variable information/n");  
  27.       exit(3);  
  28.      }  
  29.   
  30.      printf("The mem is :%d\n",finfo.smem_len);  
  31.      printf("The line_length is :%d\n",finfo.line_length);  
  32.      printf("The xres is :%d\n",vinfo.xres);  
  33.      printf("The yres is :%d\n",vinfo.yres);  
  34.      printf("bits_per_pixel is :%d\n",vinfo.bits_per_pixel);  
  35.      close (fp);  
  36. }  

        2)改变屏幕上某一个点的颜色

[html] view plain copy
  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <fcntl.h>  
  5. #include <linux/fb.h>  
  6. #include <sys/mman.h>  
  7.   
  8. int main ()   
  9. {  
  10.      int fp=0;  
  11.      struct fb_var_screeninfo  vinfo;  
  12.      struct fb_fix_screeninfo  finfo;  
  13.      long screensize=0;  
  14.      char *fbp = 0;  
  15.      int x = 0y = 0;  
  16.      long location = 0;  
  17.      fp = open ("/dev/fb0",O_RDWR);  
  18.   
  19.      if (fp < 0)  
  20.      {  
  21.           printf("Error : Can not open framebuffer device/n");  
  22.           exit(1);  
  23.      }  
  24.   
  25.      if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo))  
  26.      {  
  27.           printf("Error reading fixed information/n");  
  28.           exit(2);  
  29.      }  
  30.        
  31.      if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo))  
  32.      {  
  33.           printf("Error reading variable information/n");  
  34.           exit(3);  
  35.      }  
  36.   
  37.       screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;  
  38.      /*这就是把fp所指的文件中从开始到screensize大小的内容给映射出来,得到一个指向这块空间的指针*/  
  39.      fbp =(char *) mmap (0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fp,0);  
  40.      if ((int) fbp == -1)  
  41.      {  
  42.         printf ("Error: failed to map framebuffer device to memory./n");  
  43.         exit (4);  
  44.      }  
  45.      /*这是你想画的点的位置坐标,(0,0)点在屏幕左上角*/  
  46.      x = 100;  
  47.      y = 100;  
  48.      location = x * (vinfo.bits_per_pixel / 8) + y  *  finfo.line_length;  
  49.   
  50.      *(fbp + location) = 100;  /* 蓝色的色深 */  /*直接赋值来改变屏幕上某点的颜色*/  
  51.      *(fbp + location + 1) = 15; /* 绿色的色深*/    
  52.      *(fbp + location + 2) = 200; /* 红色的色深*/    
  53.      *(fbp + location + 3) = 0;  /* 是否透明*/   
  54.   
  55.      munmap (fbp, screensize); /*解除映射*/  
  56.      close (fp);    /*关闭文件*/  
  57.      return 0;  
  58.   
  59. }  


三,framebuffer 内部结构

    数据结构:framebuffer 设备很大程度上依靠了下面四个数据结构。这三个结构在fb.h 中声明。

                       Struct fb_var_screeninfo   //用来描述图形卡的特性的。通常是被用户设置的。

                       Struct fb_fix_screeninfo    // 定义了图形卡的硬件特性, 是不能改变的,用户选定了哪一个图形卡,那么它的硬件特性也就定下来了。

                       Struct fb_info                      //定义了当前图形卡framebuffer 设备的独立状态,一个图形卡可能有两个framebuffer, 在这种情况下,就需要两个fb_info 结构。这个结构是唯一在内核空间可见的。
   
  
    1)fb_var_screeninfo解析

 

[html] view plain copy
  1. struct fb_var_screeninfo {  
  2.     __u32 xres; /* visible resolution */  
  3.     __u32 yres;  
  4.   
  5.     __u32 xres_virtual; /* virtual resolution */  
  6.     __u32 yres_virtual;  
  7.   
  8.     __u32 xoffset; /* offset from virtual to visible */  
  9.     __u32 yoffset; /* resolution */  
  10.   
  11.     __u32 bits_per_pixel; /* guess what */  
  12.     __u32 grayscale; /* != 0 Graylevels instead of colors*/  
  13.   
  14.     struct fb_bitfield red; /*bitfield in fb mem if true color, */  
  15.     struct fb_bitfield green; /*else only length is significant */  
  16.     struct fb_bitfield blue;  
  17.     struct fb_bitfield transp; /*transparency */  
  18.   
  19.     __u32 nonstd;     /* != 0 Non standard pixel format */  
  20.     __u32 activate; /* see FB_ACTIVATE_* */  
  21.   
  22.     __u32 height; /* height of picture in mm???*/  
  23.     __u32 width; /* width of picture in mm????*/  
  24.   
  25.     __u32 accel_flags; /* acceleration flags (hints) */  
  26.   
  27.     /* Timing: All values in pixclocks, except pixclock (of course) */  
  28.   
  29.     __u32 pixclock; /* pixel clock in ps (pico seconds) */  
  30.   
  31.     __u32 left_margin; /* time from sync to picture */  
  32.     __u32 right_margin; /* time from picture to sync */  
  33.     __u32 upper_margin; /* time from sync to picture */  
  34.     __u32 lower_margin;  
  35.   
  36.     __u32 hsync_len; /* length of horizontal sync */  
  37.     __u32 vsync_len; /* length of vertical sync */  
  38.   
  39.     __u32 sync; /* see FB_SYNC_* */  
  40.     __u32 vmode; /* see FB_VMODE_* */  
  41.   
  42.     __u32 reserved[6]; /* Reserved for future compatibility*/  
  43.   
  44. };  


前几个成员决定了分辨率。

           xres和yres是在屏幕上可见的实际分辨率,

          xres-virtual决定了构建屏幕时视频卡读取屏幕内存的方式。

          bits_per_pixel 设为1,2,4,8,16,24或32来改变颜色深度


        2)  fb_fix_screeninfo

[html] view plain copy
  1. struct fb_fix_screeninfo {  
  2.   
  3.     char id[16]; /* identification string eg "TT Builtin" */  
  4.   
  5.     unsigned long smem_start; /* Start of frame buffer mem */  
  6.   
  7.     /* (physical address) */  
  8.   
  9.     __u32 smem_len; /* Length of frame buffer mem */  
  10.   
  11.     __u32 type; /* see FB_TYPE_* */  
  12.   
  13.     __u32 type_aux; /* Interleave for interleaved Planes */  
  14.   
  15.     __u32 visual; /* see FB_VISUAL_* */  
  16.   
  17.     __u16 xpanstep; /* zero if no hardware panning */  
  18.   
  19.     __u16 ypanstep; /* zero if no hardware panning */  
  20.   
  21.     __u16 ywrapstep; /* zero if no hardware ywrap */  
  22.   
  23.     __u32 line_length; /* length of a line in bytes */  
  24.   
  25.     unsigned long mmio_start; /* Start of Memory Mapped I/O */  
  26.   
  27.     /* (physical address) */  
  28.   
  29.     __u32 mmio_len; /* Length of Memory Mapped I/O */  
  30.   
  31.     __u32 accel; /* Type of acceleration available */  
  32.   
  33.     __u16 reserved[3]; /* Reserved for future compatibility */  
  34.   
  35. };  

                  3) 显示说明


【双显示器例子】

           一个例子,可能就是双显示,最近刚刚看到实际某开发者的系统,就是两个显示器,鼠标移动超过单个显示器,到最右边的时候,就跑到另一个显示器了。对于常常用多系统或者需要打开很多东西的开发人员,这个功能很实用。

          帧缓冲可以用于 页面交换page flipping(也常叫做 双缓冲double buffering),许多游戏都是采用此技术,以实现更流畅的视频输出,以便用户获得更好的游戏体验。此技术也被用于3D图形加速。

【双缓冲的主要实现原理】

          假如你的显示器是VGA模式,640×400,也就是虚拟的分辨率是640X800,也就是800线(每一行的数据,称为一条线,也就是640X1的数据了)。800线的数据存储于Framebuffer,而实际的显示内容,只是400线,

         Linux内核中的Framebuffer模型中,对应有个变量yoffset,就是表示的这个具体的纵坐标,默认是0,所以显示的内容就是,0-399线,由于和实际显示 页面大小等同,所以此处可以简称为第一帧。

         如果yoffset改变了,比如此例中变为400,那就是显示剩余的部分,400-799线。此处简称为第二帧。

         在系统显示第一帧的时候,系统在后台悄悄地准备第二帧的数据,所以,等第一帧显示完成,多数时候,第二帧的数据也准备好了,就可以直接显示,同时系统又在准备接下来的一帧的数据,这样就可以大大提高显示效率。


【平滑地滚动页面的实现原理】

           同上,在显示完第一帧数据的时候,也就是0-399线的时候,将yoffset设置为1,就可以显示1-400线的数据了,显示完成后,再设置yoffset为2,就显示2-401线的数据,这样,就可以一点点地,平滑地显示整个滚动画面了。其实也就是画面在垂直方向的滚动。其中yoffset的增加,可以使用定时器,各个一段时间,比如10us,增加1,系统自动会更新显示对应的内容,这样我们所看到的内容就是滚动的画面了。

          此外,linux中的Framebuffer模型中,提供了一些ioctl功能,给定一些参数,然后系统可以实现对应的功能,其中有个参数就是FBIOPAN_DISPLAY。具体也就是类似如下调用:

          ioctl (framebuffer_handler, FBIOPAN_DISPLAY, &variable_info);

          而这个调用,如果显示不支持framebuffer的双缓冲的话,那么其framebuffer的缓冲大小,就是和物理上的显示器大小等同,那么对应的yoffset也就不会像双缓冲那样变化了。

          也就是说,如果显卡/显示屏控制器不支持双缓冲,那么yoffset就应该一直为0,并且在运行时候,也不应该改变,也不应该去给FBIOPAN_DISPLAY的参数调用ioctl。