/LGC图形渲染/基于 framebuffer 在屏幕上画一个点

400 篇文章 3 订阅
147 篇文章 0 订阅
基于 framebuffer 在屏幕上画一个点
作者: liuuupen
日期: 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之类的函数去包装一下低层 的对线性存储空间的读写。有了画点的程序,再写出画线画圆的函数就不是非常 困难了。后面的文章将介绍对基本绘图函数的封装。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值