Framebuffer顾名思义,Frame是帧的意思,buffer是缓冲区的。Framebuffer中保存着每一帧图像的每一个像素的颜色值。
LCD操作原理
- 驱动程序设置好LCD控制器
- 根据LCD参数设置LCD控制器的时序,信号极性
- 根据LCD分辨率,BPP分配Framebuffer
- APP通过ioctl获取LCD的分辨率,BPP等参数
- APP通过mmap映射Framebuffer,在Framebuffer中写入数据。
从上图可以看到Framebuffer和LCD的可显示区域是一一对应的。使用Framebuffer显示实际就是向Framebuffer写入每个像素点的颜色数据。LCD的像素点的坐标与Framebuffer中的位置关系如下图
给出任意屏幕坐标(x,y),以及Framebuffer的基地址fb_base.另外LCD的分辨率是Xres x Yres,表示单个像素颜色的二进制位数bpp。则其颜色数据在framebuffer中的地址是:
(x,y)像素起始地址=fb_base + (y*Xres+x)*bpp/8
像素颜色表示
示例代码
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <math.h>
static int fd_fb;
static struct fb_var_screeninfo var; /* Current var */
static int screen_size; /* 一帧数据所占用的字节数*/
static unsigned char *fb_base; /* Framebuffer首地址*/
static unsigned int line_width; /* 一行数据所占用的字节数*/
static unsigned int pixel_width; /* 单个像素所占用的字节数*/
/**********************************************************************
* 函数名称: lcd_put_pixel
* 功能描述: 在LCD指定位置上输出指定颜色(描点)
* 输入参数: x坐标,y坐标,颜色
* 输出参数: 无
* 返 回 值: 会
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2020/05/12 V1.0 zh(angenao) 创建
***********************************************************************/
void lcd_put_pixel(int x, int y, unsigned int color)