原子学习笔记7——FrameBuffer 应用编程

Frame 是帧的意思,buffer 是缓冲的意思,所以 Framebuffer 就是帧缓冲,这意味着 Framebuffer 就是一块内存,里面保存着一帧图像。
应用程序通过对 LCD 设备节点/dev/fb0(假设 LCD 对应的设备节点是/dev/fb0)进行 I/O 操作即可实现对 LCD 的显示控制。
在应用程序中,操作/dev/fbX 的一般步骤如下:
①、首先打开/dev/fbX 设备文件。
②、使用 ioctl()函数获取到当前显示设备的参数信息,譬如屏幕的分辨率大小、像素格式,根据屏幕参数计算显示缓冲区的大小。
③、通过存储映射 I/O 方式将屏幕的显示缓冲区映射到用户空间(mmap)。
④、映射成功后就可以直接读写屏幕的显示缓冲区,进行绘图或图片显示等操作了。
⑤、完成显示后,调用 munmap()取消映射、并调用 close()关闭设备文件。

1、获取 LCD 屏幕的参数信息

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
int main(int argc, char *argv[])
{
    struct fb_fix_screeninfo fb_fix;
    struct fb_var_screeninfo fb_var;
    int fd;
    // /* 打开 framebuffer 设备 
    if (0 > (fd = open("/dev/fb0", O_WRONLY))) {
        perror("open error");
        exit(-1);
    }
    // /* 获取参数信息 
    ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
    ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);
    printf("分辨率: %d*%d\n"
            "像素深度 bpp: %d\n"
            "一行的字节数: %d\n"
            "像素格式: R<%d %d> G<%d %d> B<%d %d>\n",
            fb_var.xres, fb_var.yres, fb_var.bits_per_pixel,
            fb_fix.line_length,
            fb_var.red.offset, fb_var.red.length,
            fb_var.green.offset, fb_var.green.length,
            fb_var.blue.offset, fb_var.blue.length);
    // /* 关闭设备文件退出程序 
    close(fd);
    exit(0);
}

可以看出fb_fix_screeninfo、fb_var_screeninfo结构体与FBIOGET_VSCREENINFO、FBIOGET_FSCREENINFO通过ioctl()函数显示出设备参数信息。
开发板上使用的是7 寸 1024*600的显示屏
在这里插入图片描述

2、LCD 基本操作

编写应用程序,在 LCD 上实现画点(俗称打点)、画线、画矩形等基本 LCD 操作。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#define argb8888_to_rgb565(color) ({ \
    unsigned int temp = (color); \
    ((temp & 0xF80000UL) >> 8) | \
    ((temp & 0xFC00UL) >> 5) | \
    ((temp & 0xF8UL) >> 3); \
 })
static int width; //LCD X 分辨率
static int height; //LCD Y 分辨率
static unsigned short *screen_base = NULL; //映射后的显存基地址
/********************************************************************
* 函数名称: lcd_draw_point
* 功能描述: 打点
* 输入参数: x, y, color
* 返 回 值: 无
********************************************************************/
static void lcd_draw_point(unsigned int x, unsigned int y, unsigned int color)
{
    unsigned short rgb565_color = argb8888_to_rgb565(color);//得到 RGB565 颜色值
    /* 对传入参数的校验 */
    if (x >= width)
        x = width - 1;
    if (y >= height)
        y = height - 1;
    /* 填充颜色 */
    screen_base[y * width + x] = rgb565_color;
}
/********************************************************************
* 函数名称: lcd_draw_line
* 功能描述: 画线(水平或垂直线)
* 输入参数: x, y, dir, length, color
* 返 回 值: 无
********************************************************************/
static void lcd_draw_line(unsigned int x, unsigned int y, int dir,
 unsigned int length, unsigned int color)
{
    unsigned short rgb565_color = argb8888_to_rgb565(color);//得到 RGB565 颜色值
    unsigned int end;
    unsigned long temp;
    /* 对传入参数的校验 */
    if (x >= width)
        x = width - 1;
    if (y >= height)
        y = height - 1;
    /* 填充颜色 */
    temp = y * width + x;//定位到起点
    if (dir) { //水平线
        end = x + length - 1;
        if (end >= width)
            end = width - 1;
        for ( ; x <= end; x++, temp++)
            screen_base[temp] = rgb565_color;
    }
    else { //垂直线
        end = y + length - 1;
        if (end >= height)
            end = height - 1;
        for ( ; y <= end; y++, temp += width)
            screen_base[temp] = rgb565_color;
    }
}
/********************************************************************
* 函数名称: lcd_draw_rectangle
* 功能描述: 画矩形
* 输入参数: start_x, end_x, start_y, end_y, color
* 返 回 值: 无
********************************************************************/
static void lcd_draw_rectangle(unsigned int start_x, unsigned int end_x,
 unsigned int start_y, unsigned int end_y,
 unsigned int color)
{
    int x_len = end_x - start_x + 1;
    int y_len = end_y - start_y - 1;
    lcd_draw_line(start_x, start_y, 1, x_len, color);//上边
    lcd_draw_line(start_x, end_y, 1, x_len, color); //下边
    lcd_draw_line(start_x, start_y + 1, 0, y_len, color);//左边
    lcd_draw_line(end_x, start_y + 1, 0, y_len, color);//右边
}
/********************************************************************
* 函数名称: lcd_fill
* 功能描述: 将一个矩形区域填充为参数 color 所指定的颜色
* 输入参数: start_x, end_x, start_y, end_y, color
* 返 回 值: 无
********************************************************************/
static void lcd_fill(unsigned int start_x, unsigned int end_x,
 unsigned int start_y, unsigned int end_y,
 unsigned int color)
{
    unsigned short rgb565_color = argb8888_to_rgb565(color);//得到 RGB565 颜色值
    unsigned long temp;
    unsigned int x;
    /* 对传入参数的校验 */
    if (end_x >= width)
        end_x = width - 1;
    if (end_y >= height)
        end_y = height - 1;
    /* 填充颜色 */
    temp = start_y * width; //定位到起点行首
    for ( ; start_y <= end_y; start_y++, temp+=width) {
        for (x = start_x; x <= end_x; x++)
            screen_base[temp + x] = rgb565_color;
    }
}
int main(int argc, char *argv[])
{
    struct fb_fix_screeninfo fb_fix;
    struct fb_var_screeninfo fb_var;
    unsigned int screen_size;
    int fd;
    /* 打开 framebuffer 设备 */
    if (0 > (fd = open("/dev/fb0", O_RDWR))) {
        perror("open error");
        exit(EXIT_FAILURE);
    }
    /* 获取参数信息 */
    ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
    ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);
    screen_size = fb_fix.line_length * fb_var.yres;//一帧数据的数据量大小
    width = fb_var.xres;
    height = fb_var.yres;
    /* 将显示缓冲区映射到进程地址空间 */
    screen_base = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, fd, 0);
    if (MAP_FAILED == (void *)screen_base) {
        perror("mmap error");
        close(fd);
        exit(EXIT_FAILURE);
    }
    /* 画正方形方块 */
    int w = height * 0.25;//方块的宽度为 1/4 屏幕高度
    lcd_fill(0, width-1, 0, height-1, 0x0); //清屏(屏幕显示黑色)
    lcd_fill(0, w, 0, w, 0xFF0000); //红色方块
    lcd_fill(width-w, width-1, 0, w, 0xFF00); //绿色方块
    lcd_fill(0, w, height-w, height-1, 0xFF); //蓝色方块
    lcd_fill(width-w, width-1, height-w, height-1, 0xFFFF00);//黄色方块
    /* 画线: 十字交叉线 */
    lcd_draw_line(0, height * 0.5, 1, width, 0xFFFFFF);//白色线
    lcd_draw_line(width * 0.5, 0, 0, height, 0xFFFFFF);//白色线
    /* 画矩形 */
    unsigned int s_x, s_y, e_x, e_y;
    s_x = 0.25 * width;
    s_y = w;
    e_x = width - s_x;
    e_y = height - s_y;
    for ( ; (s_x <= e_x) && (s_y <= e_y);s_x+=5, s_y+=5, e_x-=5, e_y-=5)
        lcd_draw_rectangle(s_x, e_x, s_y, e_y, 0xFFFFFF);
    /* 退出 */
    munmap(screen_base, screen_size); //取消映射
    close(fd); //关闭文件
    exit(EXIT_SUCCESS); //退出进程
}
  • 首先调用 open()打开 LCD 设备文件得到文件描述符 fd;
  • 接着使用 ioctl 函数获取 LCD 的可变参数信息和固定参数信息,通过得到的信息计算 LCD 显存大小、得到 LCD 屏幕的分辨率,ALPHA I.MX6U 开发板出厂系统将 LCD 实现为一个 RGB565 显示设备,所以程序中自定义的 4 个函数在操作 LCD 像素点时、都是以 RGB565的格式写入颜色值。
  • 接着使用 mmap 建立映射;
  • 映射成功之后就可以在应用层直接操作 LCD 显存了,调用自定义的函数在 LCD 上画线、画矩形、画方块;
  • 操作完成之后,调用 munmap 取消映射,调用 close 关闭 LCD 设备文件,退出程序。

3、显示 BMP 图片

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
/**** BMP 文件头数据结构 ****/
typedef struct {
    unsigned char type[2]; //文件类型
    unsigned int size; //文件大小
    unsigned short reserved1; //保留字段 1
    unsigned short reserved2; //保留字段 2
    unsigned int offset; //到位图数据的偏移量
} __attribute__ ((packed)) bmp_file_header;
/**** 位图信息头数据结构 ****/
typedef struct {
    unsigned int size; //位图信息头大小
    int width; //图像宽度
    int height; //图像高度
    unsigned short planes; //位面数
    unsigned short bpp; //像素深度
    unsigned int compression; //压缩方式
    unsigned int image_size; //图像大小
    int x_pels_per_meter; //像素/米
    int y_pels_per_meter; //像素/米
    unsigned int clr_used;
    unsigned int clr_omportant;
} __attribute__ ((packed)) bmp_info_header;
/**** 静态全局变量 ****/
static int width; //LCD X 分辨率
static int height; //LCD Y 分辨率
static unsigned short *screen_base = NULL; //映射后的显存基地址
static unsigned long line_length; //LCD 一行的长度(字节为单位)
/********************************************************************
* 函数名称: show_bmp_image
* 功能描述: 在 LCD 上显示指定的 BMP 图片
* 输入参数: 文件路径
* 返 回 值: 成功返回 0, 失败返回-1
********************************************************************/
static int show_bmp_image(const char *path)
{
    bmp_file_header file_h;
    bmp_info_header info_h;
    unsigned short *line_buf = NULL; //行缓冲区
    unsigned long line_bytes; //BMP 图像一行的字节的大小
    unsigned int min_h, min_bytes;
    int fd = -1;
    int j;
    /* 打开文件 */
    if (0 > (fd = open(path, O_RDONLY))) {
        perror("open error");
        return -1;
    }
    /* 读取 BMP 文件头 */
    if (sizeof(bmp_file_header) != read(fd, &file_h, sizeof(bmp_file_header))) {
        perror("read error");
        close(fd);
        return -1;
    }
    if (0 != memcmp(file_h.type, "BM", 2)) {
        fprintf(stderr, "it's not a BMP file\n");
        close(fd);
        return -1;
    }
    /* 读取位图信息头 */
    if (sizeof(bmp_info_header) != read(fd, &info_h, sizeof(bmp_info_header))) {
        perror("read error");
        close(fd);
        return -1;
    }
    /* 打印信息 */
    printf("文件大小: %d\n"
        "位图数据的偏移量: %d\n"
        "位图信息头大小: %d\n"
        "图像分辨率: %d*%d\n"
        "像素深度: %d\n", file_h.size, file_h.offset,
        info_h.size, info_h.width, info_h.height,
        info_h.bpp);
    /* 将文件读写位置移动到图像数据开始处 */
    if (-1 == lseek(fd, file_h.offset, SEEK_SET)) {
        perror("lseek error");
        close(fd);
        return -1;
    }
    /* 申请一个 buf、暂存 bmp 图像的一行数据 */
    line_bytes = info_h.width * info_h.bpp / 8;
    line_buf = malloc(line_bytes);
    if (NULL == line_buf) {
        fprintf(stderr, "malloc error\n");
        close(fd);
        return -1;
    }
    if (line_length > line_bytes)
        min_bytes = line_bytes;
    else
        min_bytes = line_length;
    /**** 读取图像数据显示到 LCD ****/
    /*******************************************
     * 为了软件处理上方便,这个示例代码便不去做兼容性设计了
     * 如果你想做兼容, 可能需要判断传入的 BMP 图像是 565 还是 888
     * 如何判断呢?文档里边说的很清楚了
     * 我们默认传入的 bmp 图像是 RGB565 格式
     *******************************************/
    if (0 < info_h.height) {//倒向位图
        if (info_h.height > height) {
            min_h = height;
            lseek(fd, (info_h.height - height) * line_bytes, SEEK_CUR);
            screen_base += width * (height - 1); //定位到屏幕左下角位置
        }
        else {
            min_h = info_h.height;
            screen_base += width * (info_h.height - 1); //定位到....不知怎么描述 懂的人自然懂!
        }
        for (j = min_h; j > 0; screen_base -= width, j--) {
            read(fd, line_buf, line_bytes); //读取出图像数据
            memcpy(screen_base, line_buf, min_bytes);//刷入 LCD 显存
        }
    }
    else { //正向位图
        int temp = 0 - info_h.height; //负数转成正数
        if (temp > height)
            min_h = height;
        else
            min_h = temp;
        for (j = 0; j < min_h; j++, screen_base += width) {
            read(fd, line_buf, line_bytes);
            memcpy(screen_base, line_buf, min_bytes);
        }
    }
    /* 关闭文件、函数返回 */
    close(fd);
    free(line_buf);
    return 0;
}
int main(int argc, char *argv[])
{
    struct fb_fix_screeninfo fb_fix;
    struct fb_var_screeninfo fb_var;
    unsigned int screen_size;
    int fd;
    /* 传参校验 */
    if (2 != argc) {
        fprintf(stderr, "usage: %s <bmp_file>\n", argv[0]);
        exit(-1);
    }
    /* 打开 framebuffer 设备 */
    if (0 > (fd = open("/dev/fb0", O_RDWR))) {
        perror("open error");
        exit(EXIT_FAILURE);
    }
    /* 获取参数信息 */
    ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
    ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);
    screen_size = fb_fix.line_length * fb_var.yres;
    line_length = fb_fix.line_length;
    width = fb_var.xres;
    height = fb_var.yres;
    /* 将显示缓冲区映射到进程地址空间 */
    screen_base = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, fd, 0);
    if (MAP_FAILED == (void *)screen_base) {
        perror("mmap error");
        close(fd);
        exit(EXIT_FAILURE);
    }
    /* 显示 BMP 图片 */
    memset(screen_base, 0xFF, screen_size);
    show_bmp_image(argv[1]);
    /* 退出 */
    munmap(screen_base, screen_size); //取消映射
    close(fd); //关闭文件
    exit(EXIT_SUCCESS); //退出进程
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Framebuffer应用编程是一种在图形终端上编程的技术,在此基础之上可以进行多种应用,其中包括: 1、在LCD上显示图形: 通过Framebuffer应用编程技术,可以将图形数据写入Framebuffer中,然后由系统将Framebuffer上的数据显示到LCD屏幕上,从而实现在LCD屏幕上显示图形的功能。 2、显示一幅图像文件的图案: 同样通过Framebuffer应用编程技术,可以读取图像文件中的图像数据,将其写入Framebuffer中,然后由系统将Framebuffer上的数据显示到图形终端上,从而实现显示一幅图像文件的图案的功能。 需要注意的是,在进行Framebuffer应用编程时,需要详细了解分辨率、色深、像素布局等概念,以便在实现各种功能时能够精确控制图像的显示效果。此外,也需要根据具体的应用需求,选择适合的Framebuffer设备驱动程序和图像处理工具来完成各种操作。 ### 回答2: Framebuffer应用编程是指使用Linux Framebuffer驱动程序对图形系统进行编程开发,实现在LCD上显示图形或显示一幅图像文件的图案等功能。该技术可以方便地操作图像,包括绘图、图像处理、图像显示等等操作。 在LCD上显示图形需要先对LCD进行初始化,将待显示的图形或文本等信息储存在Framebuffer中,接着通过LCD的STN或TFT控制器将数据发送到屏幕上进行显示。在这个过程中需要考虑屏幕刷新率的问题,通常情况下一般在50~60Hz左右,否则会出现闪烁和撕裂现象,影响视觉效果。 显示一幅图像文件的图案需要读取图像文件,解析其中的图像数据,并将数据储存在Framebuffer中,接着执行刷新操作,将图像显示在LCD上。常见的图像格式有JPEG、PNG、BMP等,不同格式的处理方式也不同,需要根据实际要求进行适配。 Framebuffer应用编程是Linux图形界面编程的基础,对于需要进行图形化开发的领域,如嵌入式系统、游戏开发、嵌入式医疗设备等等都有广泛应用。同时也需要开发者具备一定的图像处理、图形算法、嵌入式开发等技能储备。 ### 回答3: Framebuffer是一种广泛应用于图形处理和显示设备中的概念。在应用编程过程中,它可以用于实现很多不同的功能,其中包括在LCD上显示图形和显示图像文件的图案。 在LCD上显示图形 要在LCD上显示图形,首先需要将对应的图形数据写入Framebuffer中,然后通过设备驱动程序将Framebuffer中的数据映射到对应的LCD显示区域中。为了实现这一过程,需要开发对应的应用程序或者使用已有的图形库,并按照规定的格式对图形数据进行处理和传输。 通常情况下,图形数据需要经过转换和优化,以兼容不同的显示设备和分辨率。并且,为了保证显示效果的质量,还需要考虑灰度、色彩和对比度等方面的参数设置,进一步改善图形的可读性和丰富性。 显示一幅图像文件的图案 要显示一幅图像文件的图案,需要先进行图像文件的读取和解码,将解码后的数据写入Framebuffer中。此后,通过上述的驱动程序映射过程,可以将图像数据同步到对应的显示设备中,实现图像的显示。 对于不同的图像格式和压缩方式,需要采用不同的解码算法和参数设置,以保证解码质量和速度。同时,为了达到更好的显示效果和资源利用率,也需要进行必要的图像处理和优化,如缩放、抖动、锐化等处理。 总之,Framebuffer应用编程具有很高的灵活性和可扩展性,可以实现很多不同类型的图形和图像处理任务,可以满足不同用户的需要和要求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值