Framebuffer 机制
Framebuffer
Framebuffer是linux系統(tǒng)為顯示設(shè)備提供的一個(gè)接口,它將顯示緩存區(qū)抽象,屏蔽圖像硬件的底層差異,允許上層應(yīng)用程序在圖形模式下直接對(duì)顯示緩沖區(qū)進(jìn)行讀寫操作。Framebuffer設(shè)備是標(biāo)準(zhǔn)的字符設(shè)備,主設(shè)備號(hào)為29,對(duì)應(yīng)于/dev/fbn設(shè)備文件。
?
雙緩沖機(jī)制
???????? Android系統(tǒng)中使用的此機(jī)制,帶來(lái)的好處是讓畫面的切換更流暢。比如可視分辨率為240x320,虛擬分辨率為240x640,當(dāng)?shù)谝粠?~319在顯示時(shí),第二幀320~639則在后臺(tái)計(jì)算;當(dāng)?shù)谝粠@示完時(shí),直接切換到第二幀顯示。接著系統(tǒng)又可以計(jì)算下一幀,這樣大大的提高了效率。
?
驅(qū)動(dòng)文件
Fbmem.c文件實(shí)現(xiàn)了幀緩沖驅(qū)動(dòng)公共的調(diào)用(核心)
modedb.c所有的VESA標(biāo)準(zhǔn)顯示模式信息
Fbmon.c解析顯示器的EDID并計(jì)算時(shí)需參數(shù)
Fbcmap.c實(shí)現(xiàn)了和調(diào)色板相關(guān)的調(diào)用???
Xxxfb.c具體控制器驅(qū)動(dòng)的實(shí)現(xiàn)?????
skeletonfb.c?? framebuffer的骨架程序
?
?
Framebuffer設(shè)備驅(qū)動(dòng)框架
幀緩沖設(shè)備提供給用戶控件的file_operations結(jié)構(gòu)由fbmem.c中的file_operations提供,而特定幀緩沖設(shè)備fb_info結(jié)構(gòu)體的注冊(cè)、注銷以及其中的成員的維護(hù),尤其是fb_ops中成員函數(shù)的則由對(duì)應(yīng)的xxxfb.c文件實(shí)現(xiàn),fb_ops中的成員函數(shù)最終會(huì)操作LCD控制器硬件寄存器。
?
相關(guān)數(shù)據(jù)結(jié)構(gòu)
Fb_info
Fb_info結(jié)構(gòu)是幀緩沖設(shè)備屬性和操作的完整描述,包含了幀緩沖設(shè)備的全部信息,如設(shè)備參數(shù)設(shè)置、以及操作函數(shù)指針。每一個(gè)幀緩沖設(shè)備都必須對(duì)應(yīng)一個(gè)fb_info。
struct fb_info {
???????? structfb_var_screeninfo var; /* 描述fb可變參數(shù) */
???????? structfb_fix_screeninfo fix;???? /* 描述fb固定參數(shù) */
???????? structbacklight_device *bl_dev; ? /* 背光設(shè)備 */
???????? structdevice *dev;?????????? /* This is this fbdevice, guess what? */
???????? structfb_ops *fbops;?? /* fb操作函數(shù)集 */
???????? ……
}
?
Fb_ops
???????? Fb_ops是操作底層fb的函數(shù)集合,對(duì)于不同的LCD控制器實(shí)現(xiàn)不同fb_ops,應(yīng)用層通過(guò)file_operations函數(shù)集合調(diào)用到fb_ops函數(shù)集合。
struct fb_ops {
???????? /*打開(kāi)和關(guān)閉fb設(shè)備 */
???????? int(*fb_open)(struct fb_info *info, int user);
???????? int(*fb_release)(struct fb_info *info, int user);
?
???????? /*用于非線性布局和在常規(guī)內(nèi)存無(wú)法映射訪問(wèn)的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);
?
???????? /*檢測(cè)可變參數(shù),并調(diào)整到支持的值*/
???????? int(*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);
?
???????? /*根據(jù)info->var設(shè)置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);
?
???????? /*繪制光標(biāo) */
???????? int(*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);
?
???????? /*旋轉(zhuǎn)顯示 */
???????? 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
???????? 記錄用戶可修改的顯示控制器參數(shù),包括屏幕分辨率、每個(gè)像素點(diǎn)包含的位數(shù)和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 */
?
???????? /*時(shí)序:出了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
???????? 記錄用戶不能修改的顯示控制器的參數(shù),如幀緩存區(qū)的物理地址和長(zhǎng)度等。
struct fb_fix_screeninfo {
???????? charid[16];???????????????????????? /*identification string eg "TT Builtin" */
???????? unsignedlong smem_start;??? /*幀緩存區(qū)的開(kāi)始物理地址*/
???????? __u32smem_len;????????????????????? /* 長(zhǎng)度 */
???????? __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;???? /*內(nèi)存映射IO的開(kāi)始地址 */
?????????????????????????????????????????????? /*(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結(jié)構(gòu)體描述每個(gè)像素點(diǎn)的組織方式。
struct fb_bitfield {
???????? __u32offset;????????????????????? /* 位域偏移?? */
???????? __u32length;???????????????????? /*位域長(zhǎng)度???? */
???????? __u32msb_right;???????????? /* != 0 : Mostsignificant bit is right */
};
?
注冊(cè)/注銷函數(shù)
Int register_framebuffer(struct fb_info*fb_info);
Int unregister_framebuffer(struct fb_info*fb_info);
fb注冊(cè)函數(shù)主要完成創(chuàng)建fb設(shè)備文件、把新的fb設(shè)備加入到registered_fb數(shù)組和convertfb_var_screeninfo to fb_videomode。Fb注銷函數(shù)作用則相反。
如何編寫一個(gè)幀緩沖設(shè)備驅(qū)動(dòng)?
內(nèi)核中提供了一個(gè)幀緩沖設(shè)備驅(qū)動(dòng)的骨架程序(skeletonfb.c),很有參考價(jià)值。現(xiàn)在大多數(shù)的LCD控制器都是SOC的,所以驅(qū)動(dòng)里用platform設(shè)備來(lái)進(jìn)行注冊(cè)。
幀緩存設(shè)備初始化,需要完成以下幾個(gè)工作:
1、映射LCD寄存器和申請(qǐng)LCD中斷;
2、申請(qǐng)fb_info并初始化fb_fix_screeninfo和fb_var_screeninfo結(jié)構(gòu)體;
3、根據(jù)具體LCD屏幕的特點(diǎn),完成LCD控制器的初始化;
4、申請(qǐng)幀緩沖設(shè)備的顯示緩存區(qū)空間;
5、調(diào)用register_framebuffer函數(shù)注冊(cè)幀緩沖設(shè)備。
實(shí)現(xiàn)fb_ops結(jié)構(gòu)體,其中fb_fillrect、fb_copyarea和fb_imageblit是必須的。
static struct fb_ops xxxfb_ops = {
.owner ?= THIS_MODULE,
.fb_open ?= xxxfb_open, ? ? ?/* 打開(kāi)fb設(shè)備 */
.fb_read ?= xxxfb_read,
.fb_write ?= xxxfb_write,
.fb_release ?= xxxfb_release, ?/* 關(guān)閉fb設(shè)備 */
.fb_check_var ?= xxxfb_check_var, ? ? ? ? /* 檢測(cè)可變參數(shù),并調(diào)整到支持的值 */
.fb_set_par ?= xxxfb_set_par, ? ? ? ? ? ? ? ? ? ? /* 初始化LCD控制器 */
.fb_setcolreg ?= xxxfb_setcolreg, ? ? ? ? ? ? ? ? ?/* 設(shè)置color寄存器 */
.fb_blank ?= xxxfb_blank, ? ? ? ? ? ? ? ? ? ? ? ? /* 顯示空白 */
.fb_pan_display= xxxfb_pan_display,
.fb_fillrect ?= xxxfb_fillrect,?/* Needed !!! 矩形填充 ?*/
.fb_copyarea ?= xxxfb_copyarea, /* Needed !!! ?數(shù)據(jù)拷貝 */
.fb_imageblit ?= xxxfb_imageblit, /* Needed !!! ?圖像填充 */
.fb_cursor ?= xxxfb_cursor, /* 顯示光標(biāo) */
.fb_rotate ?= xxxfb_rotate, ? ? ? ? ? ? ? /* 旋轉(zhuǎn)顯示 */
.fb_sync ?= xxxfb_sync,
.fb_ioctl ?= xxxfb_ioctl, ? ? ? ? ? ? ? ? ? ? ? ? ?/* fb特定的IOCTL */
.fb_mmap ?= xxxfb_mmap, ? ? ? ? ? ? /* fb特定的MMAP */
};
總結(jié)
以上是生活随笔為你收集整理的Framebuffer 机制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ESP8266的一个小问题
- 下一篇: framebuffer 保存 bmp图片