手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Ubuntu Framebuffer学习笔记

[日期:2011-07-28] 来源:Linux社区  作者:weimingtom [字体:大 中 小]

二、Framebuffer的应用开发

1. 基于/lib/fb*设备和mmap

这种方法灵活性差,开发比较费时。

这里有个示例代码,代码如下:

  1. #include <stdlib.h>   
  2. #include <unistd.h>   
  3. #include <stdio.h>   
  4. #include <fcntl.h>   
  5. #include <linux/fb.h>   
  6. #include <sys/mman.h>   
  7.   
  8. int main()   
  9. {   
  10.     int fbfd = 0;   
  11.     struct fb_var_screeninfo vinfo;   
  12.     struct fb_fix_screeninfo finfo;   
  13.     long int screensize = 0;   
  14.     char *fbp = 0;   
  15.     int x = 0, y = 0;   
  16.     long int location = 0;   
  17.     fbfd = open("/dev/fb0", O_RDWR);   
  18.     if (!fbfd) {   
  19.         printf("Error: cannot open framebuffer device.\n");   
  20.         exit(1);   
  21.     }   
  22.     printf("The framebuffer device was opened successfully.\n");   
  23.     if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {   
  24.         printf("Error reading fixed information.\n");   
  25.         exit(2);   
  26.     }   
  27.     if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {   
  28.         printf("Error reading variable information.\n");   
  29.         exit(3);   
  30.     }   
  31.     printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );   
  32.     screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;   
  33.     fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,   
  34.                        fbfd, 0);   
  35.     if ((int)fbp == -1) {   
  36.         printf("Error: failed to map framebuffer device to memory.\n");   
  37.         exit(4);   
  38.     }   
  39.     printf("The framebuffer device was mapped to memory successfully.\n");   
  40.     x = 100;    
  41.     y = 100;   
  42.     for ( y = 100; y < 300; y++ )   
  43.         for ( x = 100; x < 300; x++ ) {   
  44.             location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +   
  45.                        (y+vinfo.yoffset) * finfo.line_length;   
  46.             if ( vinfo.bits_per_pixel == 32 ) {   
  47.                 *(fbp + location) = 100;     
  48.                 *(fbp + location + 1) = 15+(x-100)/2;   
  49.                 *(fbp + location + 2) = 200-(y-100)/5;    
  50.                 *(fbp + location + 3) = 0;   
  51.             } else  {   
  52.                 int b = 10;   
  53.                 int g = (x-100)/6;   
  54.                 int r = 31-(y-100)/16;   
  55.                 unsigned short int t = r<<11 | g << 5 | b;   
  56.                 *((unsigned short int*)(fbp + location)) = t;   
  57.             }   
  58.         }   
  59.     munmap(fbp, screensize);   
  60.     close(fbfd);   
  61.     return 0;   
  62. }  

(已经在Ubuntu和qemu上测试过)

 

2. 基于DirectFB

DirectFB好像可以加快Framebuffer的绘画速度。

官方有5个示例代码

http://directfb.org/index.php?path=Development%2FTutorials

DirectFB有支持字体、图片和输入插件,

不过在编译前需要事先安装相关的开发库。

(已经在Ubuntu和qemu上测试过)

3. 基于SDL

SDL支持Framebuffer,因为它底层使用了DirectFB。

Ubuntu的SDL可以在Framebuffer下使用,如果自己编译SDL的代码,

需要事先编译DirectFB

(已经在Ubuntu和qemu上测试过)

4. 基于GTK+2

项目主页在:

http://www.gtk.org/

GTK+2通过Cairo库的DirectFB后端支持Framebuffer。

而且gtk库在编译时需要填加特定参数

./configure --prefix=$PREFIX --with-gdktarget=directfb --without-x

然后进行编译。

不过GTK+2的编译比较麻烦,而且directfb上GTK+2的开发代码尚不稳定。

测试的可执行文件在/bin/gtk-demo

(在Ubuntu上跑似乎有问题——不知道怎么拖动窗口和退出;未在qemu上测试)

5. 基于Qt

下载在

ftp://ftp.qt.nokia.com/qt/source/

一般需要自己编译

$ tar xzf qt-everywhere-opensource-src-4.8.0-tp.tar.gz

$ cd qt-everywhere-opensource-src-4.8.0-tp/

$ ./configure -shared -static -opensource -embedded generic

虽然-embedded这个参数没有出现在./configure --help里,

但它在这里是有效的。这里不需要添加--prefix参数

$ sudo make -j 4

$ sudo make install

默认装在/usr/local/Trolltech目录下。

示例代码和编译的二进制文件在demos/embedded目录下。

进入framebuffer下的文本模式,

然后运行sudo ./xxx -qws

运行时如果提示:

Qt/Embedded data directory is not owned by user 0:/tmp/qtembedded-0

可以运行:

sudo chown root:root /tmp/qtembedded-0

如果要在qemu上跑,需要复制/lib/fonts目录下的字体

(可以在Ubuntu上跑,在qemu上跑似乎有点问题——未解决)

linux
相关资讯       FrameBuffer  Ubuntu教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款