Linux源代码包中/document/fb/framebuffer.txt有例如以下介绍:

The frame buffer device provides an abstraction for the graphics hardware. 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执行机制:

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      该数据结构描写叙述了一个framebuffer device的操作函数集合。相似file_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显示特性,是固定不变的,不能够更改

详细数据结构:

[cpp] 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. };
[cpp] 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. };
[cpp] view plain copy
  1. struct fb_info {
  2. int node;
  3. int flags;
  4. struct mutex lock;      /* Lock for open/release/ioctl funcs */
  5. struct fb_var_screeninfo var;   /* Current var */
  6. struct fb_fix_screeninfo fix;   /* Current fix */
  7. struct fb_monspecs monspecs;    /* Current Monitor specs */
  8. struct work_struct queue;   /* Framebuffer event queue */
  9. struct fb_pixmap pixmap;    /* Image hardware mapper */
  10. struct fb_pixmap sprite;    /* Cursor hardware mapper */
  11. struct fb_cmap cmap;        /* Current cmap */
  12. struct list_head modelist;      /* mode list */
  13. struct fb_videomode *mode;  /* current mode */
  14. ...
  15. struct fb_ops *fbops;
  16. struct device *device;      /* This is the parent */
  17. struct device *dev;     /* This is this fb device */
  18. int class_flag;                    /* private sysfs flags */
  19. #ifdef CONFIG_FB_TILEBLITTING
  20. struct fb_tile_ops *tileops;    /* Tile Blitting */
  21. #endif
  22. char __iomem *screen_base;  /* Virtual address */
  23. unsigned long screen_size;  /* Amount of ioremapped VRAM or 0 */
  24. void *pseudo_palette;       /* Fake palette of 16 colors */
  25. ...
  26. };
[cpp] view plain copy
  1. struct fb_ops {
  2. /* open/release and usage marking */
  3. struct module *owner;
  4. int (*fb_open)(struct fb_info *info, int user);
  5. int (*fb_release)(struct fb_info *info, int user);
  6. /* For framebuffers with strange non linear layouts or that do not
  7. * work with normal memory mapped access
  8. */
  9. ssize_t (*fb_read)(struct fb_info *info, char __user *buf,
  10. size_t count, loff_t *ppos);
  11. ssize_t (*fb_write)(struct fb_info *info, const char __user *buf,
  12. size_t count, loff_t *ppos);
  13. /* checks var and eventually tweaks it to something supported,
  14. * DO NOT MODIFY PAR */
  15. int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);
  16. /* set the video mode according to info->var */
  17. int (*fb_set_par)(struct fb_info *info);
  18. /* set color register */
  19. int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,
  20. unsigned blue, unsigned transp, struct fb_info *info);
  21. /* set color registers in batch */
  22. int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);
  23. /* blank display */
  24. int (*fb_blank)(int blank, struct fb_info *info);
  25. /* pan display */
  26. int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);
  27. /* Draws a rectangle */
  28. void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);
  29. /* Copy data from area to another */
  30. void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);
  31. /* Draws a image to the display */
  32. void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);
  33. /* Draws cursor */
  34. int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);
  35. /* Rotates the display */
  36. void (*fb_rotate)(struct fb_info *info, int angle);
  37. /* wait for blit idle, optional */
  38. int (*fb_sync)(struct fb_info *info);
  39. /* perform fb specific ioctl (optional) */
  40. int (*fb_ioctl)(struct fb_info *info, unsigned int cmd,
  41. unsigned long arg);
  42. /* Handle 32bit compat ioctl (optional) */
  43. int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,
  44. unsigned long arg);
  45. /* perform fb specific mmap */
  46. int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);
  47. /* save current hardware state */
  48. void (*fb_save_state)(struct fb_info *info);
  49. /* restore saved state */
  50. void (*fb_restore_state)(struct fb_info *info);
  51. /* get capability given var */
  52. void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,
  53. struct fb_var_screeninfo *var);
  54. };
[cpp] view plain copy
  1. static const struct file_operations fb_fops = {
  2. .owner =    THIS_MODULE,
  3. .read =     fb_read,
  4. .write =    fb_write,
  5. .check_flags = my_check,
  6. .unlocked_ioctl = fb_ioctl,
  7. #ifdef CONFIG_COMPAT
  8. .compat_ioctl = fb_compat_ioctl,
  9. #endif
  10. .mmap =     fb_mmap,
  11. .open =     fb_open,
  12. .release =  fb_release,
  13. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
  14. .get_unmapped_area = get_fb_unmapped_area,
  15. #endif
  16. #ifdef CONFIG_FB_DEFERRED_IO
  17. .fsync =    fb_deferred_io_fsync,
  18. #endif
  19. };

framebuffer设备的注冊与注销:

register_framebuffer(struct fb_info *fb_info);

unregister_framebuffer(struct fb_info *fb_info);

以下看看fb_ioctl 都做了什么?

static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
unsigned long arg)
{
struct fb_ops *fb;
struct fb_var_screeninfo var;
struct fb_fix_screeninfo fix;
struct fb_con2fbmap con2fb;
struct fb_cmap cmap_from;
struct fb_cmap_user cmap;
struct fb_event event;
void __user *argp = (void __user *)arg;
long ret = 0; switch (cmd) {
case FBIOGET_VSCREENINFO:
if (!lock_fb_info(info))
return -ENODEV;
var = info->var;
unlock_fb_info(info); ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
break;
case FBIOPUT_VSCREENINFO:
if (copy_from_user(&var, argp, sizeof(var)))
return -EFAULT;
if (!lock_fb_info(info))
return -ENODEV;
console_lock();
info->flags |= FBINFO_MISC_USEREVENT;
ret = fb_set_var(info, &var);
info->flags &= ~FBINFO_MISC_USEREVENT;
console_unlock();
unlock_fb_info(info);
if (!ret && copy_to_user(argp, &var, sizeof(var)))
ret = -EFAULT;
break;
case FBIOGET_FSCREENINFO:
if (!lock_fb_info(info))
return -ENODEV;
fix = info->fix;
unlock_fb_info(info); ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
break;
case FBIOPUTCMAP:
if (copy_from_user(&cmap, argp, sizeof(cmap)))
return -EFAULT;
ret = fb_set_user_cmap(&cmap, info);
break;
case FBIOGETCMAP:
if (copy_from_user(&cmap, argp, sizeof(cmap)))
return -EFAULT;
if (!lock_fb_info(info))
return -ENODEV;
cmap_from = info->cmap;
unlock_fb_info(info);
ret = fb_cmap_to_user(&cmap_from, &cmap);
break;
case FBIOPAN_DISPLAY:
if (copy_from_user(&var, argp, sizeof(var)))
return -EFAULT;
if (!lock_fb_info(info))
return -ENODEV;
console_lock();
ret = fb_pan_display(info, &var);
console_unlock();
unlock_fb_info(info);
if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
return -EFAULT;
break;
case FBIO_CURSOR:
ret = -EINVAL;
break;
case FBIOGET_CON2FBMAP:
if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
return -EFAULT;
if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
return -EINVAL;
con2fb.framebuffer = -1;
event.data = &con2fb;
if (!lock_fb_info(info))
return -ENODEV;
event.info = info;
fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
unlock_fb_info(info);
ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
break;
case FBIOPUT_CON2FBMAP:
if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
return -EFAULT;
if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
return -EINVAL;
if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
return -EINVAL;
if (!registered_fb[con2fb.framebuffer])
request_module("fb%d", con2fb.framebuffer);
if (!registered_fb[con2fb.framebuffer]) {
ret = -EINVAL;
break;
}
event.data = &con2fb;
if (!lock_fb_info(info))
return -ENODEV;
event.info = info;
ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP, &event);
unlock_fb_info(info);
break;
case FBIOBLANK:
if (!lock_fb_info(info))
return -ENODEV;
console_lock();
info->flags |= FBINFO_MISC_USEREVENT;
ret = fb_blank(info, arg);
info->flags &= ~FBINFO_MISC_USEREVENT;
console_unlock();
unlock_fb_info(info);
break;
default:
if (!lock_fb_info(info))
return -ENODEV;
fb = info->fbops;
if (fb->fb_ioctl)
ret = fb->fb_ioctl(info, cmd, arg);
else
ret = -ENOTTY;
unlock_fb_info(info);
}
return ret;
} static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct fb_info *info = file_fb_info(file); printk(\nfb_ioctl mem\n);
if (!info)
return -ENODEV;
return do_fb_ioctl(info, cmd, arg);
}

依据文件操作的static const struct file_operations fb_fops,应用程序在打开一个framebuffer设备时。能够使用read,write,ioctl来直接操作设备。

应用例程:

[cpp] view plain copy
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/ioctl.h>
  5. #include <fcntl.h>
  6. #include <linux/fb.h>
  7. #include <sys/mman.h>
  8. struct fb_var_screeninfo vinfo;
  9. struct fb_fix_screeninfo finfo;
  10. static void fb_var_printf(struct fb_var_screeninfo tmp)
  11. {
  12. printf("fb_var_screeninfo:\n");
  13. printf("xres =%d, yres =%d, bits_per_pixel = %d\n",tmp.xres,tmp.yres,tmp.bits_per_pixel);
  14. printf("height=%d,width = %d\n",tmp.height,tmp.width);
  15. printf("xres_virtual =%d, yres_virtual =%d, xoffset=%d,yoffset=%d\n",tmp.xres_virtual,tmp.yres_virtual,tmp.xoffset,tmp.yoffset);
  16. return ;
  17. }
  18. int main(void)
  19. {
  20. int fbfd;
  21. int fbsize;
  22. unsigned char *fbbuf;
  23. char buf[100];
  24. int i,res,adc_data;
  25. for (i=0; i<100; i++) buf[i] = 0xaa;
  26. if ((fbfd = open("/dev/fb0", O_RDWR)) < 0) {
  27. printf("open fb0 failed\n");
  28. return 1;
  29. }
  30. printf("fbfd = %d\n", fbfd);
  31. if ((res =ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))) { //获取设备显示特性信息
  32. printf("bad vscreeninfo ioctl.error = %d\n",res);
  33. }
  34. fb_var_printf(vinfo);
  35. fbsize = vinfo.xres * vinfo.yres * (vinfo.bits_per_pixel/8); //计算显卡(LCD控制器)显存大小。也就是一整屏共占多少个字节
  36. printf("fbisze: %d\n",fbsize);
  37. if ((fbbuf = mmap(0, fbsize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0)) == (void*) -1)  //映射显卡设备的内存到用户控件,使得用户直接訪问设备内存(显存)
  38. {
  39. printf("map video error.\n");
  40. }
  41. for (i = 0; i< fbsize; i++) {  //填充farmebuffer缓冲区
  42. *(fbbuf+i) = 0xaa;  //颜色信息
  43. }
  44. munmap(fbbuf, fbsize);
  45. close(fbfd);
  46. return 0;
  47. }

Linux内核修炼之framebuffer分析的更多相关文章

  1. Linux内核--网络栈实现分析(十一)--驱动程序层(下)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7555870 更多请查看专栏,地 ...

  2. Linux内核--网络栈实现分析(七)--数据包的传递过程(下)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7545855 更多请查看专栏,地 ...

  3. linux内核SPI总线驱动分析(一)(转)

    linux内核SPI总线驱动分析(一)(转) 下面有两个大的模块: 一个是SPI总线驱动的分析            (研究了具体实现的过程) 另一个是SPI总线驱动的编写(不用研究具体的实现过程) ...

  4. Linux内核--网络栈实现分析(二)--数据包的传递过程--转

    转载地址http://blog.csdn.net/yming0221/article/details/7492423 作者:闫明 本文分析基于Linux Kernel 1.2.13 注:标题中的”(上 ...

  5. Linux内核态抢占机制分析(转)

    Linux内核态抢占机制分析  http://blog.sina.com.cn/s/blog_502c8cc401012pxj.html 摘 要]本文首先介绍非抢占式内核(Non-Preemptive ...

  6. linux内核中链表代码分析---list.h头文件分析(一)【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637596.html linux内核中链表代码分析---list.h头文件分析(一) 16年2月27日17 ...

  7. linux内核中链表代码分析---list.h头文件分析(二)【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637598.html linux内核中链表代码分析---list.h头文件分析(二) 16年2月28日16 ...

  8. Linux内核哈希表分析与应用

        目录(?)[+]   Linux内核哈希表分析与应用 Author:tiger-johnTime:2012-12-20mail:jibo.tiger@gmail.comBlog:http:// ...

  9. Linux内核抢占实现机制分析【转】

    Linux内核抢占实现机制分析 转自:http://blog.chinaunix.net/uid-24227137-id-3050754.html [摘要]本文详解了Linux内核抢占实现机制.首先介 ...

随机推荐

  1. 050 sqoop的使用

    一:导入  mysql-->hdfs 1.准备 2.导入数据 可以看到在跑yarn. 3.在HDFS上看结果 默认的地址:hdfs的家目录. 4.在HDFS上指定目录 5.指定map的个数,相同 ...

  2. ModuleNotFoundError: No module named 'win32api'

    原因:缺少了win32解决: pip install pypiwin32

  3. 基于C++11实现线程池的工作原理

    目录 基于C++11实现线程池的工作原理. 简介 线程池的组成 1.线程池管理器 2.工作线程 3.任务接口, 4.任务队列 线程池工作的四种情况. 1.主程序当前没有任务要执行,线程池中的任务队列为 ...

  4. 由自定义事件到vue数据响应

    前言 除了大家经常提到的自定义事件之外,浏览器本身也支持我们自定义事件,我们常说的自定义事件一般用于项目中的一些通知机制.最近正好看到了这部分,就一起看了下自定义事件不同的实现,以及vue数据响应的基 ...

  5. Solidworks实例学习

    1. 扫描绘制 2. 拉伸切除1 3. 拉伸切除2 4. 旋转切除1 5. 旋转切除2 6. 基准面构建 7. 扫描薄壁 8. 放样1 9. 放样2 10. 放样3  

  6. 在现实面前,IT从业者的无奈

    话题:在中国,有多少程序员干到40了? 作者:匿名用户 链接:https://www.zhihu.com/question/33953081/answer/349839986 学历低,计算机理论不高, ...

  7. [ Visual Studio ] MSDN

    在 Visual Studio 中创建自定义项目和项模板 编写和重构代码 (C++) C# 指南 C#最新版本 使用 MSBuild 如何:管理编辑器模式,进入全屏模式编写代码 自定义代码折叠

  8. Java学习(一)--面向对象(一)

    面向对象的思想一直指导者我们软件的分析.设计与开发.java语言是一种面向对象的语言.在学习java之前,先回想一以下向过程和面向对象. 一面向过程 面向过程主张按功能来划分系统需求.每一个功能都负责 ...

  9. cordova 开发笔记

    1.安装 Node.js Cordova需要Node.js环境,访问https://nodejs.org 下载安装, LTS版本即可,不要最新版. 2.安装 Cordova 执行下述命令把Cordov ...

  10. iOS:用Block写一个链式编程

    一.介绍 链式编程是一个比较新颖的编程方式,简单直观,用起来也比较舒服.目前比较有名的Mansory和BabyBlueTooth就是使用链式编程写的第三方框架. 二.写法 链式编程写法不同于传统方式, ...