grabber api and feature unification (#462)

* move setvideomode to common place

* implement more croping and 3d support

* more api unification

* more refactoring

* osx fix

* next step

* add a mock for osx grabber. Now it is possible to test compile on none osx platforms.

* more unifications ...

* remove obsolete includes and grabbers are not dyn allocated. dispmanx needs rework an probaly not work atm

* first version of dispmanx mock. it compiles, but outputs a black image

* now dispmanx mock works!

* activate mocks in travis linux build
prepare dispmanx to rgb image out

* dispmanx now with image rgb output
fix deadlock with w/h -1 in grabber v4l
cleanups

* fix json

* fix some runtime stuff

* Update FramebufferWrapper.cpp

fix missing code

* unify grabframe

* 3d and croping for amlogic

* fix setimage not working

* make use of templates
save some codelines

* save more code lines
This commit is contained in:
redPanther
2017-08-12 07:55:32 +02:00
committed by GitHub
parent 317a903b14
commit 9eff6384cc
61 changed files with 830 additions and 748 deletions

View File

@@ -14,8 +14,11 @@ X11Grabber::X11Grabber(bool useXGetImage, int cropLeft, int cropRight, int cropT
, _verticalDecimation(verticalPixelDecimation)
, _screenWidth(0)
, _screenHeight(0)
, _src_x(cropLeft)
, _src_y(cropTop)
, _image(0,0)
{
_useImageResampler = false;
_imageResampler.setCropping(0, 0, 0, 0); // cropping is performed by XRender, XShmGetImage or XGetImage
memset(&_pictAttr, 0, sizeof(_pictAttr));
_pictAttr.repeat = RepeatNone;
@@ -52,9 +55,7 @@ void X11Grabber::setupResources()
{
if(_XShmAvailable && !_useXGetImage)
{
_xImage = XShmCreateImage(_x11Display, _windowAttr.visual,
_windowAttr.depth, ZPixmap, NULL, &_shminfo,
_width, _height);
_xImage = XShmCreateImage(_x11Display, _windowAttr.visual, _windowAttr.depth, ZPixmap, NULL, &_shminfo, _width, _height);
_shminfo.shmid = shmget(IPC_PRIVATE, _xImage->bytes_per_line * _xImage->height, IPC_CREAT|0777);
_xImage->data = (char*)shmat(_shminfo.shmid,0,0);
_shminfo.shmaddr = _xImage->data;
@@ -109,13 +110,16 @@ bool X11Grabber::Setup()
_imageResampler.setHorizontalPixelDecimation(_XRenderAvailable ? 1 : _horizontalDecimation);
_imageResampler.setVerticalPixelDecimation(_XRenderAvailable ? 1 : _verticalDecimation);
return true;
bool result = (updateScreenDimensions(true) >=0);
ErrorIf(!result, _log, "X11 Grabber start failed");
return result;
}
Image<ColorRgb> & X11Grabber::grab()
int X11Grabber::grabFrame(Image<ColorRgb> & image, bool forceUpdate)
{
updateScreenDimensions();
if (forceUpdate)
updateScreenDimensions(forceUpdate);
if (_XRenderAvailable && !_useXGetImage)
{
double scale_x = static_cast<double>(_windowAttr.width / _horizontalDecimation) / static_cast<double>(_windowAttr.width);
@@ -145,19 +149,11 @@ Image<ColorRgb> & X11Grabber::grab()
XRenderSetPictureTransform (_x11Display, _srcPicture, &_transform);
XRenderComposite( _x11Display, // dpy
PictOpSrc, // op
_srcPicture, // src
None, // mask
_dstPicture, // dst
_cropLeft / _horizontalDecimation, // src_x _cropLeft
_cropTop / _verticalDecimation, // src_y _cropTop
0, // mask_x
0, // mask_y
0, // dst_x
0, // dst_y
_width, // width
_height); // height
// display, op, src, mask, dest, src_x = cropLeft,
// src_y = cropTop, mask_x, mask_y, dest_x, dest_y, width, height
XRenderComposite(
_x11Display, PictOpSrc, _srcPicture, None, _dstPicture, ( _src_x/_horizontalDecimation),
(_src_y/_verticalDecimation), 0, 0, 0, 0, _width, _height);
XSync(_x11Display, False);
@@ -170,93 +166,15 @@ Image<ColorRgb> & X11Grabber::grab()
_xImage = XGetImage(_x11Display, _pixmap, 0, 0, _width, _height, AllPlanes, ZPixmap);
}
}
else
else if (_XShmAvailable && !_useXGetImage)
{
if (_XShmAvailable && !_useXGetImage) {
XShmGetImage(_x11Display, _window, _xImage, _cropLeft, _cropTop, AllPlanes);
}
else
{
_xImage = XGetImage(_x11Display, _window, _cropLeft, _cropTop, _width, _height, AllPlanes, ZPixmap);
}
}
if (_xImage == nullptr)
{
Error(_log, "Grab Failed!");
return _image;
}
_imageResampler.processImage(reinterpret_cast<const uint8_t *>(_xImage->data), _xImage->width, _xImage->height, _xImage->bytes_per_line, PIXELFORMAT_BGR32, _image);
return _image;
}
int X11Grabber::grabFrame(Image<ColorRgb> & image)
{
if (_XRenderAvailable && !_useXGetImage)
{
double scale_x = static_cast<double>(_windowAttr.width / _horizontalDecimation) / static_cast<double>(_windowAttr.width);
double scale_y = static_cast<double>(_windowAttr.height / _verticalDecimation) / static_cast<double>(_windowAttr.height);
double scale = qMin(scale_y, scale_x);
_transform =
{
{
{
XDoubleToFixed(1),
XDoubleToFixed(0),
XDoubleToFixed(0)
},
{
XDoubleToFixed(0),
XDoubleToFixed(1),
XDoubleToFixed(0)
},
{
XDoubleToFixed(0),
XDoubleToFixed(0),
XDoubleToFixed(scale)
}
}
};
XRenderSetPictureTransform (_x11Display, _srcPicture, &_transform);
XRenderComposite( _x11Display, // dpy
PictOpSrc, // op
_srcPicture, // src
None, // mask
_dstPicture, // dst
_cropLeft / _horizontalDecimation, // src_x _cropLeft
_cropTop / _verticalDecimation, // src_y _cropTop
0, // mask_x
0, // mask_y
0, // dst_x
0, // dst_y
_width, // width
_height); // height
XSync(_x11Display, False);
if (_XShmAvailable)
{
XShmGetImage(_x11Display, _pixmap, _xImage, 0, 0, AllPlanes);
}
else
{
_xImage = XGetImage(_x11Display, _pixmap, 0, 0, _width, _height, AllPlanes, ZPixmap);
}
// use xshm
XShmGetImage(_x11Display, _window, _xImage, _src_x, _src_y, AllPlanes);
}
else
{
if (_XShmAvailable && !_useXGetImage) {
XShmGetImage(_x11Display, _window, _xImage, _cropLeft, _cropTop, AllPlanes);
}
else
{
_xImage = XGetImage(_x11Display, _window, _cropLeft, _cropTop, _width, _height, AllPlanes, ZPixmap);
}
// all things done by xgetimage
_xImage = XGetImage(_x11Display, _window, _src_x, _src_y, _width, _height, AllPlanes, ZPixmap);
}
if (_xImage == nullptr)
@@ -270,7 +188,7 @@ int X11Grabber::grabFrame(Image<ColorRgb> & image)
return 0;
}
int X11Grabber::updateScreenDimensions()
int X11Grabber::updateScreenDimensions(bool force)
{
const Status status = XGetWindowAttributes(_x11Display, _window, &_windowAttr);
if (status == 0)
@@ -279,7 +197,7 @@ int X11Grabber::updateScreenDimensions()
return -1;
}
if (_screenWidth == unsigned(_windowAttr.width) && _screenHeight == unsigned(_windowAttr.height))
if (!force && _screenWidth == unsigned(_windowAttr.width) && _screenHeight == unsigned(_windowAttr.height))
{
// No update required
return 0;
@@ -294,14 +212,16 @@ int X11Grabber::updateScreenDimensions()
_screenWidth = _windowAttr.width;
_screenHeight = _windowAttr.height;
int width=0, height=0;
// Image scaling is performed by XRender when available, otherwise by ImageResampler
if (_XRenderAvailable && !_useXGetImage)
{
_width = (_screenWidth > unsigned(_cropLeft + _cropRight))
width = (_screenWidth > unsigned(_cropLeft + _cropRight))
? ((_screenWidth - _cropLeft - _cropRight) / _horizontalDecimation)
: _screenWidth / _horizontalDecimation;
_height = (_screenHeight > unsigned(_cropTop + _cropBottom))
height = (_screenHeight > unsigned(_cropTop + _cropBottom))
? ((_screenHeight - _cropTop - _cropBottom) / _verticalDecimation)
: _screenHeight / _verticalDecimation;
@@ -309,19 +229,52 @@ int X11Grabber::updateScreenDimensions()
}
else
{
_width = (_screenWidth > unsigned(_cropLeft + _cropRight))
width = (_screenWidth > unsigned(_cropLeft + _cropRight))
? (_screenWidth - _cropLeft - _cropRight)
: _screenWidth;
_height = (_screenHeight > unsigned(_cropTop + _cropBottom))
height = (_screenHeight > unsigned(_cropTop + _cropBottom))
? (_screenHeight - _cropTop - _cropBottom)
: _screenHeight;
Info(_log, "Using XGetImage for grabbing");
}
// calculate final image dimensions and adjust top/left cropping in 3D modes
switch (_videoMode)
{
case VIDEO_3DSBS:
_width = width /2;
_height = height;
_src_x = _cropLeft / 2;
_src_y = _cropTop;
break;
case VIDEO_3DTAB:
_width = width;
_height = height / 2;
_src_x = _cropLeft;
_src_y = _cropTop / 2;
break;
case VIDEO_2D:
default:
_width = width;
_height = height;
_src_x = _cropLeft;
_src_y = _cropTop;
break;
}
Info(_log, "Update output image resolution: [%dx%d] to [%dx%d]", _image.width(), _image.height(), _width, _height);
_image.resize(_width, _height);
setupResources();
return 1;
}
void X11Grabber::setVideoMode(VideoMode mode)
{
Info(_log, "a %d", mode);
Grabber::setVideoMode(mode);
updateScreenDimensions(true);
}

View File

@@ -1,78 +1,24 @@
// Hyperion includes
#include <hyperion/Hyperion.h>
#include <hyperion/ImageProcessorFactory.h>
#include <hyperion/ImageProcessor.h>
// X11 grabber includes
#include <grabber/X11Wrapper.h>
#include <grabber/X11Grabber.h>
X11Wrapper::X11Wrapper(bool useXGetImage, int cropLeft, int cropRight, int cropTop, int cropBottom, int horizontalPixelDecimation, int verticalPixelDecimation, const unsigned updateRate_Hz, const int priority)
: GrabberWrapper("X11", priority)
, _updateInterval_ms(1000/updateRate_Hz)
, _timeout_ms(2 * _updateInterval_ms)
, _grabber(new X11Grabber(useXGetImage, cropLeft, cropRight, cropTop, cropBottom, horizontalPixelDecimation, verticalPixelDecimation))
, _ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb{0,0,0})
: GrabberWrapper("X11", &_grabber, 0, 0, updateRate_Hz, priority, hyperion::COMP_GRABBER)
, _grabber(useXGetImage, cropLeft, cropRight, cropTop, cropBottom, horizontalPixelDecimation, verticalPixelDecimation)
, _init(false)
, _x11SetupSuccess(false)
{
// Configure the timer to generate events every n milliseconds
_timer.setInterval(_updateInterval_ms);
}
{}
X11Wrapper::~X11Wrapper()
{
delete _grabber;
}
bool X11Wrapper::start()
void X11Wrapper::action()
{
if (! _init )
{
_init = true;
_x11SetupSuccess = _grabber->Setup();
if ( _x11SetupSuccess )
if ( ! _grabber.Setup() )
{
_x11SetupSuccess = (_grabber->updateScreenDimensions() >= 0);
_processor->setSize(_grabber->getImageWidth(), _grabber->getImageHeight());
_image.resize(_grabber->getImageWidth(), _grabber->getImageHeight());
stop();
}
}
// Start the timer with the pre configured interval
if ( _x11SetupSuccess )
if (_grabber.updateScreenDimensions() >= 0 )
{
GrabberWrapper::start();
transferFrame(_grabber);
}
ErrorIf( ! _x11SetupSuccess, _log, "X11 Grabber start failed");
return _x11SetupSuccess;
}
void X11Wrapper::action()
{
int result = _grabber->updateScreenDimensions();
if (result < 0 )
{
return;
}
if ( result > 0 )
{
_processor->setSize(_grabber->getImageWidth(), _grabber->getImageHeight());
_image.resize(_grabber->getImageWidth(), _grabber->getImageHeight());
}
// Grab frame into the allocated image
_grabber->grabFrame(_image);
emit emitImage(_priority, _image, _timeout_ms);
_processor->process(_image, _ledColors);
setColors(_ledColors, _timeout_ms);
}
void X11Wrapper::setVideoMode(const VideoMode mode)
{
_grabber->setVideoMode(mode);
}