【RFB】linux-framebuffer设备相关参数

原文地址

http://blog.csdn.net/tianshuai1111/article/details/7630590


如果应用程序需要知道Framebuffer设备的相关参数,必须通过ioctl()系统调用来完成。

在头文件<linux/fb.h>中定义了所有的ioctl命令字,不过,最常用的ioctl命令字是下面这两个:FBIOGET_FSCREENINFO和FBIOGET_VSCREENINFO。

前者返回与Framebuffer有关的固定的信息,比如图形硬件上实际的帧缓存空间的大小、能否硬件加速等信息。

而后者返回的是与Framebuffer有关的可变信息。

之所以可变,是因为对同样的图形硬件,可以工作在不同的模式下。

简单来讲,一个支持1024x768x24图形模式的硬件通常也能工作在800x600x16的图形模式下。

可变的信息就是指Framebuffer的长度、宽度以及颜色深度等信息。


这两个命令字相关的结构体有两个:struct fb_fix_screeninfo和struct fb_var_screeninfo。



[html] view plain copy
  1. struct fb_fix_screeninfo {

  2. char id[16]; /* identification string eg "TT Builtin" */

  3. unsigned long smem_start; /* Start of frame buffer mem */

  4. /* (physical address) */

  5. __u32 smem_len; /* Length of frame buffer mem */

  6. __u32 type; /* see FB_TYPE_* */

  7. __u32 type_aux; /* Interleave for interleaved Planes */

  8. __u32 visual; /* see FB_VISUAL_* */

  9. __u16 xpanstep; /* zero if no hardware panning */

  10. __u16 ypanstep; /* zero if no hardware panning */

  11. __u16 ywrapstep; /* zero if no hardware ywrap */

  12. __u32 line_length; /* length of a line in bytes */

  13. unsigned long mmio_start; /* Start of Memory Mapped I/O */

  14. /* (physical address) */

  15. __u32 mmio_len; /* Length of Memory Mapped I/O */

  16. __u32 accel; /* Indicate to driver which */

  17. /* specific chip/card we have */

  18. __u16 reserved[3]; /* Reserved for future compatibility */

  19. };




[html] view plain copy
  1. struct fb_var_screeninfo {

  2. __u32 xres; /* visible resolution */

  3. __u32 yres;

  4. __u32 xres_virtual; /* virtual resolution */

  5. __u32 yres_virtual;

  6. __u32 xoffset; /* offset from virtual to visible */

  7. __u32 yoffset; /* resolution */

  8. __u32 bits_per_pixel; /* guess what */

  9. __u32 grayscale; /* != 0 Graylevels instead of colors */

  10. struct fb_bitfield red; /* bitfield in fb mem if true color, */

  11. struct fb_bitfield green; /* else only length is significant */

  12. struct fb_bitfield blue;

  13. struct fb_bitfield transp; /* transparency */

  14. __u32 nonstd; /* != 0 Non standard pixel format */

  15. __u32 activate; /* see FB_ACTIVATE_* */

  16. __u32 height; /* height of picture in mm */

  17. __u32 width; /* width of picture in mm */

  18. __u32 accel_flags; /* (OBSOLETE) see fb_info.flags */

  19. /* Timing: All values in pixclocks, except pixclock (of course) */

  20. __u32 pixclock; /* pixel clock in ps (pico seconds) */

  21. __u32 left_margin; /* time from sync to picture */

  22. __u32 right_margin; /* time from picture to sync */

  23. __u32 upper_margin; /* time from sync to picture */

  24. __u32 lower_margin;

  25. __u32 hsync_len; /* length of horizontal sync */

  26. __u32 vsync_len; /* length of vertical sync */

  27. __u32 sync; /* see FB_SYNC_* */

  28. __u32 vmode; /* see FB_VMODE_* */

  29. __u32 rotate; /* angle we rotate counter clockwise */

  30. __u32 reserved[5]; /* Reserved for future compatibility */

  31. };



这两个结构体都比较大,前者用于保存Framebuffer设备的固定信息,后者用于保存Framebuffer设备的可变信息。

在调用ioctl()的时候,要用到这两个结构体。

应用程序中通常要用到struct fb_var_screeninfo的下面这几个字段:

xres、yres、bits_per_pixel,分别表示x轴的分辨率、y轴的分辨率以及每像素的颜色深度(颜色深度的单位为bit/pixel),其类型定义都是无符号32位整型数。

http://hi.baidu.com/atoe/blog/item/e8da6416912a8a4a20a4e94f.html

http://hi.baidu.com/excellentderek/blog/item/f387e64e24b713cdd0c86a59.html

图形系统开发基础(挺详细)

http://linux.chinaunix.net/bbs/thread-1063136-1-1.html

又一个framebuffer编程的例子。

-------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>

char *fb_addr;
unsigned fb_size;

int print_screen(char *buf,int width,int height);

int main(int argc,char *argv[])
{
int screen_fbd=0;

struct fb_fix_screeninfo fb_fix;
struct fb_var_screeninfo fb_var;

char *env=NULL;

short *picture;

env="/dev/fb0";

screen_fbd=open(env,O_RDWR);

printf("Success opening framebuffer device %s/n",env);

ioctl(screen_fbd,FBIOGET_FSCREENINFO,&fb_fix);

printf("fb_fix.line_length=%d/n",fb_fix.line_length);
printf("fb_fix.accel=%d/n",fb_fix.accel);

ioctl(screen_fbd,FBIOGET_VSCREENINFO,&fb_var);

printf("fb_var.xres=%d/n",fb_var.xres);
printf("fb_var.yres=%d/n",fb_var.yres);

fb_size=fb_var.yres*fb_fix.line_length;

fb_addr=(char *)mmap(NULL,fb_size,PROT_READ|PROT_WRITE,MAP_SHARED,screen_fbd,0);
/*fb_addr的获取,是很核心的步骤,表示成功获得了framebuffer设备*/

picture=(char *)malloc(fb_var.yres*fb_fix.line_length);
memset(picture,0xFF,fb_var.yres*fb_fix.line_length);
/*注意,这里对颜色的赋值只是一次赋一半值,也就是一个字节,8bit*/
/*而事实上,一个像素的颜色值是16bit*/
/*0xFFFF就是白色*/
/*介绍一下16bit的颜色的类型,颜色是由RGB组成,如果是565排列,
则依次为Red Green Blue
11111 111111 11111
*/

print_screen(picture,fb_var.xres,fb_var.yres);

return 0;
}

int print_screen(char *buf,int width,int height)
{
short *t_data=(short *)buf;

short *t_fb_addr=(short *)fb_addr;

int bytew=width<<1; /*像素数乘以2即是字节数,因为颜色深度是2个字节(16bit)*/

while(--height>=0)
{
memcpy(t_fb_addr,t_data,bytew); /*一行的数据赋值*/
t_fb_addr += width;
t_data += width;
}
}


你可能感兴趣的:(硬件,应用程序,结构体,信息,空间)