基于 framebuffer 在屏幕上画一个点
作者: liupeng
日期: 2008-09-15

简介

本文介绍了一个通过 framebuffer 在屏幕上绘制一个点的程序。通过该程序,读者 可以看到操作 framebuffer 在屏幕上绘制的基本流程和方法。本程序使用的屏幕是 32 位色。

程序主要流程

  1. 打开/dev/fb 设备文件。
  2. 用 ioctrl 操作取得当前显示屏幕的参数,如屏幕分辨率,每个像素点的比特数。根据屏幕参数可计算屏幕缓冲区的大小。
  3. 将屏幕缓冲区映射到用户空间。
  4. 映射后就可以直接读写屏幕缓冲区,进行绘图和图片显示了。

屏幕缓存映射

对于 framebuffer 设备,通过 mmap 映射操作,可将屏幕缓冲区的物理地址映 射到用户空间的一段虚拟地址中,之后用户就可以通过读写这段虚拟地址访问屏 幕缓冲区,在屏幕上绘图了。

/* Figure out the size of the screen in bytes */
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

/* Map the device to memory */
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);

在屏幕上绘制一个点

通过如下代码计算点 (x,y) 在缓存中的位置:

    /* Figure out where in memory to put the pixel */
    location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                    (y+vinfo.yoffset) * finfo.line_length;

由于屏幕是 32 位色,每个像素由四个字节组成,分别保存该像素的 RBGA 值。

下面的代码将蓝、绿、红、阿尔法值依次写入连续的四个字节。

    *(fbp + location) = 0; /* Some blue */
    *(fbp + location + 1) = 0; /* A little green */
    *(fbp + location + 2) = 255; /* A lot of red */
    *(fbp + location + 3) = 0; /* No transparency */

代码示例

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>

int main () {
    int fbfd = 0;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long int screensize = 0;
    char *fbp = 0;
    int x = 0, y = 0;
    long int location = 0;

    /* Open the file for reading and writing */
    fbfd = open("/dev/fb0", O_RDWR);
    if (fbfd < 0) {
        printf("Error: cannot open framebuffer device.\n");
        return -1;
    }
    printf("The framebuffer device was opened successfully.\n");

    /* Get fixed screen information */
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
        printf("Error reading fixed information.\n");
        return -1;
    }

    /* Get variable screen information */
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
        printf("Error reading variable information.\n");
        return -1;
    }

    /* Figure out the size of the screen in bytes */
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

    /* Map the device to memory */
    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
                                 fbfd, 0);

    if ((int)fbp == -1) {
        printf("Error: failed to map framebuffer device to memory.\n");
        return -1;
    }
    printf("The framebuffer device was mapped to memory successfully.\n");

    x = 100; y = 100; /* Where we are going to put the pixel */

    /* Figure out where in memory to put the pixel */
    location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                    (y+vinfo.yoffset) * finfo.line_length;

    /* set RGBA value to the value */
    *(fbp + location) = 0; /* Some blue */
    *(fbp + location + 1) = 0; /* A little green */
    *(fbp + location + 2) = 255; /* A lot of red */
    *(fbp + location + 3) = 0; /* No transparency */

    munmap(fbp, screensize);
    close(fbfd);

    return 0;

}

由于是对线性存储空间的读写,所以代码有点不清晰,不易理解。但是有了这个 基本的代码实现,我们可以很容易写一些DrawPoint之类的函数去包装一下低层 的对线性存储空间的读写。有了画点的程序,再写出画线画圆的函数就不是非常 困难了。后面的文章将介绍对基本绘图函数的封装。