FrameBuffer是Linux下对于显存操作的抽象层,一般作为一个驱动文件,位置:/dev/fb0。
用C++封装了一下,便于使用:
- fbhelper.h
#ifndef FBHELPER_H
#define FBHELPER_H
//非线程安全
struct fb_var_screeninfo;
class FBHelper
{
public:
FBHelper();
~FBHelper();
int setScreenPtr(int index);
const unsigned char* getScreenPtr();
private:
int _iFbFd;
int _iFbMemSize;
int _iFbPixelByte;
void *_pFbMemAddr;
struct fb_var_screeninfo* _pFbScreenInfo;
};
#endif // FBHELPER_H
- fbhelper.cpp
#include "fbhelper.h"
#include "guidef.h"
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#define FB_NODE ("/dev/fb0")
FBHelper::FBHelper() : _iFbFd(-1), _iFbMemSize(0), _iFbPixelByte(1), _pFbMemAddr(NULL), _pFbScreenInfo(NULL)
{
_pFbScreenInfo = new struct fb_var_screeninfo;
_iFbFd = open(FB_NODE, O_RDWR);
if ( _iFbFd < 0 || ioctl(_iFbFd, FBIOGET_VSCREENINFO, _pFbScreenInfo) != 0 )
{
std::cout << "[ERR] Fail to operate frame buffer.\n";
}
_pFbScreenInfo->yoffset = 0;
_pFbScreenInfo->xres = BMPWIDTH;
_pFbScreenInfo->yres = BMPHEIGHT * MAX_FB_BUFFER_NUM;
_pFbScreenInfo->xres_virtual = _pFbScreenInfo->xres;
_pFbScreenInfo->yres_virtual = _pFbScreenInfo->yres;
if ( ioctl(_iFbFd, FBIOPUT_VSCREENINFO, _pFbScreenInfo) != 0 )
{
std::cout << "[ERR] Fail to set fb info.\n";
}
struct fb_fix_screeninfo fixInfo;
if ( ioctl(_iFbFd, FBIOGET_FSCREENINFO, &fixInfo) != 0 )
{
std::cout << "[ERR] Fail to get fb fix info.\n";
}
// else
// {
// int *p = (int *)(&fixInfo);
// for (int i = 0; i < sizeof(struct fb_fix_screeninfo)/sizeof(int); i++)
// {
// printf("%d==0x%x %d==\n", i, *p, *p);
// p++;
// }
// }
_iFbPixelByte = _pFbScreenInfo->bits_per_pixel / 8;
_iFbMemSize = _pFbScreenInfo->xres_virtual * _pFbScreenInfo->yres_virtual * _iFbPixelByte;
_pFbMemAddr = mmap(0, _iFbMemSize, PROT_WRITE | PROT_READ, MAP_SHARED, _iFbFd, 0);
if ( _pFbMemAddr == MAP_FAILED )
{
std::cout << "[ERR] Fail to mmap fb.\n";
}
// printf("[fb vir-addr = 0x%x - 0x%x]\n", (int)_pFbMemAddr, (int)_pFbMemAddr + _iFbMemSize);
memset(_pFbMemAddr, 255, _iFbMemSize);
}
FBHelper::~FBHelper()
{
if (_pFbMemAddr && _iFbMemSize > 0)
{
munmap(_pFbMemAddr, _iFbMemSize);
}
if (_iFbFd >= 0)
{
close(_iFbFd);
}
if (_pFbScreenInfo)
{
delete _pFbScreenInfo;
}
}
int FBHelper::setScreenPtr(int index)
{
if (!_pFbScreenInfo)
{
return -1;
}
int iFrameBytes = BMPWIDTH* BMPHEIGHT * _iFbPixelByte;
if ( (index + 1) * iFrameBytes > _iFbMemSize )
{
return -1;
}
_pFbScreenInfo->yoffset = index * BMPHEIGHT;
if ( ioctl(_iFbFd, FBIOPUT_VSCREENINFO, _pFbScreenInfo) < 0 )
{
return -1;
}
return 0;
}
const unsigned char *FBHelper::getScreenPtr()
{
if (!_pFbScreenInfo)
{
return NULL;
}
//ioctl(_iFbFd, FBIO_WAITFORVSYNC, 0);
return (unsigned char*)_pFbMemAddr + _pFbScreenInfo->yoffset * BMPWIDTH * _pFbScreenInfo->bits_per_pixel / 8;
}