首页 > 代码库 > Framebuffer 机制【转】

Framebuffer 机制【转】

本文转载自:http://blog.csdn.net/paul_liao/article/details/7706477

Framebuffer

Framebuffer是Linux系统为显示设备提供的一个接口,它将显示缓存区抽象,屏蔽图像硬件的底层差异,允许上层应用程序在图形模式下直接对显示缓冲区进行读写操作。Framebuffer设备是标准的字符设备,主设备号为29,对应于/dev/fbn设备文件。

 

双缓冲机制

         Android系统中使用的此机制,带来的好处是让画面的切换更流畅。比如可视分辨率为240x320,虚拟分辨率为240x640,当第一帧0~319在显示时,第二帧320~639则在后台计算;当第一帧显示完时,直接切换到第二帧显示。接着系统又可以计算下一帧,这样大大的提高了效率。

 

驱动文件

Fbmem.c文件实现了帧缓冲驱动公共的调用(核心)

modedb.c所有的VESA标准显示模式信息

Fbmon.c解析显示器的EDID并计算时需参数

Fbcmap.c实现了和调色板相关的调用   

Xxxfb.c具体控制器驱动的实现     

skeletonfb.c   framebuffer的骨架程序

 

 

Framebuffer设备驱动框架

技术分享

 

帧缓冲设备提供给用户控件的file_operations结构由fbmem.c中的file_operations提供,而特定帧缓冲设备fb_info结构体的注册、注销以及其中的成员的维护,尤其是fb_ops中成员函数的则由对应的xxxfb.c文件实现,fb_ops中的成员函数最终会操作LCD控制器硬件寄存器。

 

相关数据结构

Fb_info

Fb_info结构是帧缓冲设备属性和操作的完整描述,包含了帧缓冲设备的全部信息,如设备参数设置、以及操作函数指针。每一个帧缓冲设备都必须对应一个fb_info。

struct fb_info {

         structfb_var_screeninfo var; /* 描述fb可变参数 */

         structfb_fix_screeninfo fix;     /* 描述fb固定参数 */

         structbacklight_device *bl_dev;   /* 背光设备 */

         structdevice *dev;           /* This is this fbdevice, guess what? */

         structfb_ops *fbops;   /* fb操作函数集 */

         ……

}

 

Fb_ops

         Fb_ops是操作底层fb的函数集合,对于不同的LCD控制器实现不同fb_ops,应用层通过file_operations函数集合调用到fb_ops函数集合。

struct fb_ops {

         /*打开和关闭fb设备 */

         int(*fb_open)(struct fb_info *info, int user);

         int(*fb_release)(struct fb_info *info, int user);

 

         /*用于非线性布局和在常规内存无法映射访问的fb */

         ssize_t(*fb_read)(struct fb_info *info, char __user *buf,

                               size_t count, loff_t *ppos);

         ssize_t(*fb_write)(struct fb_info *info, const char __user *buf,

                                size_t count, loff_t *ppos);

 

         /*检测可变参数,并调整到支持的值*/

         int(*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);

 

         /*根据info->var设置video模式 */

         int(*fb_set_par)(struct fb_info *info);

 

         /*set color register */

         int(*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,

                                unsigned blue, unsigned transp, structfb_info *info);

 

         /*set color registers in batch */

         int(*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);

 

         /*空白显示 */

         int(*fb_blank)(int blank, struct fb_info *info);

 

         /*pan display */

         int(*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);

 

         /*矩形填充 */

         void(*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);

         /*Copy data from area to another */

         void(*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);

         /*Draws a image to the display */

         void(*fb_imageblit) (struct fb_info *info, const struct fb_image *image);

 

         /*绘制光标 */

         int(*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);

 

         /*旋转显示 */

         void(*fb_rotate)(struct fb_info *info, int angle);

 

         /*wait for blit idle, optional */

         int(*fb_sync)(struct fb_info *info);

 

         /*perform fb specific ioctl (optional) */

         int(*fb_ioctl)(struct fb_info *info, unsigned int cmd,

                            unsignedlong arg);

 

         /*Handle 32bit compat ioctl (optional) */

         int(*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,

                            unsignedlong arg);

 

         /*fb特定的mmap */

         int(*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);

 

         /*get capability given var */

         void(*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,

                                struct fb_var_screeninfo *var);

 

         /*teardown any resources to do with this framebuffer */

         void(*fb_destroy)(struct fb_info *info);

};

 

fb_var_screeninfo

         记录用户可修改的显示控制器参数,包括屏幕分辨率、每个像素点包含的位数和timing等。

struct fb_var_screeninfo {

         __u32xres;                         /* 可视分别率                 */

         __u32yres;

         __u32xres_virtual;          /* 虚拟分别率                 */

         __u32yres_virtual;

         __u32xoffset;                    /* 虚拟到可视的偏移 */

         __u32yoffset;

 

         __u32bits_per_pixel;               /* guesswhat                    */

         __u32grayscale;               /* 0 = color, 1 =grayscale,       */

                                                        /* >1 = FOURCC                          */

         /*fb缓存的RGB位域*/

structfb_bitfield red;                /*bitfield in fb mem if true color, */

         structfb_bitfield green;  /* else only length issignificant */

         structfb_bitfield blue;

         structfb_bitfield transp; /* transparency                          */    

 

         __u32nonstd;                    /* != 0 Nonstandard pixel format */

 

         __u32activate;                           /* seeFB_ACTIVATE_*             */

 

         __u32height;                     /* height ofpicture in mm    */

         __u32width;                      /* width ofpicture in mm     */

 

         __u32accel_flags;            /* (OBSOLETE) seefb_info.flags */

 

         /*时序:出了pixclock其他单位都是ps*/

         __u32pixclock;                           /*pixel clock in ps (pico seconds) */

         __u32left_margin;           /* time from syncto picture    */

         __u32right_margin;                 /* time frompicture to sync    */

         __u32upper_margin;               /* time fromsync to picture    */

         __u32lower_margin;

         __u32hsync_len;              /* length ofhorizontal sync     */

         __u32vsync_len;              /* length ofvertical sync */

         __u32sync;                        /* seeFB_SYNC_*            */

         __u32vmode;                    /* seeFB_VMODE_*                 */

         __u32rotate;                     /* angle werotate counter clockwise */

         __u32colorspace;            /* colorspace forFOURCC-based modes */

         __u32reserved[4];           /* Reserved forfuture compatibility */

};

 

Fb_fix_screeninfo

         记录用户不能修改的显示控制器的参数,如帧缓存区的物理地址和长度等。

struct fb_fix_screeninfo {

         charid[16];                         /*identification string eg "TT Builtin" */

         unsignedlong smem_start;    /*帧缓存区的开始物理地址*/

         __u32smem_len;                      /* 长度 */

         __u32type;                        /* seeFB_TYPE_*             */

         __u32type_aux;                         /*Interleave for interleaved Planes */

         __u32visual;                      /* seeFB_VISUAL_*                  */

         __u16xpanstep;                         /* zeroif no hardware panning  */

         __u16ypanstep;                         /* zeroif no hardware panning  */

         __u16ywrapstep;             /* zero if nohardware ywrap    */

         __u32line_length;            /* length of aline in bytes    */

         unsignedlong mmio_start;     /*内存映射IO的开始地址 */

                                               /*(physical address) */

         __u32mmio_len;                       /* Lengthof Memory Mapped I/O  */

         __u32accel;                       /* Indicateto driver which       */

                                               /*  specific chip/card we have       */

         __u16capabilities;           /* see FB_CAP_*                        */

         __u16reserved[2];           /* Reserved forfuture compatibility */

};

 

Fb_bitfield

         Fb_bitfield结构体描述每个像素点的组织方式。

struct fb_bitfield {

         __u32offset;                      /* 位域偏移   */

         __u32length;                     /*位域长度     */

         __u32msb_right;             /* != 0 : Mostsignificant bit is right */

};

 

注册/注销函数

Int register_framebuffer(struct fb_info*fb_info);

Int unregister_framebuffer(struct fb_info*fb_info);

fb注册函数主要完成创建fb设备文件、把新的fb设备加入到registered_fb数组和convertfb_var_screeninfo to fb_videomode。Fb注销函数作用则相反。

 

如何编写一个帧缓冲设备驱动?

内核中提供了一个帧缓冲设备驱动的骨架程序(skeletonfb.c),很有参考价值。现在大多数的LCD控制器都是SOC的,所以驱动里用platform设备来进行注册。

帧缓存设备初始化,需要完成以下几个工作:

1、映射LCD寄存器和申请LCD中断;

2、申请fb_info并初始化fb_fix_screeninfo和fb_var_screeninfo结构体;

3、根据具体LCD屏幕的特点,完成LCD控制器的初始化;

4、申请帧缓冲设备的显示缓存区空间;

5、调用register_framebuffer函数注册帧缓冲设备。

 

实现fb_ops结构体,其中fb_fillrect、fb_copyarea和fb_imageblit是必须的。

static struct fb_ops xxxfb_ops = {
.owner  = THIS_MODULE,
.fb_open  = xxxfb_open,      /* 打开fb设备 */
.fb_read  = xxxfb_read,
.fb_write  = xxxfb_write,
.fb_release  = xxxfb_release,  /* 关闭fb设备 */
.fb_check_var  = xxxfb_check_var,         /* 检测可变参数,并调整到支持的值 */
.fb_set_par  = xxxfb_set_par,                     /* 初始化LCD控制器 */
.fb_setcolreg  = xxxfb_setcolreg,                  /* 设置color寄存器 */
.fb_blank  = xxxfb_blank,                         /* 显示空白 */
.fb_pan_display= xxxfb_pan_display,
.fb_fillrect  = xxxfb_fillrect, /* Needed !!! 矩形填充  */
.fb_copyarea  = xxxfb_copyarea, /* Needed !!!  数据拷贝 */
.fb_imageblit  = xxxfb_imageblit, /* Needed !!!  图像填充 */
.fb_cursor  = xxxfb_cursor, /* 显示光标 */
.fb_rotate  = xxxfb_rotate,               /* 旋转显示 */
.fb_sync  = xxxfb_sync,
.fb_ioctl  = xxxfb_ioctl,                          /* fb特定的IOCTL */
.fb_mmap  = xxxfb_mmap,             /* fb特定的MMAP */
};

Framebuffer 机制【转】