电子量产工具——2-3显示系统_显示管理
软件总框架
一、显示管理
disp_manager.c的作用 1.承上启下 2.下面各个模块把DispOpr注册进来 3.上面选择某个模块 4.还提供一些函数,比如PutPixel
二、
1.framebuffer.c
framebuffer.c
#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 "disp_manager.h"
static int fd_fb;
static struct fb_var_screeninfo var;
static int screen_size;
static unsigned char *fb_base;
static unsigned int line_width;
static unsigned int pixel_width;
static int FbDeviceInit(void)
{
fd_fb = open("/dev/fb0", O_RDWR);
if (fd_fb < 0)
{
printf("can't open /dev/fb0\n");
return -1;
}
if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
{
printf("can't get var\n");
return -1;
}
line_width = var.xres * var.bits_per_pixel / 8;
pixel_width = var.bits_per_pixel / 8;
screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
fb_base = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
if (fb_base == (unsigned char *)-1)
{
printf("can't mmap\n");
return -1;
}
return 0;
}
static int FbDeviceExit(void)
{
munmap(fb_base, screen_size);
close(fb_base);
return 0;
}
static int FbGetBuffer(PDispBuff ptDispBuff)
{
ptDispBuff->iXres = var.xres;
ptDispBuff->iYres = var.yres;
ptDispBuff->iBpp = var.bits_per_pixel;
ptDispBuff->buff = fb_base;
return 0;
}
static int FbFlushRegion(PRegion ptRegion, PDispBuff ptDispBuff)
{
return 0;
}
static DispOpr g_tFramebufferOpr = {
.name = "fb",
.DeviceInit = FbDeviceInit,
.DeviceExit = FbDeviceExit,
.GetBuffer = FbGetBuffer,
.FlushRegion = FbFlushRegion,
};
void FramebufferInit(void)
{
RegisterDisplay(&g_tFramebufferOpr);
}
2.disp_manager.h
disp_manager.h
#ifndef _DISP_MANAGER_H
#define _DISP_MANAGER_H
typedef struct DispBuff {
int iXres;
int iYres;
int iBpp;
char *buff;
}DispBuff, *PDispBuff;
typedef struct Region {
int iLeftUpX;
int iLeftUpY;
int iWidth;
int iHeigh;
}Region, *PRegion;
typedef struct DispOpr {
char *name;
int DeviceInit(void);
int DeviceExit(void);
int GetBuffer(PDispBuff ptDispBuff);
int FlushRegion(PRegion ptRegion, char *buffer);
struct DispOpr *ptNext;
}DispOpr, *PDispOpr;
void RegisterDisplay(PDispOpr ptDispOpr);
void SelectDefaultDisplay(char *name);
int InitDefaultDisplay(void);
void DisplayInit(PDispOpr ptDispOpr);
int PutPixel(int x, int y, unsigned int dwColor);
static int FlushDisplayRegion(PRegion ptRegion, PDispBuff ptDispBuff);
#endif
3.disp_manager.c
disp_manager.c
#include "disp_manager.h"
static PDispOpr g_DispDevs = NULL;
static PDispOpr g_DispDefault = NULL;
static DispBuff g_tDispBuff;
static int line_width;
static int pixel_width;
int PutPixel(int x, int y, unsigned int dwColor)
{
unsigned char *pen_8 = g_tDispBuff->buff+y*line_width+x*pixel_width;
unsigned short *pen_16;
unsigned int *pen_32;
unsigned int red, green, blue;
pen_16 = (unsigned short *)pen_8;
pen_32 = (unsigned int *)pen_8;
switch (g_tDispBuff.iBpp)
{
case 8:
{
*pen_8 = dwColor;
break;
}
case 16:
{
red = (dwColor >> 16) & 0xff;
green = (dwColor >> 8) & 0xff;
blue = (dwColor >> 0) & 0xff;
dwColor = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
*pen_16 = dwColor;
break;
}
case 32:
{
*pen_32 = dwColor;
break;
}
default:
{
printf("can't surport %dbpp\n", g_tDispBuff.iBpp);
break;
}
}
}
void RegisterDisplay(PDispOpr ptDispOpr)
{
ptDispOpr->ptNext = g_DispDevs;
g_DispDevs = ptDispOpr;
}
void SelectDefaultDisplay(char *name)
{
PDispOpr pTmp = g_DispDevs;
while (pTmp)
{
if(strcmp(name, pTmp->name) == 0)
{
g_DispDefault = pTmp;
return 0;
}
pTmp = pTmp->ptNext;
}
return -1;
}
int InitDefaultDisplay(void)
{
int ret;
ret = g_DispDefault->DeviceInit();
if(ret)
{
printf("DeviceInit err\n");
return -1;
}
ret = g_DispDefault->GetBuffer(g_tDispBuff);
if(ret)
{
printf("GetBuffer err\n");
return -1;
}
line_width = g_tDispBuff.iXres * g_tDispBuff.iBpp/8;
pixel_width = g_tDispBuff.iBpp/8
return 0;
}
static int FlushDisplayRegion(PRegion ptRegion, PDispBuff ptDispBuff)
{
int ret;
ret = g_DispDefault->FlushRegion(ptRegion, ptDispBuff);
return 0;
}
void DisplayInit(PDispOpr ptDispOpr)
{
FramebufferInit();
}
总结
1.display 初始化 {//2.调用framebuffer初始化} 3.注册LCD硬件相关的结构体 4.注册LCD硬件相关函数 放入 g_DispDevs 链表 5.对应不同的显示方式 ——》SelectDefaultDisplay 6.来选择默认的输出设备 7.来初始化默认的设备 ————》》然后去调用 PutPixel 8.绘制想要的图片 ————》》 更新到硬件上去 FlushDisplayRegion 9.绘制好的区域 更新(刷)到硬件上去
|