Linux 커 널 수련 의 framebuffer 분석
10170 단어 리 눅 스 커 널 학습
Linux 소스 패키지 중/document/fb/framebuffer.txt 는 다음 과 같이 소개 합 니 다.frame buffer 장 치 는 그래 픽 하드웨어 에 대한 추상 화 를 제공 합 니 다.It represents the frame buffer of some video hardware and allows application software to access the graphics hardware through a well-defined interface, so the software doesn't need to know anything about the low-level (hardware register) stuff.
Frame buffer 메커니즘 은 그래 픽 디 스 플레이 카드 에 추상 적 인 층 을 제공 합 니 다.응용 프로그램 이 바 텀 하드웨어 의 실현 디 테 일 을 고려 하지 않 고 일부 API 인 터 페 이 스 를 통 해 디 스 플레이 장치 에 접근 할 수 있 도록 할 수 있다. 그러나 Framebuffer 자체 가 데 이 터 를 연산 하 는 능력 을 갖 추 지 못 해 일시 적 으로 물 을 보관 하 는 싱 크 대 에 비유 할 수 밖 에 없다.연못 의 물 은 바로 표 시 된 것 이다.CPU 는 연산 한 결 과 를 이 싱 크 대 에 놓 고 싱 크 대 는 결 과 를 모니터(보통 DMA 를 통 해 전송)로 흘 립 니 다.따라서 응용 프로그램 은 이 싱 크 대 를 읽 고 쓰 면 디 스 플레이 카드 를 조작 하 는 것 과 같 습 니 다.시스템 에서/dev/fb*에서 framebuffer 장 치 를 볼 수 있 습 니 다.다음 그림 은 framebuffer 운영 체 제 를 잘 묘사 하고 있 습 니 다.
프레임 버퍼 서브 시스템 의 차원 구조:
위의 그림 은 주로 아래 파일 에 있 습 니 다.
drivers/vedio/fbmem.c 이 파일 은 framebuffer 구현 의 핵심 으로 하드웨어 와 무관 합 니 다.
drivers/vedio/xxxfb.c 이 파일 은 주로 framebuffer 장치 구동 의 실현 입 니 다.예 를 들 어 s3c2410fb.c 는 framebuffer 장치 구동 을 실현 합 니 다.
fbmem.c 는 framebuffer 를 실현 하 는 핵심 으로 하드웨어 와 무관 합 니 다.다음 데이터 구 조 를 사 용 했 습 니 다.
struct fb_info *fb_info 이 데이터 구 조 는 framebuffer device 와 관련 된 일련의 정보 struct fb 를 묘사 합 니 다.ops *fb_ops 이 데이터 구 조 는 file 와 유사 한 framebuffer device 의 조작 함수 집합 을 묘사 합 니 다.operations,하지만 커 널 에 만 static const struct file 을 사용 합 니 다.operations fb_fops 이 데이터 구 조 는 파일 조작 함수 집합 으로 프로그램 이 장 치 를 열 때 사용 자 는 read,write,ioctl 등 struct fb 를 사용 할 수 있 습 니 다.var_screeninfo var 이 데이터 구 조 는 framebuffer device 디 스 플레이 특성 을 설명 하 며 변경 가능 한 struct fb 입 니 다.fix_screeninfo fix 이 데이터 구 조 는 framebuffer device 디 스 플레이 기능 을 저장 하 는 데 사 용 됩 니 다.고정 적 으로 변 하지 않 으 며 변경 할 수 없습니다.
구체 적 인 데이터 구조:
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 */
__u32 yoffset; /* resolution */
__u32 bits_per_pixel; /* guess what */
__u32 grayscale; /* != 0 Graylevels 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; /* (OBSOLETE) see fb_info.flags */
/* 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 rotate; /* angle we rotate counter clockwise */
__u32 reserved[5]; /* Reserved for future compatibility */
};
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; /* Indicate to driver which */
/* specific chip/card we have */
__u16 reserved[3]; /* Reserved for future compatibility */
};
struct fb_info {
int node;
int flags;
struct mutex lock; /* Lock for open/release/ioctl funcs */
struct fb_var_screeninfo var; /* Current var */
struct fb_fix_screeninfo fix; /* Current fix */
struct fb_monspecs monspecs; /* Current Monitor specs */
struct work_struct queue; /* Framebuffer event queue */
struct fb_pixmap pixmap; /* Image hardware mapper */
struct fb_pixmap sprite; /* Cursor hardware mapper */
struct fb_cmap cmap; /* Current cmap */
struct list_head modelist; /* mode list */
struct fb_videomode *mode; /* current mode */
...
struct fb_ops *fbops;
struct device *device; /* This is the parent */
struct device *dev; /* This is this fb device */
int class_flag; /* private sysfs flags */
#ifdef CONFIG_FB_TILEBLITTING
struct fb_tile_ops *tileops; /* Tile Blitting */
#endif
char __iomem *screen_base; /* Virtual address */
unsigned long screen_size; /* Amount of ioremapped VRAM or 0 */
void *pseudo_palette; /* Fake palette of 16 colors */
...
};
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);
/* For framebuffers with strange non linear layouts or that do not
* work with normal memory mapped access
*/
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);
/* checks var and eventually tweaks it to something supported,
* DO NOT MODIFY PAR */
int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);
/* set the video mode according to info->var */
int (*fb_set_par)(struct fb_info *info);
/* set color register */
int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,
unsigned blue, unsigned transp, struct fb_info *info);
/* set color registers in batch */
int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);
/* blank display */
int (*fb_blank)(int blank, struct fb_info *info);
/* pan display */
int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);
/* Draws a rectangle */
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);
/* Draws cursor */
int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);
/* Rotates the display */
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,
unsigned long arg);
/* Handle 32bit compat ioctl (optional) */
int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,
unsigned long arg);
/* perform fb specific mmap */
int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);
/* save current hardware state */
void (*fb_save_state)(struct fb_info *info);
/* restore saved state */
void (*fb_restore_state)(struct fb_info *info);
/* get capability given var */
void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,
struct fb_var_screeninfo *var);
};
static const struct file_operations fb_fops = {
.owner = THIS_MODULE,
.read = fb_read,
.write = fb_write,
.check_flags = my_check,
.unlocked_ioctl = fb_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = fb_compat_ioctl,
#endif
.mmap = fb_mmap,
.open = fb_open,
.release = fb_release,
#ifdef HAVE_ARCH_FB_UNMAPPED_AREA
.get_unmapped_area = get_fb_unmapped_area,
#endif
#ifdef CONFIG_FB_DEFERRED_IO
.fsync = fb_deferred_io_fsync,
#endif
};
framebuffer 장치 의 등록 과 로그아웃:
register_framebuffer(struct fb_info *fb_info);
unregister_framebuffer(struct fb_info *fb_info);
파일 에 따라 작 동 하 는 static const struct fileoperations fb_fops,응용 프로그램 이 프레임 버퍼 장 치 를 열 때 read,write,ioctl 을 사용 하여 장 치 를 직접 조작 할 수 있 습 니 다.
응용 루틴:
#include
#include
#include
#include
#include
#include
#include
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
static void fb_var_printf(struct fb_var_screeninfo tmp)
{
printf("fb_var_screeninfo:
");
printf("xres =%d, yres =%d, bits_per_pixel = %d
",tmp.xres,tmp.yres,tmp.bits_per_pixel);
printf("height=%d,width = %d
",tmp.height,tmp.width);
printf("xres_virtual =%d, yres_virtual =%d, xoffset=%d,yoffset=%d
",tmp.xres_virtual,tmp.yres_virtual,tmp.xoffset,tmp.yoffset);
return ;
}
int main(void)
{
int fbfd;
int fbsize;
unsigned char *fbbuf;
char buf[100];
int i,res,adc_data;
for (i=0; i<100; i++) buf[i] = 0xaa;
if ((fbfd = open("/dev/fb0", O_RDWR)) < 0) {
printf("open fb0 failed
");
return 1;
}
printf("fbfd = %d
", fbfd);
if ((res =ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))) { //
printf("bad vscreeninfo ioctl.error = %d
",res);
}
fb_var_printf(vinfo);
fbsize = vinfo.xres * vinfo.yres * (vinfo.bits_per_pixel/8); // (LCD ) ,
printf("fbisze: %d
",fbsize);
if ((fbbuf = mmap(0, fbsize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0)) == (void*) -1) // , ( )
{
printf("map video error.
");
}
for (i = 0; i< fbsize; i++) { // farmebuffer
*(fbbuf+i) = 0xaa; //
}
munmap(fbbuf, fbsize);
close(fbfd);
return 0;
}