好象很简单哦~
fbtools.h
#ifndef _FBTOOLS_H_
#define _FBTOOLS_H_
#include
//a framebuffer device structure;
typedef struct fbdev{
int fb;
unsigned long fb_mem_offset;
unsigned long fb_mem;
struct fb_fix_screeninfo fb_fix;
struct fb_var_screeninfo fb_var;
char dev[20];
} FBDEV, *PFBDEV;
//open & init a frame buffer
//to use this function,
//you must set FBDEV.dev=\"/dev/fb0\"
//or \"/dev/fbX\"
//it\'s your frame buffer.
int fb_open(PFBDEV pFbdev);」
//close a frame buffer
int fb_close(PFBDEV pFbdev);
//get display depth
int get_display_depth(PFBDEV pFbdev);
//full screen clear
void fb_memset(void *addr, int c, size_t len);
#endif
fbtools.c
#include
#include
#include
#include
#include
#include
#include
#include
#include \"fbtools.h\"
#define TRUE 1
#define FALSE 0
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
//open & init a frame buffer
int fb_open(PFBDEV pFbdev)
{
pFbdev->fb = open(pFbdev->dev,O_RDWR);
if(pFbdev->fb < 0)
{
printf(\"Error opening %s: %m. Check kernel config\\n\",pFbdev->dev);
return FALSE;
}
if (-1 ==ioctl(pFbdev->fb,FBIOGET_VSCREENINFO,&(pFbdev->fb_var)))
{
printf(\"ioctl FBIOGET_VSCREENINFO\\n\");
return FALSE;
}
if (-1 ==ioctl(pFbdev->fb,FBIOGET_FSCREENINFO,&(pFbdev->fb_fix)))
{
printf(\"ioctl FBIOGET_FSCREENINFO\\n\");
return FALSE;
}
//map physics address to virtual address
pFbdev->fb_mem_offset = (unsignedlong)(pFbdev->fb_fix.smem_start) &(~PAGE_MASK);
pFbdev->fb_mem = (unsigned long int)mmap(NULL,pFbdev->fb_fix.smem_len +
pFbdev->fb_mem_offset,
PROT_READ | PROT_WRITE, MAP_SHARED, pFbdev->fb,0);
if (-1L == (long) pFbdev->fb_mem)
{
printf(\"mmap error! mem:%d offset:%d\\n\",pFbdev->fb_mem,
pFbdev->fb_mem_offset);
return FALSE;
}
return TRUE;
}
//close frame buffer
int fb_close(PFBDEV pFbdev)
{
close(pFbdev->fb);
pFbdev->fb=-1;
}
//get display depth
int get_display_depth(PFBDEV pFbdev);
{
if(pFbdev->fb<=0)
{
printf(\"fb device not open, open it first\\n\");
return FALSE;
}
return pFbdev->fb_var.bits_per_pixel;
}
//full screen clear
void fb_memset (void *addr, int c, size_t len)
{
memset(addr, c, len);
}
//use by test
#define DEBUG
#ifdef DEBUG
main()
{
FBDEV fbdev;
memset(&fbdev, 0, sizeof(FBDEV));
strcpy(fbdev.dev, \"/dev/fb0\");
if(fb_open(&fbdev)==FALSE)
{
printf(\"open frame buffer error\\n\");
return;
}
fb_memset(fbdev.fb_mem + fbdev.fb_mem_offset, 0,fbdev.fb_fix.smem_len);
fb_close(&fbdev);
}
PCI设备可以将自己的控制寄存器映射到物理内存空间,而后,对这些控制寄存器的访问,给变成了对物理内存的访问。因此,这些寄存器又被称为“memio”。一旦被映射到物理内存,Linux的普通进程就可以通过 mmap 将这些内存 I/O 映射到进程地址空间,这样就可以直接访问这些寄存器了。
当然,因为不同的显示芯片具有不同的加速能力,对memio的使用和定义也各自不同,这时,就需要针对加速芯片的不同类型来编写实现不同的加速功能。比如大多数芯片都提供了对矩形填充的硬件加速支持,但不同的芯片实现方式不同,这时,就需要针对不同的芯片类型编写不同的用来完成填充矩形的函数。
说到这里,读者可能已经意识到 FrameBuffer只是一个提供显示内存和显示芯片寄存器从物理内存映射到进程地址空间中的设备。所以,对于应用程序而言,如果希望在 FrameBuffer之上进行图形编程,还需要完成其他许多工作。举个例子来讲,FrameBuffer就像一张画布,使用什么样子的画笔,如何画画,还需要你自己动手完成。
int main () {
int fp=0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
fp = open ("/dev/fb0",O_RDWR);
if (fp < 0){
printf("Error : Can not open framebuffer device\n");
exit(1);
}
if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo)){
printf("Error reading fixed information\n");
exit(2);
}
if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo)){
printf("Error reading variable information\n");
exit(3);
}
printf("The mem is :%d\n",finfo.smem_len);
printf("The line_length is :%d\n",finfo.line_length);
printf("The xres is :%d\n",vinfo.xres);
printf("The yres is :%d\n",vinfo.yres);
printf("bits_per_pixel is :%d\n",vinfo.bits_per_pixel);
close (fp);
}
struct fb_var_screeninfo 和 struct fb_fix_screeninfo两个数据结
http://blog.sina.com.cn/s/blog_60f8c3bb0100x6qb.html