2015-01-18 00:04:45 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <linux/fb.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Local includes
|
2016-03-09 07:03:17 +01:00
|
|
|
#include <grabber/FramebufferFrameGrabber.h>
|
2015-01-18 00:04:45 +01:00
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
FramebufferFrameGrabber::FramebufferFrameGrabber(const QString & device, const unsigned width, const unsigned height) :
|
2015-01-18 00:04:45 +01:00
|
|
|
_fbfd(0),
|
|
|
|
_fbp(0),
|
|
|
|
_fbDevice(device),
|
|
|
|
_width(width),
|
|
|
|
_height(height),
|
2016-07-12 00:53:48 +02:00
|
|
|
_imgResampler(new ImageResampler()),
|
|
|
|
_log(Logger::getInstance("FRAMEBUFFERGRABBER"))
|
2015-01-18 00:04:45 +01:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct fb_var_screeninfo vinfo;
|
|
|
|
|
|
|
|
// Check if the framebuffer device can be opened and display the current resolution
|
2017-03-04 22:17:42 +01:00
|
|
|
_fbfd = open(QSTRING_CSTR(_fbDevice), O_RDONLY);
|
2015-01-24 23:42:22 +01:00
|
|
|
if (_fbfd == 0)
|
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
Error(_log, "Error openning %s", QSTRING_CSTR(_fbDevice));
|
2015-01-24 23:42:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get variable screen information
|
|
|
|
result = ioctl (_fbfd, FBIOGET_VSCREENINFO, &vinfo);
|
|
|
|
if (result != 0)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "Could not get screen information");
|
2015-01-24 23:42:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "Display opened with resolution: %dx%d@%dbit", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
|
2015-01-24 23:42:22 +01:00
|
|
|
}
|
|
|
|
close(_fbfd);
|
|
|
|
}
|
2015-01-18 00:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FramebufferFrameGrabber::~FramebufferFrameGrabber()
|
|
|
|
{
|
2015-01-24 23:42:22 +01:00
|
|
|
delete _imgResampler;
|
2015-01-18 00:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FramebufferFrameGrabber::setVideoMode(const VideoMode videoMode)
|
|
|
|
{
|
2015-01-24 23:42:22 +01:00
|
|
|
_imgResampler->set3D(videoMode);
|
2015-01-18 00:04:45 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 23:42:22 +01:00
|
|
|
void FramebufferFrameGrabber::grabFrame(Image<ColorRgb> & image)
|
2015-01-18 00:04:45 +01:00
|
|
|
{
|
|
|
|
struct fb_var_screeninfo vinfo;
|
2015-01-24 23:42:22 +01:00
|
|
|
unsigned capSize, bytesPerPixel;
|
|
|
|
PixelFormat pixelFormat;
|
2015-01-18 00:04:45 +01:00
|
|
|
|
|
|
|
/* Open the framebuffer device */
|
2017-03-04 22:17:42 +01:00
|
|
|
_fbfd = open(QSTRING_CSTR(_fbDevice), O_RDONLY);
|
2015-01-18 00:04:45 +01:00
|
|
|
|
|
|
|
/* get variable screen information */
|
|
|
|
ioctl (_fbfd, FBIOGET_VSCREENINFO, &vinfo);
|
|
|
|
|
|
|
|
bytesPerPixel = vinfo.bits_per_pixel / 8;
|
|
|
|
capSize = vinfo.xres * vinfo.yres * bytesPerPixel;
|
2015-01-24 23:42:22 +01:00
|
|
|
|
|
|
|
if (vinfo.bits_per_pixel == 16)
|
|
|
|
{
|
|
|
|
pixelFormat = PIXELFORMAT_BGR16;
|
|
|
|
}
|
|
|
|
else if (vinfo.bits_per_pixel == 24)
|
|
|
|
{
|
|
|
|
pixelFormat = PIXELFORMAT_BGR24;
|
|
|
|
}
|
|
|
|
else if (vinfo.bits_per_pixel == 32)
|
|
|
|
{
|
|
|
|
pixelFormat = PIXELFORMAT_BGR32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "Unknown pixel format: %d bits per pixel", vinfo.bits_per_pixel);
|
2015-01-24 23:42:22 +01:00
|
|
|
close(_fbfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-18 00:04:45 +01:00
|
|
|
/* map the device to memory */
|
|
|
|
_fbp = (unsigned char*)mmap(0, capSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, _fbfd, 0);
|
|
|
|
|
2015-01-24 23:42:22 +01:00
|
|
|
_imgResampler->setHorizontalPixelDecimation(vinfo.xres/_width);
|
|
|
|
_imgResampler->setVerticalPixelDecimation(vinfo.yres/_height);
|
|
|
|
_imgResampler->processImage(_fbp,
|
|
|
|
vinfo.xres,
|
|
|
|
vinfo.yres,
|
|
|
|
vinfo.xres * bytesPerPixel,
|
|
|
|
pixelFormat,
|
|
|
|
image);
|
2015-01-18 00:04:45 +01:00
|
|
|
|
|
|
|
munmap(_fbp, capSize);
|
|
|
|
close(_fbfd);
|
|
|
|
}
|