《Linux驱动开发详解》——LCD设备驱动重要数据结构及驱动框架

核心文件:/drivers/video/fbmem.c

18.2.3.Linux帧缓冲相关数据结构与函数

         1. fb_info结构体(最关键)

  1. /* struct fb_info 结构体 */
  2. struct fb_info {
  3.      int node;
  4.      int flags;
  5.      struct mutex lock;         /* 用于 open/release/ioctl的锁 */
  6.      struct mutex mm_lock;         /* Lock for fb_mmap and smem_* fields */
  7.      struct fb_var_screeninfo var;     /* Current var */
  8.      struct fb_fix_screeninfo fix;     /* Current fix */
  9.      struct fb_monspecs monspecs;     /* 显示器标准 */
  10.      struct work_struct queue;     /* Framebuffer event queue 帧缓冲事件队列 */
  11.      struct fb_pixmap pixmap;     /* Image hardware mapper 图像硬件mapper */
  12.      struct fb_pixmap sprite;     /* Cursor hardware mapper 光标硬件mapper */
  13.      struct fb_cmap cmap;         /* Current cmap 目前颜色表 */
  14.      struct list_head modelist;      /* mode list */
  15.      struct fb_videomode *mode;     /* current mode 目前video模式 */
  16. #ifdef CONFIG_FB_BACKLIGHT
  17.      /* assigned backlight device 对应的背光设备 */
  18.      /* set before framebuffer registration,
  19.        remove after unregister */
  20.      struct backlight_device *bl_dev;
  21.      /* Backlight level curve 背光调整 */
  22.      struct mutex bl_curve_mutex;    
  23.     u8 bl_curve[FB_BACKLIGHT_LEVELS];
  24. #endif
  25. #ifdef CONFIG_FB_DEFERRED_IO
  26.      struct delayed_work deferred_work;
  27.      struct fb_deferred_io *fbdefio;
  28. #endif
  29.      struct fb_ops *fbops; /* 帧缓冲操作 */
  30.      struct device *device;         /* This is the parent 父设备 */
  31.      struct device *dev;         /* This is this fb device fb设备 */
  32.      int class_flag;                    /* private sysfs flags */
  33. #ifdef CONFIG_FB_TILEBLITTING
  34.      struct fb_tile_ops *tileops;    /* Tile Blitting */
  35. #endif
  36.      char __iomem *screen_base;     /* Virtual address */
  37.      unsigned long screen_size;     /* Amount of ioremapped VRAM or 0 */
  38.      void *pseudo_palette;         /* Fake palette of 16 colors */
  39. #define FBINFO_STATE_RUNNING    0
  40. #define FBINFO_STATE_SUSPENDED    1
  41.     u32 state;             /* Hardware state i.e suspend */
  42.      void *fbcon_par;                /* fbcon use-only private area */
  43.      /* From here on everything is device dependent */
  44.      void *par;
  45.      /* we need the PCI or similiar aperture base/size not
  46.        smem_start/size as smem_start may just be an object
  47.        allocated inside the aperture so may not actually overlap */
  48.      resource_size_t aperture_base;
  49.      resource_size_t aperture_size;
  50. };

        2.fb_ops结构体

                 fb_info成员变量fb_ops为指向底层操作的函数指针,这些函数需要驱动程序猿自己编写。

  1. /* fb_ops 结构体 */
  2. struct fb_ops {
  3.      /* open/release and usage marking */
  4.      struct module *owner;
  5.      int (*fb_open)(struct fb_info *info, int user);
  6.      int (*fb_release)(struct fb_info *info, int user);
  7.      /* 对于非线性布局的/常规内存映射无法工作的帧缓冲设备*/
  8.      ssize_t (*fb_read)(struct fb_info *info, char __user *buf, size_t count, loff_t *ppos);
  9.      ssize_t (*fb_write)(struct fb_info *info, const char __user *buf, size_t count, loff_t *ppos);
  10.      /* 调整可变参数,并调整到支持的值 */
  11.      int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);
  12.      /* 根据info->var设置video模式 */
  13.      int (*fb_set_par)(struct fb_info *info);
  14.      /* set color register */
  15.      int (*fb_setcolreg)( unsigned regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info *info);
  16.      /* 批量设置color寄存器,设置颜色表 */
  17.      int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);
  18.      /* 显示空白 */
  19.      int (*fb_blank)( int blank, struct fb_info *info);
  20.      /* pan display */
  21.      int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);
  22.      /* 矩形填充 */
  23.      void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);
  24.      /* 数据复制 */
  25.      void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);
  26.      /* 图形填充 */
  27.      void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);
  28.      /* 绘制光标 */
  29.      int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);
  30.      /* 旋转显示 */
  31.      void (*fb_rotate)(struct fb_info *info, int angle);
  32.      /* 等待blit空闲(可选) */
  33.      int (*fb_sync)(struct fb_info *info);
  34.      /* fb特定的ioctl(可选) */
  35.      int (*fb_ioctl)(struct fb_info *info, unsigned int cmd, unsigned long arg);
  36.      /* 处理32位的compat ioctl(可选) */
  37.      int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd, unsigned long arg);
  38.      /* fb特定的mmap */
  39.      int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);
  40.      /* get capability given var */
  41.      void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,struct fb_var_screeninfo *var);
  42.      /* teardown any resources to do with this framebuffer */
  43.      void (*fb_destroy)(struct fb_info *info);
  44. };
      fb_ops 的 fb_check_var() 成员函数用于检查可以修改的屏幕参数并调整到合适的值,而 fb_set_par() 则使得用户设置的屏幕参数在硬件上有效。

       3. fb_var_screeninfo 和 fb_fix_screeninfo 结构体

               fb_var_screeninfo 记录用户可以修改的显示控制器参数,包括屏幕分辨率和每个像素点的比特数。  

               fb_fix_screeninfo 记录用户不能修改的显示控制器参数,包括屏幕缓冲区的物理地址、长度。当对帧缓冲舍内进行映射操作的时候,就是从fb_fix_screeninfo中取得缓冲区的物理地址。上述结构体都需要在驱动程序中初始化和设置。

  1. /* fb_var_screeninfo 结构体 */
  2. struct fb_var_screeninfo {
  3.     __u32 xres;             /* visible resolution  可见度解析      */
  4.     __u32 yres;
  5.     __u32 xres_virtual;         /* virtual resolution  虚拟度解析      */
  6.     __u32 yres_virtual;
  7.     __u32 xoffset;             /* offset from virtual to visible 虚拟到可见之间的偏移 */
  8.     __u32 yoffset;             /* resolution            */
  9.     __u32 bits_per_pixel;         /* guess what            */
  10.     __u32 grayscale;         /* 非0时指灰度 */
  11. /* fb缓存的R/G/B位域 */
  12.      struct fb_bitfield red;         /* bitfield in fb mem if true color, */
  13.      struct fb_bitfield green;    
  14.      struct fb_bitfield blue;
  15.      struct fb_bitfield transp;     /* transparency 透明度           */    
  16.     __u32 nonstd;             /* != 0 非标准像素格式 */
  17.     __u32 activate;             /* see FB_ACTIVATE_*        */
  18.     __u32 height;             /* height of picture in mm    */
  19.     __u32 width;             /* width of picture in mm     */
  20.     __u32 accel_flags;         /* (OBSOLETE) see fb_info.flags */
  21.      /* 定时:除了pixclock本身外,其他的都以像素时钟为单位 */
  22.     __u32 pixclock;             /* 像素时钟(皮秒) */
  23.     __u32 left_margin;         /* 行切换:从同步到绘图之间的延迟    */
  24.     __u32 right_margin;         /* 行切换:从绘图到同步之间的延迟    */
  25.     __u32 upper_margin;         /* 帧切换:从同步到绘图之间的延迟    */
  26.     __u32 lower_margin; /* 帧切换:从绘图到同步之间的延迟    */  
  27.  __u32 hsync_len;         /*   水平同步的长度  */
  28.     __u32 vsync_len;         /* 垂直同步的长度  */
  29.     __u32 sync;             /* see FB_SYNC_*        */
  30.     __u32 vmode;             /* see FB_VMODE_*        */
  31.     __u32 rotate;             /* 顺时针旋转的角度 */
  32.     __u32 reserved[ 5];         /* Reserved for future compatibility */
  33. };

  1. /* fb_fix_screeninfo 结构体*/
  2. struct fb_fix_screeninfo {
  3.      char id[ 16];             /* identification string eg "TT Builtin" */
  4.      unsigned long smem_start;     /* fb缓存的开始位置(物理地址) */
  5.     __u32 smem_len;             /* fb缓存的长度 */
  6.     __u32 type;             /* see FB_TYPE_*        */
  7.     __u32 type_aux;             /* Interleave for interleaved Planes */
  8. /* visual:记录屏幕使用的色彩模式 ①Monochrome(FB_VISUAL_MON01、FB_VISUAL_MON10) 每个像素都是黑或白
  9. ②Pseudo color(FB_VISUAL_PSEDOCOLOR、FB_VISUAL_STATIC_PSEUDOCOLOR), 即伪彩色,采用索引颜色显示
  10. ③True color(FB_VISUAL_TRUECOLOR),即彩色,分成红绿蓝三基色
  11. ④Direct color(FB_VISUAL_DIRECTCOLOR)每个像素偃师市也是由红绿蓝组成,不过每个颜色值是个索引,需要查表
  12. ⑤Grayscale displays,灰度显示,红、绿、蓝的值都一样
  13.  */
  14.     __u32 visual;             /* see FB_VISUAL_* 记录屏幕使用的色彩模式 */
  15.     __u16 xpanstep;             /* zero if no hardware panning  */
  16.     __u16 ypanstep;             /* zero if no hardware panning  */
  17.     __u16 ywrapstep;         /* zero if no hardware ywrap    */
  18.     __u32 line_length;         /* 1行的字节数    */
  19.      unsigned long mmio_start;     /* 内存映射IO长度 */
  20.     __u32 mmio_len;             /* 内存映射IO长度 */
  21.     __u32 accel;             /* Indicate to driver which    */
  22.     __u16 reserved[ 3];         /* Reserved for future compatibility */
  23. };
      
          4.fb_bitfield 结构体

                 fb_bitfield描述每一像素显示缓冲区的组织方式,包含位域偏移、位域长度和MSB(最高有效位)指示。

  1. /* fb_bitfield 结构体 */
  2. struct fb_bitfield {
  3.     __u32 offset;             /* beginning of bitfield    */
  4.     __u32 length;             /* length of bitfield        */
  5.     __u32 msb_right;         /* != 0 : Most significant bit MSB在右边 */
  6. };

        5.fb_cmap结构体

                fb_cmap结构体记录设备无关的颜色表信息,用户空间可以通过 ioctl() 的 FBIOGETCMAP 和 FBIOPUTCMAP 命令读取或设定 颜色表。

  1. /* fb_cmap 结构体 */
  2. struct fb_cmap {
  3.     __u32 start;             /* First entry  第一个元素入口  */
  4.     __u32 len;             /* Number of entries 原色数量 */
  5.     __u16 *red;             /* Red values  RGB透明度  */
  6.     __u16 *green;
  7.     __u16 *blue;
  8.     __u16 *transp;             /* transparency, can be NULL */
  9. };

   
  1. /* 用户空间获取颜色表的例程
  2. * 若BPP为8位,颜色表长度为256
  3. * 若BPP为4位,颜色表长度为16,否则颜色表长度为0
  4. * 因为对于BPP大于等于16的情况,使用颜色表超不划算
  5. */
  6. /* 读取颜色表 */
  7. if( (vinfo.bits_per_pixel == 8) || (vinfo.bits_per_pixel == 4) ){
  8.     screencols = (vinfo.bits_per_pixel == 8) ? 256 : 16; /* 颜色表的大小 */
  9.      int loopc;
  10.     startcmap = new fb_cmap;
  11.     startcmap->start = 0;
  12.     startcmap->len = screencols;
  13.      /* 分配颜色表的内存 */
  14.     startcmap->red = ( unsigned short int*) malloc( sizeof( unsigned short int) * screencols);
  15.     startcmap->green = ( unsigned short int*) malloc( sizeof( unsigned short int) * screencols);
  16.     startcmap->blue = ( unsigned short int*) malloc( sizeof( unsigned short int) * screencols);
  17.     startcmap->transp = ( unsigned short int*) malloc( sizeof( unsigned short int) * screencols);;
  18.      /* 获取颜色表 */
  19.     ioctl(df, FBIOGETCMAP, startcmap);
  20.      for( loopc = 0; loopc < screencols; loopc++ ){
  21.         screenclut[loopc] = qRgb(startcmap->red[loopc] >> 8, startcmap->green[loopc] >> 8, startcmap->blue[loopc] >> 8)
  22.     }
  23. }
          对于一个256色(BPP=8)的800*600的图像,若使用红绿蓝分别用一个字节描述,则需要800*600*3=1 440 000Byte,若使用颜色表,需要800*600*1+256*3=480 768Byte的空间。

        7.文件操作结构体

  1. /* fb_ops */
  2. static const struct file_operations fb_fops = {
  3.     .owner =    THIS_MODULE,
  4.     .read =        fb_read,
  5.     .write =    fb_write,
  6.     .unlocked_ioctl = fb_ioctl,
  7. #ifdef CONFIG_COMPAT
  8.     .compat_ioctl = fb_compat_ioctl,
  9. #endif
  10.     .mmap =        fb_mmap,
  11.     .open =        fb_open,
  12.     .release =    fb_release,
  13. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
  14.     .get_unmapped_area = get_fb_unmapped_area,
  15. #endif
  16. #ifdef CONFIG_FB_DEFERRED_IO
  17.     .fsync =    fb_deferred_io_fsync,
  18. #endif
  19. };

        7.注册于注销帧缓冲设备

                     int  register_framebuffer(struct fb_info *fb_info); /* 注册成功返回0, 超过设备最大数目FB_MAX(32)返回-ENXIO */

                     int  unregister_framebuffer(struct fb_info *fb_info);


18.3帧缓冲设备驱动结构

            

18.4帧缓冲设备驱动的模块加载与卸载函数

           帧缓冲设备驱动的模块加载函数:

                   (1)申请FBI结构体的内存空间,初始化FBI结构体中固定和可变的屏幕参数。

                   (2)根据具体的LCD屏幕特点,完成LCD控制器硬件的初始化。

                   (3)申请帧缓冲设备的显示缓冲区空间。

                   (4)注册帧缓冲设备。

          在帧缓冲设备驱动的模块加载函数中完成的工作只是注册平台驱动,初始化FBI结构体中的固定和可变参数、LCD控制器硬件的初始化、申请帧缓冲设备的显示缓冲区空间和注册帧缓冲设备的工作在平台驱动的探测函数中完成。

  1. /* 帧缓冲设备驱动的模块加载/卸载及平台驱动的探测/移除函数模板*/
  2. /* 平台驱动结构体 */
  3. static struct platform_driver xxxfb_driver = {
  4.     .probe     = xxxfb_probe,
  5.     .remove    = xxxfb_remove,
  6.     .suspend   = xxxfb_suspend,
  7.     .resume    = xxxfb_resume,
  8.     .driver    = {
  9.             .name  = "xxx-lcd", /* 驱动名 */
  10.             .owner = THIS_MODULE,
  11.         },
  12. };
  13. /* 平台驱动探测函数 */
  14. static int __ init xxxfb_probe(...)
  15. {
  16.      struct fb_info* info;
  17.      /* 分配fb_info结构体 */
  18.     info = framebuffer_alloc(...);
  19.     info->screen_base = framebuffer_virtual_memory;
  20.     info->var = xxxfb_var;
  21.     info->fix = xxxfb_fix;
  22.      /* 分配显示缓冲区 */
  23.     alloc_dis_buffer(...);
  24.      /* 初始化LCD控制器 */
  25.     lcd_init(...);
  26.     
  27.      /* 检查可变参数 */
  28.     xxxfb_check_var(&info->var, info);
  29.     
  30.      /* 注册fb_info */
  31.      if( register_framebuffer(info)< 0 )
  32.          return -EINVAL;
  33.      return 0;
  34. }
  35. /* 平台驱动移除函数 */
  36. static void __ exit xxxfb_remove(...)
  37. {
  38.      struct fb_info* info = dev_get_drv_data(dev);
  39.      if(info){
  40.         unregister_framebuffer(info);
  41.         dealloc_dis_buffer(...); /* 释放缓冲区 */
  42.         framebuffer_release(info); /* 注销 fb_info */
  43.     }
  44. }
  45. int __ init xxxfb_init(void)
  46. {
  47.      return platform_driver_register(&xxxfb_driver);  /* 注册平台驱动 */
  48. }
  49. static void __ exit xxxfb_cleanup()
  50. {
  51.     platform_driver_unregister(&xxxfb_driver);
  52. }
  53. module_init(xxxfb_init);
  54. module_exit(xxxfb_cleanup);

18.5 帧缓冲设备显示缓冲区的申请与释放

         在分配缓冲区时一定要考虑cache的一致性问题,因为系统往往会通过DMA方式搬移显示数据。合适的方式是使用 dma_alloc_writecombine() 函数分配一段 writecombining 区域,对应的 writecombining 区域由 dma_free_writecombine() 函数释放。

         writecombing 意味着“写合并”,它允许写入的数据被合并,并临时保存在写合并缓冲区(WCB)中,直到进行一次 burst 传输而不再需要多次 single 传输。通过 dma_alloc_writecombine() 分配的显示缓冲区不会出现 cache 一致性问题,这一点类似 dma_alloc_coherent()。

  1. /* 帧缓冲设备显示缓冲区的分配与释放 */
  2. static int __ init xxxfb_map_video_memory(struct xxxfb_info *fbi)
  3. {
  4.     fbi->map_size = PAGE_ALIGN(fbi->fb->fix.smem_len + PAGE_SIZE);
  5.     fbi->map_cpu  = dma_alloc_writecombine(fbi->dev, fbi->map_size, &fbi->map_dma, GFP_KERNEL); /* 分配内存 */
  6.     fbi->map_size = fbi->fb->fix.smem_len;  /* 显示缓冲区大小 */
  7.      if( fbi->map_cpu ){
  8.          memset(fbi->map_cpu, 0xf0, fbi->map_size);
  9.         fbi->screen_dma = fbi->map_dma;
  10.         fbi->fb->screen_base = fbi->map_cpu;
  11.         fbi->fb->fix.smem_start = fbi->screen_dma;
  12.     }
  13.      return fbi->map_cpu ? 0 : -ENOMEM;
  14. }
  15. static inline void xxxfb_unmap_video_memory(struct s3c2410fb_info* fbi)
  16. {
  17.      /* 释放显示缓冲区 */
  18.     dma_free_writecombine(fbi->dev, fbi->map_size, fbi->map_cpu, fbi->map_dma);
  19. }

18.6 帧缓冲设备的参数设置

18.6.1 定时参数 

            FBI结构体可变参数var中的left_margin、right_margin、upper_margin、lower_margin、hsync_len和vsync_len直接查LCD数据手册就可以得到。

18.6.2 像素时钟 

          FBI可变参数var中的pixclock意味着像素时钟,例如,如果为28.37516MHz,那么画一个像素需要35242ps(皮秒):

                    1/(28.37516MHz) = 35.242E-9s

                    如果屏幕的分辨率是640*480,显示一行的时间是:640*35.242E-9s = 22.555E-6 s

          每条扫描线是640,但是水平回扫和水平同步也需要时间,假设水平回扫和同步需要272个像素时钟,因此,画一条扫描线完整的时间是:(640+272)* 35.242E-9 s - 9 s = 32.141E-6 s

          可以计算出水平扫描率大约是32kHz:1/(32.141E-6 s) = 31.113E3 Hz

         完整的屏幕有480条线,但是垂直回扫和垂直同步也需要时间,假设垂直回扫和垂直同步需要49个像素时钟,因此,画一个完整的屏幕的时间是:(480+49)*32.141E-6 s = 17.002E-3 s

        可以计算出垂直扫描率大约是59kHz:1/(17.002E-3 s) = 58.815 Hz

         这意味着屏幕数据每秒钟大约刷新59次。


18.6.3 颜色位域

            FBI可变参数var中的red、green、blue、位域的设置直接由显示缓冲区与显示点的对应关系决定,例如,对于RGB565模式,red占居5位,偏移11位,green占居6位,偏移5位,blue占居5位,偏移0位,即:

  1. fbinfo->var.red.offset = 11;
  2. fbinfo->var.green.offset = 5;
  3. fbinfo->var.blue.offset = 0;
  4. fbinfo->var.transp.offset = 0;
  5. fbinfo->var.red.length = 5;
  6. fbinfo->var.green.length = 6;
  7. gbinfo->var.blue.length = 5;

18.6.4 固定参数 

          FBI固定参数 fix 中 smem_start 指示帧缓冲设备显示缓冲区的首地址,smem_len 为帧缓冲设备显示缓冲区的大小,计算公式为:

                     smem_len = max_xres * max_yres * max_bpp;

           即:帧缓冲设备显示缓冲区的大小 = 最大的 x 解析度 * 最大的 y 解析度 * 最大的 BPP;

        

18.7 帧缓冲设备驱动的 fb_ops 成员函数 

           fb_check_var() 用于调整可变参数,并修正为硬件所支持的值;fb_set_par() 则根据屏幕参数设置具体读写LCD控制器的寄存器以使得LCD控制器进入相应的工作状态。

          对于 fb_ops 中的 fb_fillrect() 、 fb_copyarea() 和 fb_imageblit() 成员函数,通常直接使用对应的通用的 cfb_fillrect() 、cfb_copyarea() 和 cfb_imageblit() 函数即可。cfb_fillrect() 函数定义在 drivers/video/cfbfillrect.c 文件中,cfb_copyarea() 定义在 /drivers/video/cfbcopyarea.c 文件中, cfb_imageblit() 定义在 /drivers/cfbimagblt.c 文件中。


  1. /* fb_setcolreg() 函数模板,实现伪颜色表(针对 FB_VISUAL_TRUECOLOCK、FB_VISUAL_DIRECTCOLOR模式)和颜色表的填充*/
  2. static int xxxfb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info * info)
  3. {
  4.      struct xxxfb_info* fbi = info->par;
  5.      unsigned int val;
  6.      switch( fbi->fb->fix.visual ){
  7.          case FB_VISUAL_TRUECOLOR:
  8.              /* 真彩色,设置伪颜色表 */
  9.              if( regno < 16 ){     /* 伪颜色表只有16个成员,实际上,他们对应16种控制台颜色,logo显示也会使用伪颜色表 */
  10.                 u32 * pal = fbi->fb->pseudo_palette;
  11.                 val = chan_to_field(red, &fbi->fb->var.red);
  12.                 val |= chan_to_field(green, &fbi->fb->var.green);
  13.                 val |= charn_to_field(blue, &fbi->fb->var.blue);
  14.                 pal[regno] = val;
  15.             }
  16.               break;
  17.          case FB_VISUAL_PSEUDOCOLOR:
  18.              if( regno < 256 ){
  19.                  /* RGB565模式 */
  20.                 val = ( (red >> 0) & 0xf800 );
  21.                 val |= ( (green >> 5) & 0x07e0 );
  22.                 val |= ( (blue >> 11) & 0x001f );
  23.                 writel(val, XXX_TFTPAL(regno));
  24.                 schedule_palette_update(fbi, regno, val);
  25.             }
  26.              break;
  27.         ...
  28.     }
  29.      return 0;
  30. }

18.8 LCD设备驱动的读写、mmap 和 ioctl 函数

         (仔细分析)

18.9 帧缓冲设备的用户空间访问

        通过/dev/fbn,应用程序可进行的针对帧缓冲设备的操作主要有:

              (1)读/写 /dev/fbn : 相当于读写帧缓冲区。例如 cp /dev/fb0 tmp 可以将当前屏幕的内容复制到一个文件中。cp tmp > /dev/fb0则讲图像文件tmp显示在屏幕上。

              (2)映射操作:对于帧缓冲设备,可通过 mmap() 映射操作将屏幕缓冲区的物理地址映射到用户控件的一段虚拟地址中,之后用户控件就可以通过读写这段虚拟地址访问屏幕缓冲区,在屏幕上绘图。而且若干个进程可以映射到同一个显示缓冲区。实际上,使用帧缓冲设备的应用程序都是通过映射操作来显示图形的。

              (3)I/O控制:对于帧缓冲设备,对设备文件的 ioctl() 操作可读取/设置显示设备及屏幕的参数,如分辨率、显示颜色数、屏幕大小等。  

                 应用程序操作/dev/fbn的一般步骤:

             (1)打开 /dev/fbn 设备文件。

             (2)用 ioctl() 操作取得当前显示屏幕的参数,如分辨率、每个像素点的比特位数和偏移。根据屏幕参数课计算屏幕缓冲区的大小。

            (3)将屏幕缓冲区映射到用户空间。

            (4)映射后就可以直接读/写屏幕缓冲区进行绘图和图片显示了。

  1. /* 用户空间访问帧缓冲设备显示缓冲区范例 */
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <linux/fb.h>
  7. #include <sys/mman.h>
  8. int main()
  9. {
  10. int fbfd = 0;
  11. struct fb_var_screeninfo vinfo;
  12. unsigned long screensize = 0;
  13. char* fbp = 0;
  14. int x = 0, y = 0;
  15. int i = 0;
  16. fbfd = open( "/dev/fb0", O_RDWR);
  17. if( !fbfd ){
  18. printf( "Error:cannot open framebuffer device.\n");
  19. exit( 1);
  20. }
  21. printf( "The framebuffer device was opened successfully.\n");
  22. //Get Variable screen information
  23. if(ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)){
  24. printf( "Error reading variable information.\n");
  25. exit( 1);
  26. }
  27. printf( "%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
  28. if(vinfo.bits_per_pixel != 16){
  29. printf( "Error:not supported bits_per_pixel,it only supports 16 bit color\n");
  30. exit( 1);
  31. }
  32. //Figure out the size of the screen in bytes
  33. screensize = vinfo.xres * vinfo.yres * 2;
  34. //Map the device to memory
  35. fbp = ( char*)mmap( 0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  36. if( ( int)fbp == -1 ){
  37. printf( "Error:failed to map framebuffer device to memory.\n");
  38. exit( 4);
  39. }
  40. printf( "The framebuffer device was mapped to memory successfully.\n");
  41. //Draw 3 rect with graduated RED/GREEN/BLUE
  42. for(i = 0; i < 3; i++ ){
  43. for(y = i * (vinfo.yres / 3); y < (i + 1) * (vinfo.yres / 3); y++ ){
  44. for(x = 0; x < vinfo.xres; x++ ){
  45. long location = x * 2 + y * vinfo.xres * 2;
  46. int r = 0, g = 0, b = 0;
  47. unsigned short rgb;
  48. if(i == 0)
  49. r = ((x * 1.0) / vinfo.xres) * 32;
  50. if(i == 1)
  51. g = ((x * 1.0) / vinfo.xres) * 64;
  52. if(i == 2)
  53. b = ((x * 1.0) / vinfo.xres) * 32;
  54. rgb = (r << 11) | (g << 5) | b;
  55. *(( unsigned short*)(fbp + location)) = rgb;
  56. }
  57. }
  58. }
  59. munmap(fbp, screensize);
  60. close(fbfd);
  61. return 0;
  62. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值