(二)DirectFB 入门

2.1 DirectFB的整体框架

DirectFB必须通过Linux下Framebuffer驱动来访问硬件设备,它在Framebuffer的基础上提供了图形设备的加速、输入设备处理提取、透明窗口和多重显示层的功能。另外DirectFB设计之初就充分考虑了嵌入式系统的固有特性,体积小巧,资源消耗低,图1是DirectFB的整体框架。

 

 

2.2  DirectFB 开发环境搭建

环境:unbunu10.04  DirectFB 1.6  flux1.4.3  root 用户身份登陆

 

2.2.1 开启Framebuffer

要使用DirectFB,操作系统必须开启Framebuffer ,

 

1.获取硬件支持的framebuffer模式,同时设置引导参数

 

$ hwinfo --framebuffer

  Mode 0x0317: 1024x768 (+2048), 16 bits

我这里挑选了一个比较常见的mode,然后追加到kernel的引导参数当中去。

$ vim /etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash vga=0x0317"

 

2.在initrd当中添加对应模块的支持

$ vim /etc/modprobe.d/blacklist-framebuffer.conf

 

注释这一行

#blacklist vesafb

 

然后往initrd当中添加对应模块

$ vim /etc/initramfs-tools/modules

 

加入如下内容

fbcon

vesafb

vga16fb

 

3.最后更新grub2和initrd,重新引导即可

$ update-grub2

$ update-initramfs -u

 

2.2.2 编译安装flux1.4.3 

解压源码包 按顺序执行以下命令

$ ./autogen.sh     生成configure

$ ./configure --prefix=/usr      生成makefile 文件

$ make

$ make install

 

2.2.3 安装DirectFB 

解压源码包 按顺序执行以下命令

$ ./autogen.sh     

$ ./configure --prefix=/usr  --enable-x11=yes

$ make

$ make install

 

./configure  该步骤可能出错 原因是没有安装libtool ,PNG ,JPEG FreeType2 之类的。

$ apt-get install libtool 

$ apt-get install libpng12-dev

$ apt-get install libjpeg62-dev

$ apt-get install  libfreetype6-dev

$ ./configure --prefix=/usr  --enable-x11=yes  

由于DirectFB默认是关闭X11的,所以在这一步./configure的时候要将x11开启

 

2.2.4 其它

如果DirectFB 需要多处理器支持,还必须安装 linux-fusion 9.0.1,安装方法如安装flux。

 

2.3 一个例子

 

编译源码

g++ -I/usr/include/directfb -ldirectfb -lpthread -ldl -lz test.cpp -o test

提示可能不成功

原因是没用安装 x11开发包。

$ apt-get install  xorg-dev

然后重新编译,并且运行./test 成功,屏幕中出现一条横线。

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <directfb.h>
  
//这是最上层的接口,所有的函数入口均由它(IDirectFB)而来
static IDirectFB *dfb = NULL;
  
//主平面也就是“屏幕”了。在交互层使用DFSCL_FULLSCREEN,它是主层平面。
static IDirectFBSurface *primary = NULL;  
//这里存储主平面的高和宽,从而为其它的操作提供支持
static int screen_width  = 0;
static int screen_height = 0;
static IDirectFBFont * font = NULL;

//用以检测错误的宏定义,用来检测大部分的函数的返回值是否正常。只适合用在小的测试程序
#define DFBCHECK(x...)  
{                
       DFBResult err = x;   
        
         if (err != DFB_OK)  
         {  
                fprintf( stderr, "%s <%d>:nt", __FILE__, __LINE__ ); 
               DirectFBErrorFatal( #x, err );  
        } 
}
int main (int argc, char **argv)
{
    //为了创建一个平面,需要定义一个平面描述子(surface description)
    DFBSurfaceDescription dsc;
    DFBFontDescription font_dsc;
     
    //初始化
     DFBCHECK (DirectFBInit (&argc, &argv));
     DFBCHECK (DirectFBCreate (&dfb));
     DFBCHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
  
   //设定dsc的一些属性,现在可以不用关心
     dsc.flags = DSDESC_CAPS;
      dsc.caps  = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
 
       //使用我们设定的dsc创建主平面(primary)
       DFBCHECK (dfb->CreateSurface( dfb, &dsc, &primary ));
       //得到主平面的宽与高
       DFBCHECK (primary->GetSize (primary, &screen_width, &screen_height));
 
        //设置字体的高度是固定的
        font_dsc.flags = DFDESC_HEIGHT;
        font_dsc.height = screen_height/16;
 
    //set Font 第二个参数是字体的路径,测试的话可以去下载一个放到指定目录下,然后修改参数二即可
    dfb->CreateFont( dfb, "/home/jia/DirectFB-examples-1.6.0/data/decker.ttf", &font_dsc, &font );
    primary->SetFont(primary, font);
         
    //通过画一个和主平面同等大小的矩形来清空主平面;默认颜色为黑色
    primary->FillRectangle (primary, 1, 1, screen_width, screen_height);
    //为了能显示画出来的线,先设置一下线的颜色,线的位置在屏幕的中间
    primary->SetColor (primary, 0x80, 0x80, 0xff, 0xff);
    primary->DrawLine (primary,0, screen_height / 2,screen_width - 1, screen_height / 2);
    //写hello 在屏幕上   
     primary->DrawString(primary, "hello world", -1,0,font_dsc.height*1,  DSTF_LEFT);
 
     DFBCHECK (primary->Flip (primary, NULL, 0));  //去刷新屏幕
     sleep (5);  //等待5秒后,程序自动退出
     primary->Release( primary );
       dfb->Release( dfb );
       return 0;
}
原文地址:https://www.cnblogs.com/wolfrickwang/p/3516950.html