FrameBuffer操作

FrameBuffer是Linux下对于显存操作的抽象层,一般作为一个驱动文件,位置:/dev/fb0。
用C++封装了一下,便于使用:

  1. 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
  1. 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值