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>
|
2020-07-12 09:19:59 +02:00
|
|
|
#include <cstring>
|
2015-01-18 00:04:45 +01:00
|
|
|
|
|
|
|
// 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-08-04 23:08:15 +02:00
|
|
|
FramebufferFrameGrabber::FramebufferFrameGrabber(const QString & device, const unsigned width, const unsigned height)
|
|
|
|
: Grabber("FRAMEBUFFERGRABBER", width, height)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _fbDevice()
|
2015-01-18 00:04:45 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
setDevicePath(device);
|
2015-01-18 00:04:45 +01:00
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
int FramebufferFrameGrabber::grabFrame(Image<ColorRgb> & image)
|
2015-01-18 00:04:45 +01:00
|
|
|
{
|
2017-09-01 08:50:37 +02:00
|
|
|
if (!_enabled) return 0;
|
|
|
|
|
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 */
|
2020-07-12 09:19:59 +02:00
|
|
|
int fbfd = open(QSTRING_CSTR(_fbDevice), O_RDONLY);
|
|
|
|
if (fbfd == -1)
|
|
|
|
{
|
|
|
|
Error(_log, "Error opening %s, %s : ", QSTRING_CSTR(_fbDevice), std::strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-18 00:04:45 +01:00
|
|
|
|
|
|
|
/* get variable screen information */
|
2020-07-12 09:19:59 +02:00
|
|
|
ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
|
2015-01-18 00:04:45 +01:00
|
|
|
|
|
|
|
bytesPerPixel = vinfo.bits_per_pixel / 8;
|
|
|
|
capSize = vinfo.xres * vinfo.yres * bytesPerPixel;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
switch (vinfo.bits_per_pixel)
|
2015-01-24 23:42:22 +01:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
case 16: pixelFormat = PixelFormat::BGR16; break;
|
|
|
|
case 24: pixelFormat = PixelFormat::BGR24; break;
|
2019-03-08 23:51:50 +01:00
|
|
|
#ifdef ENABLE_AMLOGIC
|
2020-06-28 23:05:32 +02:00
|
|
|
case 32: pixelFormat = PixelFormat::PIXELFORMAT_RGB32; break;
|
2019-03-08 23:51:50 +01:00
|
|
|
#else
|
2020-06-28 23:05:32 +02:00
|
|
|
case 32: pixelFormat = PixelFormat::BGR32; break;
|
2019-03-08 23:51:50 +01:00
|
|
|
#endif
|
2017-08-12 07:55:32 +02:00
|
|
|
default:
|
|
|
|
Error(_log, "Unknown pixel format: %d bits per pixel", vinfo.bits_per_pixel);
|
2020-07-12 09:19:59 +02:00
|
|
|
close(fbfd);
|
2017-08-12 07:55:32 +02:00
|
|
|
return -1;
|
2015-01-24 23:42:22 +01:00
|
|
|
}
|
2017-08-12 07:55:32 +02:00
|
|
|
|
2015-01-18 00:04:45 +01:00
|
|
|
/* map the device to memory */
|
2020-07-12 09:19:59 +02:00
|
|
|
unsigned char * fbp = (unsigned char*)mmap(0, capSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, fbfd, 0);
|
|
|
|
if (fbp == MAP_FAILED) {
|
|
|
|
Error(_log, "Error mapping %s, %s : ", QSTRING_CSTR(_fbDevice), std::strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-18 00:04:45 +01:00
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
_imageResampler.setHorizontalPixelDecimation(vinfo.xres/_width);
|
|
|
|
_imageResampler.setVerticalPixelDecimation(vinfo.yres/_height);
|
2020-07-12 09:19:59 +02:00
|
|
|
_imageResampler.processImage(fbp,
|
2015-01-24 23:42:22 +01:00
|
|
|
vinfo.xres,
|
|
|
|
vinfo.yres,
|
|
|
|
vinfo.xres * bytesPerPixel,
|
|
|
|
pixelFormat,
|
|
|
|
image);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2020-07-12 09:19:59 +02:00
|
|
|
munmap(fbp, capSize);
|
|
|
|
close(fbfd);
|
2017-08-12 07:55:32 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-01-18 00:04:45 +01:00
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
void FramebufferFrameGrabber::setDevicePath(const QString& path)
|
|
|
|
{
|
|
|
|
if(_fbDevice != path)
|
|
|
|
{
|
|
|
|
_fbDevice = path;
|
|
|
|
int result;
|
|
|
|
struct fb_var_screeninfo vinfo;
|
|
|
|
|
|
|
|
// Check if the framebuffer device can be opened and display the current resolution
|
2020-07-12 09:19:59 +02:00
|
|
|
int fbfd = open(QSTRING_CSTR(_fbDevice), O_RDONLY);
|
|
|
|
if (fbfd == -1)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2020-07-12 09:19:59 +02:00
|
|
|
Error(_log, "Error opening %s, %s : ", QSTRING_CSTR(_fbDevice), std::strerror(errno));
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get variable screen information
|
2020-07-12 09:19:59 +02:00
|
|
|
result = ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
|
2018-12-27 23:11:32 +01:00
|
|
|
if (result != 0)
|
|
|
|
{
|
2020-07-12 09:19:59 +02:00
|
|
|
Error(_log, "Could not get screen information, %s", std::strerror(errno));
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Info(_log, "Display opened with resolution: %dx%d@%dbit", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
|
|
|
|
}
|
2020-07-12 09:19:59 +02:00
|
|
|
close(fbfd);
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|