*一、FrameBuffer的原理*
FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口。
Linux是工作在保护模式下,所以用户态进程是无法象DOS那样使用显卡BIOS里提供的中断调用来实现直接写屏,Linux抽象出FrameBuffer这 个设备来供用户态进程实现直接写屏。Framebuffer机制模仿显卡的功能,将显卡硬件结构抽象掉,可以通过Framebuffer的读写直接对显存进行操 作。用户可以将Framebuffer看成是显示内存的一个映像,将其映射到进程地址空间之后,就可以直接进行读写操作,而写操作可以立即反应在屏幕上。这种操 作是抽象的,统一的。用户不必关心物理显存的位置、换页机制等等具体细节。这些都是由Framebuffer设备驱动来完成的。
但Framebuffer本身不具备任何运算数据的能力,就好比是一个暂时存放水的水池.CPU将运算后的结果放到这个水池,水池再将结果流到显示器.中间不会对数据做处理. 应用程序也可以直接读写这个水池的内容.在这种机制下,尽管Framebuffer需要真正的显卡驱动的支持,但所有显示任务都有CPU完成,因此CPU负担很重.
framebuffer的设备文件一般是 /dev/fb0、/dev/fb1 等等。
可以用命令: #dd if=/dev/zero of=/dev/fb 清空屏幕.
如果显示模式是 1024x768-8 位色,用命令:$ dd if=/dev/zero of=/dev/fb0 bs=1024 count=768 清空屏幕;
用命令: #dd if=/dev/fb of=fbfile 可以将fb中的内容保存下来;
可以重新写回屏幕: #dd if=fbfile of=/dev/fb;
在使用Framebuffer时,Linux是将显卡置于图形模式下的.
在应用程序中,一般通过将 FrameBuffer 设备映射到进程地址空间的方式使用,比如下面的程序就打开 /dev/fb0 设备,并通过mmap 系统调用进行地址映射,随后用 memset 将屏幕清空(这里假设显示模式是 1024x768-8 位色模式,线性内存模式):
int fb;
unsigned char* fb_mem;
fb = open ("/dev/fb0", O_RDWR);
fb_mem = mmap (NULL, 1024*768, PROT_READ|PROT_WRITE,MAP_SHARED,fb,0);
memset (fb_mem, 0, 1024*76);
FrameBuffer 设备还提供了若干ioctl命令,通过这些命令,可以获得显示设备的一些固定信息(比如显示内存大小)、与显示模式相关的可变信息(比如分辨率、象素结构、每扫描线的字节宽度),以及伪彩 色模式下的调色板信息等等。
通过 FrameBuffer设备,还可以获得当前内核所支持的加速显示卡的类型(通过固定信息得到),这种类型通常是和特定显示芯片相关的。比如目前最新的内核(2.4.9)中,就包含有 对S3、Matrox、nVidia、3Dfx 等等流行显示芯片的加速支持。在获得了加速芯片类型之后,应用程序就可以将 PCI设备的内存I/O(memio)映射到进程的地址空间。这些memio
一般是用来控制显示卡的寄存器,通过对这些寄存器的操作,应用程序就可以控制特定显卡的加速功能。
PCI设备可以将自己的控制寄存器映射到物理内存空间,而后,对这些控制寄存器的访问,给变成了对物理内存的访问。因此,这些寄存器又被称为"memio"。一旦被映 射到物理内存,Linux的普通进程就可以通过 mmap 将这些内存 I/O 映射到进程地址空间,这样就可以直接访问这些寄存器了。
当然,因为不同的显示芯片具有不同的加速能力,对memio的使用和定义也各自不同,这时,就需要针对加速芯片的不同类型来编写实现不同的加速功能。比如大多数芯片都提供了对矩形填充的硬件加速支持,但不同的芯片实现方 式不同,这时,就需要针对不同的芯片类型编写不同的用来完成填充矩形的函数。
FrameBuffer 只是一个提供显示内存和显示芯片寄存器从物理内存映射到进程地址空间中的设备。所以,对于应用程序而言,如果希望在FrameBuffer 之上进行图形编程,还需要自己动手完成其他许多工作。
Framebuffer对应的源文件在linux/drivers/video/目录下。总的抽象设备文件为fbcon.c,在这个目录下还有与各种显卡驱动相关的源文件。
(一)、分析Framebuffer设备驱动
需要特别提出的是在INTEL平台上,老式的VESA1.2卡,如CGA/EGA卡,是不能支持Framebuffer的,因为Framebuffer要求显卡支持线性帧缓冲,即CPU可以访问显缓冲中的每一位, 但是VESA1.2 卡只能允许CPU一次访问64K的地址空间。
FrameBuffer设备驱动基于如下两个文件:
1) linux/include/linux/fb.h
2) linux/drivers/video/fbmem.c 下面分析这两个文件。
1、fb.h
几乎主要的结构都是在这个中文件定义的。这些结构包括:
1)fb_var_screeninfo
这个结构描述了显示卡的特性:
struct fb_var_screeninfo
{
__u32 xres; /* visible resolution */
__u32 yres;
__u32 xres_virtual; /* virtual resolution */
__u32 yres_virtual;
__u32 xoffset; /* offset from virtual to visible resolution */
__u32 yoffset;
__u32 bits_per_pixel; /* guess what */
__u32 grayscale; /* != 0 Gray levels instead of colors */
struct fb_bitfield red; /* bitfield in fb mem if true color, */
struct fb_bitfield green; /* else only length is significant */
struct fb_bitfield blue;
struct fb_bitfield transp; /* transparency */
__u32 nonstd; /* != 0 Non standard pixel format */
__u32 activate; /* see FB_ACTIVATE_* */
__u32 height; /* height of picture in mm */
__u32 width; /* width of picture in mm */
__u32 accel_flags; /* acceleration flags (hints) */
/* Timing: All values in pixclocks, except pixclock (of course) */
__u32 pixclock; /* pixel clock in ps (pico seconds) */
__u32 left_margin; /* time from sync to picture */
__u32 right_margin; /* time from picture to sync */
__u32 upper_margin; /* time from sync to picture */
__u32 lower_margin;
__u32 hsync_len; /* length of horizontal sync */
__u32 vsync_len; /* length of vertical sync */
__u32 sync; /* see FB_SYNC_* */
__u32 vmode; /* see FB_VMODE_* */
__u32 reserved[6]; /* Reserved for future compatibility */
};
2) fb_fix_screeninfon
这个结构在显卡被设定模式后创建,它描述显示卡的属性,并且系统运行时不能被修改;比如FrameBuffer内存的起始地址。它依赖于被设定的模式,当一个模 式被设定后,内存信息由显示卡硬件给出,内存的位置等信息就不可以修改。
struct fb_fix_screeninfo {
char id[16]; /* identification string eg "TT Builtin" */
unsigned long smem_start; /* Start of frame buffer mem */
/* (physical address) */
__u32 smem_len; /* Length of frame buffer mem */
__u32 type; /* see FB_TYPE_* */
__u32 type_aux; /* Interleave for interleaved Planes */
__u32 visual; /* see FB_VISUAL_* */
__u16 xpanstep; /* zero if no hardware panning */
__u16 ypanstep; /* zero if no hardware panning */
__u16 ywrapstep; /* zero if no hardware ywrap */
__u32 line_length; /* length of a line in bytes */
unsigned long mmio_start; /* Start of Memory Mapped I/O */
/* (physical address) */
__u32 mmio_len; /* Length of Memory Mapped I/O */
__u32 accel; /* Type of acceleration available */
__u16 reserved[3]; /* Reserved for future compatibility */
};
3) fb_cmap
描述设备无关的颜色映射信息。可以通过FBIOGETCMAP 和 FBIOPUTCMAP 对应的ioctl操作设定或获取颜色映射信息.
struct fb_cmap {
__u32 start; /* First entry */
__u32 len; /* Number of entries */
__u16 *red; /* Red values */
__u16 *green;
__u16 *blue;
__u16 *transp; /* transparency, can be NULL */
};
4) fb_info
定义显卡的当前状态;fb_info结构仅在内核中可见,在这个结构中有一个fb_ops指针, 指向驱动设备工作所需的函数集。
struct fb_info {
char modename[40]; /* default video mode */
kdev_t node;
int flags;
int open; /* Has this been open already ? */
#define FBINFO_FLAG_MODULE 1 /* Low-level driver is a module */
struct fb_var_screeninfo var; /* Current var */
struct fb_fix_screeninfo fix; /* Current fix */
struct fb_monspecs monspecs; /* Current Monitor specs */
struct fb_cmap cmap; /* Current cmap */
struct fb_ops *fbops;
char *screen_base; /* Virtual address */
struct display *disp; /* initial display variable */
struct vc_data *display_fg; /* Console visible on this display */
char fontname[40]; /* default font name */
devfs_handle_t devfs_handle; /* Devfs handle for new name */
devfs_handle_t devfs_lhandle; /* Devfs handle for compat. symlink */
int (*changevar)(int); /* tell console var has changed */
int (*switch_con)(int, struct fb_info*);
/* tell fb to switch consoles */
int (*updatevar)(int, struct fb_info*);
/* tell fb to update the vars */
void (*blank)(int, struct fb_info*); /* tell fb to (un)blank the screen */
/* arg = 0: unblank */
/* arg > 0: VESA level (arg-1) */
void *pseudo_palette; /* Fake palette of 16 colors and
the cursor's color for non
palette mode */
/* From here on everything is device dependent */
void *par;
};
5) struct fb_ops
用户应用可以使用ioctl()系统调用来操作设备,这个结构就是用一支持ioctl()的这些操作的。
struct fb_ops {
/* open/release and usage marking */
struct module *owner;
int (*fb_open)(struct fb_info *info, int user);
int (*fb_release)(struct fb_info *info, int user);
/* get non settable parameters */
int (*fb_get_fix)(struct fb_fix_screeninfo *fix, int con,
struct fb_info *info);
/* get settable parameters */
int (*fb_get_var)(struct fb_var_screeninfo *var, int con,
struct fb_info *info);
/* set settable parameters */
int (*fb_set_var)(struct fb_var_screeninfo *var, int con,
struct fb_info *info);
/* get colormap */
int (*fb_get_cmap)(struct fb_cmap *cmap, int kspc, int con,
struct fb_info *info);
/* set colormap */
int (*fb_set_cmap)(struct fb_cmap *cmap, int kspc, int con,
struct fb_info *info);
/* pan display (optional) */
int (*fb_pan_display)(struct fb_var_screeninfo *var, int con,
struct fb_info *info);
/* perform fb specific ioctl (optional) */
int (*fb_ioctl)(struct inode *inode, struct file *file, unsigned int cmd,
unsigned long arg, int con, struct fb_info *info);
/* perform fb specific mmap */
int (*fb_mmap)(struct fb_info *info, struct file *file, struct
vm_area_struct *vma);
/* switch to/from raster image mode */
int (*fb_rasterimg)(struct fb_info *info, int start);
};
6) structure map
struct fb_info_gen | struct fb_info | fb_var_screeninfo
| | fb_fix_screeninfo
| | fb_cmap
| | modename[40]
| | fb_ops ---|--->ops on var
| | ... | fb_open
| | | fb_release
| | | fb_ioctl
| | | fb_mmap
| struct fbgen_hwswitch -|-> detect
| | encode_fix
| | encode_var
| | decode_fix
| | decode_var
| | get_var
| | set_var
| | getcolreg
| | setcolreg
| | pan_display
| | blank
| | set_disp
[编排有点困难,第一行的第一条竖线和下面的第一列竖线对齐,第一行的第二条竖线和下面的第二列竖线对齐就可以了]
这个结构 fbgen_hwswitch抽象了硬件的操作.虽然它不是必需的,但有时候很有用.
2、 fbmem.c
fbmem.c 处于Framebuffer设备驱动技术的中心位置.它为上层应用程序提供系统调用也为下一层的特定硬件驱动提供接口;那些底层硬件驱动需要用到这儿的接口来向 系统内核注册它们自己.
fbmem.c 为所有支持FrameBuffer的设备驱动提供了通用的接口,避免重复工作.
1) 全局变量
struct fb_info *registered_fb[FB_MAX];
int num_registered_fb;
这两变量记录了所有fb_info 结构的实例,fb_info 结构描述显卡的当前状态,所有设备对应的fb_info结构都保存在这个数组中,当一个FrameBuffer设备驱动向系统注册自己时,其对应的fb_info结构就会添加到这个结构中,同时num_registered_fb 为自动加1. static struct {
const char *name;
int (*init)(void);
int (*setup)(void);
} fb_drivers[] __initdata= { ....};
如果FrameBuffer设备被静态链接到内核,其对应的入口就会添加到这个表中;如果是动态加载的,即使用insmod/rmmod,就不需要关心这个表。
static struct file_operations fb_ops ={
owner: THIS_MODULE,
read: fb_read,
write: fb_write,
ioctl: fb_ioctl,
mmap: fb_mmap,
open: fb_open,
release: fb_release
};
这是一个提供给应用程序的接口.
2)fbmem.c 实现了如下函数.
register_framebuffer(struct fb_info *fb_info);
unregister_framebuffer(struct fb_info *fb_info);
这两个是提供给下层FrameBuffer设备驱动的接口,设备驱动通过这两函数向系统注册或注销自己。几乎底层设备驱动所要做的所有事情就是填充fb_inf o结构然后向系统注册或注销它。
(二)一个LCD显示芯片的驱动实例
以Skeleton LCD控制器驱动为例,在LINUX中存有一个/fb/skeleton.c的skeleton的Framebuffer驱动程序,很简单,仅仅是填充了fb_info结构,并且注册/注销自己。设备驱动是向用户程序提供系统调用接口,所以我们需要实现底层硬件操作并且定义file_operations结构来向系统提供系统调用接口,从而实现更有效的LCD控制器驱动程序。
1)在系统内存中分配显存
在fbmem.c文件中可以看到,file_operations结构中的open()和release()操作不需底层支持,但read()、write()和 mmap()操作需要函数fb_get_fix()的支持.因此需要重新实现函数fb_get_fix()。另外还需要在系统内存中分配显存空间,大多数的LCD控制器都没有自己的显存空间,被分配的地址空间的起 始地址与长度将会被填充到fb_fix_screeninfo结构的smem_start 和smem_len 的两个变量中.被分配的空间必须是物理连续的。
2)实现 fb_ops 中的函数
用户应用程序通过ioctl()系统调用操作硬件,fb_ops 中的函数就用于支持这些操作。(注: fb_ops结构与file_operations结构不同,fb_ops是底层操作的抽象,而file_operations是提供给上层系统调用的接口,可以直接调用ioctl()系统调用在文件fbmem.c中实现,通过观察可以发现ioctl()命令与fb_ops's 中函数的关系:
FBIOGET_VSCREENINFO fb_get_var
FBIOPUT_VSCREENINFO fb_set_var
FBIOGET_FSCREENINFO fb_get_fix
FBIOPUTCMAP fb_set_cmap
FBIOGETCMAP fb_get_cmap
FBIOPAN_DISPLAY fb_pan_display 如果我们定义了fb_XXX_XXX 方法,用户程序就可以使用FBIOXXXX宏的ioctl()操作来操作硬件。
文件linux/drivers/video/fbgen.c或者linux/drivers/video目录下的其它设备驱动是比较好的参考资料。在所有的这 些函数中fb_set_var()是最重要的,它用于设定显示卡的模式和其它属性,下面是函数fb_set_var()的执行步骤:
1)检测是否必须设定模式
2)设定模式
3)设定颜色映射
4) 根据以前的设定重新设置LCD控制器的各寄存器。
第四步表明了底层操作到底放置在何处。在系统内存中分配显存后,显存的起始地址及长度将被设定到LCD控制器的各寄存器中(一般通过fb_set_var()函数),显存中的内容将自动被LCD控制器输出到屏幕上。另一方面,用户程序通过函数mmap()将显存映射到用户进程地址空间中,然后用户进程向映射空间发送 的所有数据都将会被显示到LCD显示器上。
二 Framebuffer的配置及应用
借助于framebuffer,我们能够在console下面作很多事情。首先下载framebuffer的配置工具fbset:
# apt-get install fbset 下载完毕后,配置文件/etc/fb.modes随之产生。
比较简单的作法是用万能的vesafb,如果它被编译进了内核,如:
Device Drivers -> Graphics support ->
- VESA VGA graphics support
那么在grub内核引导那一行的后面加上vga=791 它的含义是VESA framebuffer console @ 1024x768x64k,进入系统后可以直接使用framebuffer,看一下这种情况下的各项数据:
# fbset -s
mode "1024x768-76"
# D: 78.653 MHz, H: 59.949 kHz, V: 75.694 Hz
geometry 1024 768 1024 768 16
timings 12714 128 32 16 4 128 4
rgba 5/11,6/5,5/0,0/0
endmode
用具体显卡的framebuffer驱动是另一种选择,拿Nvidia显卡为例,Nvidia显卡的xorg驱动模块与其framebuffer的驱动模块是互相排斥的,如果要用一个就必须清除另一个:
# rmmod nvidia
装载nvidia的framebuffer驱动:
# modprobe nvidiafb
装载成功的时候,会产生/dev/fb0设备,console屏幕上的字体会有变化。
看一下当前的配置:
# fbset -s
mode "1024x768-85"
# D: 94.500 MHz, H: 68.677 kHz, V: 84.997 Hz
geometry 1024 768 1024 32767 8
timings 10582 208 48 36 1 96 3
hsync high
vsync high
accel true
rgba 8/0,8/0,8/0,0/0
endmode
需要改变一下geometry及色深:
# fbset -g 1024 768 1024 768 32
# fbset -s
mode "1024x768-85"
# D: 94.500 MHz, H: 68.677 kHz, V: 84.997 Hz
geometry 1024 768 1024 768 32
timings 10582 208 48 36 1 96 3
hsync high
vsync high
accel true
rgba 8/16,8/8,8/0,8/24
endmode
我们把它与使用VESA ramebuffer后的数据比较一下,显然,根据具体的显卡来驱动framebuffer可以在颜色上达到最佳值,好,现在我们在console下面能够作的事情:
一、视频播放,可以用mplayer 或者 fbxine:
# mplayer -vo fbdev -vf scale=1024:768 video_file.avi
-vo fbdev 是告诉mplayer用framebuffer作视频驱动.
-vf scale=1024:768 是全屏的方法,可按屏幕的具体情况作调整
用fbxine的话需要下载:
# apt-get install xine-console
二、图片文件与pdf文件浏览:
# apt-get install fbi
用这个软件包里的fbi可以浏览图片,fbgs可以观看pdf文件:
# fbi -a *jpg
# fbgs -c *pdf
三、中文显示:
# apt-get install jfbterm
# jfbterm
中文显示的效果完美。
三 Framebuffer使用测试
这两天拾起以前做过的Framebuffer,不相同的是以前在嵌入式上做的,现在在自己电脑上Debian上进行测试,不过都类似罢了,嵌入式里要初始化很多东西。下面具体列一下步骤。至于Framebuffer的原理,就我的理解是比较简单的,无非往mmap好的fb上填写显示数据罢了,不对这些数据进行处理,FrameBuffer 只是一个提供显示内存和显示芯片寄存器从物理内存映射到进程地址空间中的设备,它需要真正的显卡驱动的支持。在这次测试中,我用了默认就安装的vesafb,好像又被称为万能Fb驱动。
1、 首先在系统Grub启动时按e进入命令启动行的编辑模式,改为:kernel /boot/vmlinuz-2.6.18-5-686 root=/dev/sda6 ro vga=791(vga=791表示fb用1024 * 768 * 16bpp,其他模式的参数可以上网查查);
2、 进入系统的命令行模式,编译fb测试例子:gcc fb_test.c;
3、 允许测试例子:sudo ./a.out >> fb.txt(必须要用超级用户权限,>>将屏幕打印写到fb.txt中),效果如下:
打印如下:
4、还可以通过fb在命令行模式下看视频,如:sudo mplayer –vo fbdev ./air_nessesity.mpg。
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <linux/fb.h>
- #include <linux/kd.h>
- #include <sys/mman.h>
- #include <sys/ioctl.h>
- #include <sys/time.h>
- #include <string.h>
- #include <errno.h>
-
- struct fb_var_screeninfo vinfo;
- struct fb_fix_screeninfo finfo;
- char *frameBuffer = 0;
-
- //打印fb驱动中fix结构信息,注:在fb驱动加载后,fix结构不可被修改。
- void
- printFixedInfo ()
- {
- printf ("Fixed screen info:\n"
- "\tid: %s\n"
- "\tsmem_start: 0x%lx\n"
- "\tsmem_len: %d\n"
- "\ttype: %d\n"
- "\ttype_aux: %d\n"
- "\tvisual: %d\n"
- "\txpanstep: %d\n"
- "\typanstep: %d\n"
- "\tywrapstep: %d\n"
- "\tline_length: %d\n"
- "\tmmio_start: 0x%lx\n"
- "\tmmio_len: %d\n"
- "\taccel: %d\n"
- "\n",
- finfo.id, finfo.smem_start, finfo.smem_len, finfo.type,
- finfo.type_aux, finfo.visual, finfo.xpanstep, finfo.ypanstep,
- finfo.ywrapstep, finfo.line_length, finfo.mmio_start,
- finfo.mmio_len, finfo.accel);
- }
-
- //打印fb驱动中var结构信息,注:fb驱动加载后,var结构可根据实际需要被重置
- void
- printVariableInfo ()
- {
- printf ("Variable screen info:\n"
- "\txres: %d\n"
- "\tyres: %d\n"
- "\txres_virtual: %d\n"
- "\tyres_virtual: %d\n"
- "\tyoffset: %d\n"
- "\txoffset: %d\n"
- "\tbits_per_pixel: %d\n"
- "\tgrayscale: %d\n"
- "\tred: offset: %2d, length: %2d, msb_right: %2d\n"
- "\tgreen: offset: %2d, length: %2d, msb_right: %2d\n"
- "\tblue: offset: %2d, length: %2d, msb_right: %2d\n"
- "\ttransp: offset: %2d, length: %2d, msb_right: %2d\n"
- "\tnonstd: %d\n"
- "\tactivate: %d\n"
- "\theight: %d\n"
- "\twidth: %d\n"
- "\taccel_flags: 0x%x\n"
- "\tpixclock: %d\n"
- "\tleft_margin: %d\n"
- "\tright_margin: %d\n"
- "\tupper_margin: %d\n"
- "\tlower_margin: %d\n"
- "\thsync_len: %d\n"
- "\tvsync_len: %d\n"
- "\tsync: %d\n"
- "\tvmode: %d\n"
- "\n",
- vinfo.xres, vinfo.yres, vinfo.xres_virtual, vinfo.yres_virtual,
- vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel,
- vinfo.grayscale, vinfo.red.offset, vinfo.red.length,
- vinfo.red.msb_right, vinfo.green.offset, vinfo.green.length,
- vinfo.green.msb_right, vinfo.blue.offset, vinfo.blue.length,
- vinfo.blue.msb_right, vinfo.transp.offset, vinfo.transp.length,
- vinfo.transp.msb_right, vinfo.nonstd, vinfo.activate,
- vinfo.height, vinfo.width, vinfo.accel_flags, vinfo.pixclock,
- vinfo.left_margin, vinfo.right_margin, vinfo.upper_margin,
- vinfo.lower_margin, vinfo.hsync_len, vinfo.vsync_len,
- vinfo.sync, vinfo.vmode);
- }
-
- //画大小为width*height的同色矩阵,8alpha+8reds+8greens+8blues
- void
- drawRect_rgb32 (int x0, int y0, int width, int height, int color)
- {
- const int bytesPerPixel = 4;
- const int stride = finfo.line_length / bytesPerPixel;
-
- int *dest = (int *) (frameBuffer)
- + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);
-
- int x, y;
- for (y = 0; y < height; ++y)
- {
- for (x = 0; x < width; ++x)
- {
- dest[x] = color;
- }
- dest += stride;
- }
- }
-
- //画大小为width*height的同色矩阵,5reds+6greens+5blues
- void
- drawRect_rgb16 (int x0, int y0, int width, int height, int color)
- {
- const int bytesPerPixel = 2;
- const int stride = finfo.line_length / bytesPerPixel;
- const int red = (color & 0xff0000) >> (16 + 3);
- const int green = (color & 0xff00) >> (8 + 2);
- const int blue = (color & 0xff) >> 3;
- const short color16 = blue | (green << 5) | (red << (5 + 6));
-
- short *dest = (short *) (frameBuffer)
- + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);
-
- int x, y;
- for (y = 0; y < height; ++y)
- {
- for (x = 0; x < width; ++x)
- {
- dest[x] = color16;
- }
- dest += stride;
- }
- }
-
- //画大小为width*height的同色矩阵,5reds+5greens+5blues
- void
- drawRect_rgb15 (int x0, int y0, int width, int height, int color)
- {
- const int bytesPerPixel = 2;
- const int stride = finfo.line_length / bytesPerPixel;
- const int red = (color & 0xff0000) >> (16 + 3);
- const int green = (color & 0xff00) >> (8 + 3);
- const int blue = (color & 0xff) >> 3;
- const short color15 = blue | (green << 5) | (red << (5 + 5)) | 0x8000;
-
- short *dest = (short *) (frameBuffer)
- + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);
-
- int x, y;
- for (y = 0; y < height; ++y)
- {
- for (x = 0; x < width; ++x)
- {
- dest[x] = color15;
- }
- dest += stride;
- }
- }
-
- void
- drawRect (int x0, int y0, int width, int height, int color)
- {
- switch (vinfo.bits_per_pixel)
- {
- case 32:
- drawRect_rgb32 (x0, y0, width, height, color);
- break;
- case 16:
- drawRect_rgb16 (x0, y0, width, height, color);
- break;
- case 15:
- drawRect_rgb15 (x0, y0, width, height, color);
- break;
- default:
- printf ("Warning: drawRect() not implemented for color depth %i\n",
- vinfo.bits_per_pixel);
- break;
- }
- }
-
- #define PERFORMANCE_RUN_COUNT 5
- void
- performSpeedTest (void *fb, int fbSize)
- {
- int i, j, run;
- struct timeval startTime, endTime;
- unsigned long long results[PERFORMANCE_RUN_COUNT];
- unsigned long long average;
- unsigned int *testImage;
-
- unsigned int randData[17] = {
- 0x3A428472, 0x724B84D3, 0x26B898AB, 0x7D980E3C, 0x5345A084,
- 0x6779B66B, 0x791EE4B4, 0x6E8EE3CC, 0x63AF504A, 0x18A21B33,
- 0x0E26EB73, 0x022F708E, 0x1740F3B0, 0x7E2C699D, 0x0E8A570B,
- 0x5F2C22FB, 0x6A742130
- };
-
- printf ("Frame Buffer Performance test...\n");
- for (run = 0; run < PERFORMANCE_RUN_COUNT; ++run)
- {
- /* Generate test image with random(ish) data: */
- testImage = (unsigned int *) malloc (fbSize);
- j = run;
- for (i = 0; i < (int) (fbSize / sizeof (int)); ++i)
- {
- testImage[i] = randData[j];
- j++;
- if (j >= 17)
- j = 0;
- }
-
- gettimeofday (&startTime, NULL);
- memcpy (fb, testImage, fbSize);
- gettimeofday (&endTime, NULL);
-
- long secsDiff = endTime.tv_sec - startTime.tv_sec;
- results[run] =
- secsDiff * 1000000 + (endTime.tv_usec - startTime.tv_usec);
-
- free (testImage);
- }
-
- average = 0;
- for (i = 0; i < PERFORMANCE_RUN_COUNT; ++i)
- average += results[i];
- average = average / PERFORMANCE_RUN_COUNT;
-
- printf (" Average: %llu usecs\n", average);
- printf (" Bandwidth: %.03f MByte/Sec\n",
- (fbSize / 1048576.0) / ((double) average / 1000000.0));
- printf (" Max. FPS: %.03f fps\n\n",
- 1000000.0 / (double) average);
-
- /* Clear the framebuffer back to black again: */
- memset (fb, 0, fbSize);
- }
-
- int
- main (int argc, char **argv)
- {
- const char *devfile = "/dev/fb0";
- long int screensize = 0;
- int fbFd = 0;
-
-
- /* Open the file for reading and writing */
- fbFd = open (devfile, O_RDWR);
- if (fbFd == -1)
- {
- perror ("Error: cannot open framebuffer device");
- exit (1);
- }
-
- //获取finfo信息并显示
- if (ioctl (fbFd, FBIOGET_FSCREENINFO, &finfo) == -1)
- {
- perror ("Error reading fixed information");
- exit (2);
- }
- printFixedInfo ();
- //获取vinfo信息并显示
- if (ioctl (fbFd, FBIOGET_VSCREENINFO, &vinfo) == -1)
- {
- perror ("Error reading variable information");
- exit (3);
- }
- printVariableInfo ();
-
- /* Figure out the size of the screen in bytes */
- screensize = finfo.smem_len;
-
- /* Map the device to memory */
- frameBuffer =
- (char *) mmap (0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
- fbFd, 0);
- if (frameBuffer == MAP_FAILED)
- {
- perror ("Error: Failed to map framebuffer device to memory");
- exit (4);
- }
-
- //测试virt fb的性能
- performSpeedTest (frameBuffer, screensize);
-
- printf ("Will draw 3 rectangles on the screen,\n"
- "they should be colored red, green and blue (in that order).\n");
- drawRect (vinfo.xres / 8, vinfo.yres / 8,
- vinfo.xres / 4, vinfo.yres / 4, 0xffff0000);
- drawRect (vinfo.xres * 3 / 8, vinfo.yres * 3 / 8,
- vinfo.xres / 4, vinfo.yres / 4, 0xff00ff00);
- drawRect (vinfo.xres * 5 / 8, vinfo.yres * 5 / 8,
- vinfo.xres / 4, vinfo.yres / 4, 0xff0000ff);
-
- sleep (5);
- printf (" Done.\n");
-
- munmap (frameBuffer, screensize); //解除内存映射,与mmap对应
-
- close (fbFd);
- return 0;
- }
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <linux/fb.h>
- #include <linux/kd.h>
- #include <sys/mman.h>
- #include <sys/ioctl.h>
- #include <sys/time.h>
- #include <string.h>
- #include <errno.h>
-
- struct fb_var_screeninfo vinfo;
- struct fb_fix_screeninfo finfo;
- char *frameBuffer = 0;
-
- //打印fb驱动中fix结构信息,注:在fb驱动加载后,fix结构不可被修改。
- void
- printFixedInfo ()
- {
- printf ("Fixed screen info:\n"
- "\tid: %s\n"
- "\tsmem_start: 0x%lx\n"
- "\tsmem_len: %d\n"
- "\ttype: %d\n"
- "\ttype_aux: %d\n"
- "\tvisual: %d\n"
- "\txpanstep: %d\n"
- "\typanstep: %d\n"
- "\tywrapstep: %d\n"
- "\tline_length: %d\n"
- "\tmmio_start: 0x%lx\n"
- "\tmmio_len: %d\n"
- "\taccel: %d\n"
- "\n",
- finfo.id, finfo.smem_start, finfo.smem_len, finfo.type,
- finfo.type_aux, finfo.visual, finfo.xpanstep, finfo.ypanstep,
- finfo.ywrapstep, finfo.line_length, finfo.mmio_start,
- finfo.mmio_len, finfo.accel);
- }
-
- //打印fb驱动中var结构信息,注:fb驱动加载后,var结构可根据实际需要被重置
- void
- printVariableInfo ()
- {
- printf ("Variable screen info:\n"
- "\txres: %d\n"
- "\tyres: %d\n"
- "\txres_virtual: %d\n"
- "\tyres_virtual: %d\n"
- "\tyoffset: %d\n"
- "\txoffset: %d\n"
- "\tbits_per_pixel: %d\n"
- "\tgrayscale: %d\n"
- "\tred: offset: %2d, length: %2d, msb_right: %2d\n"
- "\tgreen: offset: %2d, length: %2d, msb_right: %2d\n"
- "\tblue: offset: %2d, length: %2d, msb_right: %2d\n"
- "\ttransp: offset: %2d, length: %2d, msb_right: %2d\n"
- "\tnonstd: %d\n"
- "\tactivate: %d\n"
- "\theight: %d\n"
- "\twidth: %d\n"
- "\taccel_flags: 0x%x\n"
- "\tpixclock: %d\n"
- "\tleft_margin: %d\n"
- "\tright_margin: %d\n"
- "\tupper_margin: %d\n"
- "\tlower_margin: %d\n"
- "\thsync_len: %d\n"
- "\tvsync_len: %d\n"
- "\tsync: %d\n"
- "\tvmode: %d\n"
- "\n",
- vinfo.xres, vinfo.yres, vinfo.xres_virtual, vinfo.yres_virtual,
- vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel,
- vinfo.grayscale, vinfo.red.offset, vinfo.red.length,
- vinfo.red.msb_right, vinfo.green.offset, vinfo.green.length,
- vinfo.green.msb_right, vinfo.blue.offset, vinfo.blue.length,
- vinfo.blue.msb_right, vinfo.transp.offset, vinfo.transp.length,
- vinfo.transp.msb_right, vinfo.nonstd, vinfo.activate,
- vinfo.height, vinfo.width, vinfo.accel_flags, vinfo.pixclock,
- vinfo.left_margin, vinfo.right_margin, vinfo.upper_margin,
- vinfo.lower_margin, vinfo.hsync_len, vinfo.vsync_len,
- vinfo.sync, vinfo.vmode);
- }
-
- //画大小为width*height的同色矩阵,8alpha+8reds+8greens+8blues
- void
- drawRect_rgb32 (int x0, int y0, int width, int height, int color)
- {
- const int bytesPerPixel = 4;
- const int stride = finfo.line_length / bytesPerPixel;
-
- int *dest = (int *) (frameBuffer)
- + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);
-
- int x, y;
- for (y = 0; y < height; ++y)
- {
- for (x = 0; x < width; ++x)
- {
- dest[x] = color;
- }
- dest += stride;
- }
- }
-
- //画大小为width*height的同色矩阵,5reds+6greens+5blues
- void
- drawRect_rgb16 (int x0, int y0, int width, int height, int color)
- {
- const int bytesPerPixel = 2;
- const int stride = finfo.line_length / bytesPerPixel;
- const int red = (color & 0xff0000) >> (16 + 3);
- const int green = (color & 0xff00) >> (8 + 2);
- const int blue = (color & 0xff) >> 3;
- const short color16 = blue | (green << 5) | (red << (5 + 6));
-
- short *dest = (short *) (frameBuffer)
- + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);
-
- int x, y;
- for (y = 0; y < height; ++y)
- {
- for (x = 0; x < width; ++x)
- {
- dest[x] = color16;
- }
- dest += stride;
- }
- }
-
- //画大小为width*height的同色矩阵,5reds+5greens+5blues
- void
- drawRect_rgb15 (int x0, int y0, int width, int height, int color)
- {
- const int bytesPerPixel = 2;
- const int stride = finfo.line_length / bytesPerPixel;
- const int red = (color & 0xff0000) >> (16 + 3);
- const int green = (color & 0xff00) >> (8 + 3);
- const int blue = (color & 0xff) >> 3;
- const short color15 = blue | (green << 5) | (red << (5 + 5)) | 0x8000;
-
- short *dest = (short *) (frameBuffer)
- + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);
-
- int x, y;
- for (y = 0; y < height; ++y)
- {
- for (x = 0; x < width; ++x)
- {
- dest[x] = color15;
- }
- dest += stride;
- }
- }
-
- void
- drawRect (int x0, int y0, int width, int height, int color)
- {
- switch (vinfo.bits_per_pixel)
- {
- case 32:
- drawRect_rgb32 (x0, y0, width, height, color);
- break;
- case 16:
- drawRect_rgb16 (x0, y0, width, height, color);
- break;
- case 15:
- drawRect_rgb15 (x0, y0, width, height, color);
- break;
- default:
- printf ("Warning: drawRect() not implemented for color depth %i\n",
- vinfo.bits_per_pixel);
- break;
- }
- }
-
- #define PERFORMANCE_RUN_COUNT 5
- void
- performSpeedTest (void *fb, int fbSize)
- {
- int i, j, run;
- struct timeval startTime, endTime;
- unsigned long long results[PERFORMANCE_RUN_COUNT];
- unsigned long long average;
- unsigned int *testImage;
-
- unsigned int randData[17] = {
- 0x3A428472, 0x724B84D3, 0x26B898AB, 0x7D980E3C, 0x5345A084,
- 0x6779B66B, 0x791EE4B4, 0x6E8EE3CC, 0x63AF504A, 0x18A21B33,
- 0x0E26EB73, 0x022F708E, 0x1740F3B0, 0x7E2C699D, 0x0E8A570B,
- 0x5F2C22FB, 0x6A742130
- };
-
- printf ("Frame Buffer Performance test...\n");
- for (run = 0; run < PERFORMANCE_RUN_COUNT; ++run)
- {
- /* Generate test image with random(ish) data: */
- testImage = (unsigned int *) malloc (fbSize);
- j = run;
- for (i = 0; i < (int) (fbSize / sizeof (int)); ++i)
- {
- testImage[i] = randData[j];
- j++;
- if (j >= 17)
- j = 0;
- }
-
- gettimeofday (&startTime, NULL);
- memcpy (fb, testImage, fbSize);
- gettimeofday (&endTime, NULL);
-
- long secsDiff = endTime.tv_sec - startTime.tv_sec;
- results[run] =
- secsDiff * 1000000 + (endTime.tv_usec - startTime.tv_usec);
-
- free (testImage);
- }
-
- average = 0;
- for (i = 0; i < PERFORMANCE_RUN_COUNT; ++i)
- average += results[i];
- average = average / PERFORMANCE_RUN_COUNT;
-
- printf (" Average: %llu usecs\n", average);
- printf (" Bandwidth: %.03f MByte/Sec\n",
- (fbSize / 1048576.0) / ((double) average / 1000000.0));
- printf (" Max. FPS: %.03f fps\n\n",
- 1000000.0 / (double) average);
-
- /* Clear the framebuffer back to black again: */
- memset (fb, 0, fbSize);
- }
-
- int
- main (int argc, char **argv)
- {
- const char *devfile = "/dev/fb0";
- long int screensize = 0;
- int fbFd = 0;
-
-
- /* Open the file for reading and writing */
- fbFd = open (devfile, O_RDWR);
- if (fbFd == -1)
- {
- perror ("Error: cannot open framebuffer device");
- exit (1);
- }
-
- //获取finfo信息并显示
- if (ioctl (fbFd, FBIOGET_FSCREENINFO, &finfo) == -1)
- {
- perror ("Error reading fixed information");
- exit (2);
- }
- printFixedInfo ();
- //获取vinfo信息并显示
- if (ioctl (fbFd, FBIOGET_VSCREENINFO, &vinfo) == -1)
- {
- perror ("Error reading variable information");
- exit (3);
- }
- printVariableInfo ();
-
- /* Figure out the size of the screen in bytes */
- screensize = finfo.smem_len;
-
- /* Map the device to memory */
- frameBuffer =
- (char *) mmap (0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
- fbFd, 0);
- if (frameBuffer == MAP_FAILED)
- {
- perror ("Error: Failed to map framebuffer device to memory");
- exit (4);
- }
-
- //测试virt fb的性能
- performSpeedTest (frameBuffer, screensize);
-
- printf ("Will draw 3 rectangles on the screen,\n"
- "they should be colored red, green and blue (in that order).\n");
- drawRect (vinfo.xres / 8, vinfo.yres / 8,
- vinfo.xres / 4, vinfo.yres / 4, 0xffff0000);
- drawRect (vinfo.xres * 3 / 8, vinfo.yres * 3 / 8,
- vinfo.xres / 4, vinfo.yres / 4, 0xff00ff00);
- drawRect (vinfo.xres * 5 / 8, vinfo.yres * 5 / 8,
- vinfo.xres / 4, vinfo.yres / 4, 0xff0000ff);
-
- sleep (5);
- printf (" Done.\n");
-
- munmap (frameBuffer, screensize); //解除内存映射,与mmap对应
-
- close (fbFd);
- return 0;
- }
- #include <stdlib.h>